Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #275 from davidyell/develop
Browse files Browse the repository at this point in the history
Merge for release
  • Loading branch information
davidyell authored Aug 2, 2019
2 parents b4a841e + 33e7905 commit d0fb682
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 69 deletions.
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
"source": "https://github.com/davidyell/CakePHP3-Proffer"
},
"require": {
"php": ">=5.6",
"php": ">=5.6.0",
"cakephp/orm": "3.*",
"intervention/image": "^2.3"
},
"require-dev": {
"phpunit/phpunit": "^5.5",
"cakephp/cakephp": "^3.4.0",
"cakephp/cakephp-codesniffer": "^2.0"
"phpunit/phpunit": "^5|^6",
"cakephp/cakephp": "~3.4",
"cakephp/cakephp-codesniffer": "~3.0"
},
"autoload": {
"psr-4": {
Expand All @@ -37,5 +37,10 @@
"Proffer\\Tests\\Fixture\\": "tests/Fixture",
"Proffer\\Tests\\Stubs\\": "tests/Stubs"
}
},
"scripts": {
"cs-check": "phpcs --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/",
"cs-fix": "phpcbf --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/",
"test": "phpunit --colors=always"
}
}
10 changes: 8 additions & 2 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ It's always advised to lock your dependencies to a specific version number. You
:warning: Installing the plugin without the use of Composer is unsupported, you do so at your own risk.

## CakePHP
Then you'll need to load the plugin in your `config/bootstrap.php` file.
Then you'll need to load the plugin in your `src/Application.php` file.

```php
Plugin::load('Proffer');
$this->addPlugin('Proffer');
```

or you can use the console to do this for you.

```bash
bin/cake plugin load Proffer
```

## Database
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
autoload_files:
- tests/bootstrap.php
6 changes: 5 additions & 1 deletion src/Lib/ImageTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ protected function thumbnailResize(Image $image, $width, $height)
*/
protected function thumbnailCustom(Image $image, $custom, $params)
{
return call_user_func_array([$image, $custom], $params);
if (method_exists($image, $custom)) {
return call_user_func_array($image->{$custom}(), $params);
}

return $image;
}

/**
Expand Down
16 changes: 11 additions & 5 deletions src/Lib/ProfferPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Proffer\Lib;

use Cake\ORM\Entity;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Table;
use Cake\Utility\Text;

Expand All @@ -31,11 +31,11 @@ class ProfferPath implements ProfferPathInterface
* Construct the class and setup the defaults
*
* @param Table $table Instance of the table
* @param Entity $entity Instance of the entity data
* @param EntityInterface $entity Instance of the entity data
* @param string $field The name of the upload field
* @param array $settings Array of settings for the upload field
*/
public function __construct(Table $table, Entity $entity, $field, array $settings)
public function __construct(Table $table, EntityInterface $entity, $field, array $settings)
{
if (isset($settings['root'])) {
$this->setRoot($settings['root']);
Expand Down Expand Up @@ -253,13 +253,19 @@ public function createPathFolder()
*
* @param string $folder Absolute path to the folder
* @param bool $rmdir If you want to remove the folder as well
* @return void
* @return bool
*/
public function deleteFiles($folder, $rmdir = false)
{
array_map('unlink', glob($folder . DS . '*'));
$fileList = glob($folder . DS . '*');
if ($fileList !== false) {
array_map('unlink', $fileList);
}

if ($rmdir) {
rmdir($folder);
}

return true;
}
}
15 changes: 6 additions & 9 deletions src/Model/Behavior/ProfferBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function initialize(array $config)
Type::map('proffer.file', '\Proffer\Database\Type\FileType');
$schema = $this->_table->getSchema();
foreach (array_keys($this->getConfig()) as $field) {
$schema->setColumnType($field, 'proffer.file');
if (is_string($field)) {
$schema->setColumnType($field, 'proffer.file');
}
}
$this->_table->setSchema($schema);
}
Expand Down Expand Up @@ -75,12 +77,10 @@ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $opti
* @param \Proffer\Lib\ProfferPathInterface|null $path Inject an instance of ProfferPath
*
* @return true
*
* @throws \Exception
*/
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options, ProfferPathInterface $path = null)
{

foreach ($this->getConfig() as $field => $settings) {
$tableEntityClass = $this->_table->getEntityClass();

Expand Down Expand Up @@ -109,9 +109,8 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o
* @param \Cake\Datasource\EntityInterface $entity The current entity to process
* @param \Proffer\Lib\ProfferPathInterface|null $path Inject an instance of ProfferPath
*
* @throws \Exception If the file cannot be renamed / moved to the correct path
*
* @return void
* @throws \Exception If the file cannot be renamed / moved to the correct path
*/
protected function process($field, array $settings, EntityInterface $entity, ProfferPathInterface $path = null)
{
Expand Down Expand Up @@ -153,9 +152,8 @@ protected function process($field, array $settings, EntityInterface $entity, Pro
* @param array $settings Array of upload settings for the field
* @param \Proffer\Lib\ProfferPathInterface|null $path Inject an instance of ProfferPath
*
* @throws \App\Exception\InvalidClassException If the custom class doesn't implement the interface
*
* @return \Proffer\Lib\ProfferPathInterface
* @throws \Proffer\Exception\InvalidClassException If the custom class doesn't implement the interface
*/
protected function createPath(EntityInterface $entity, $field, array $settings, ProfferPathInterface $path = null)
{
Expand Down Expand Up @@ -187,9 +185,8 @@ protected function createPath(EntityInterface $entity, $field, array $settings,
* @param array $settings Array of upload field settings
* @param \Proffer\Lib\ProfferPathInterface $path Instance of the path class
*
* @throws \App\Exception\InvalidClassException If the transform class doesn't implement the interface
*
* @return void
* @throws \Proffer\Exception\InvalidClassException If the transform class doesn't implement the interface
*/
protected function createThumbnails(EntityInterface $entity, array $settings, ProfferPathInterface $path)
{
Expand Down
34 changes: 24 additions & 10 deletions src/Shell/ProfferShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use Cake\Console\Shell;
use Cake\Core\Exception\Exception;
use Cake\ORM\Entity;
use Cake\ORM\ResultSet;
use Proffer\Lib\ImageTransform;
use Proffer\Lib\ProfferPath;

Expand Down Expand Up @@ -75,14 +77,16 @@ public function getOptionParser()
/**
* Introduction to the shell
*
* @return void
* @return bool|int|null
*/
public function main()
{
$this->out('Welcome to the Proffer shell.');
$this->out('This shell can be used to regenerate thumbnails and cleanup unlinked images.');
$this->hr();
$this->out($this->OptionParser->help());

return parent::main();
}

/**
Expand Down Expand Up @@ -161,19 +165,28 @@ public function cleanup($table)
$this->out($this->nl(0));
}

$config = $this->Table->behaviors()->Proffer->config();
$config = $this->Table->behaviors()->get('Proffer')->config();

// Get the root upload folder for this table
$uploadFieldFolders = glob(WWW_ROOT . 'files' . DS . strtolower($table) . DS . '*');
if (!is_array($uploadFieldFolders)) {
$this->err('No files found to process.');
$this->_stop();
}

// Loop through each upload field configured for this table (field)
foreach ($uploadFieldFolders as $fieldFolder) {
foreach ((array)$uploadFieldFolders as $fieldFolder) {
// Loop through each instance of an upload for this field (seed)
$pathFieldName = pathinfo($fieldFolder, PATHINFO_BASENAME);
$pathFieldName = pathinfo((string)$fieldFolder, PATHINFO_BASENAME);
$uploadFolders = glob($fieldFolder . DS . '*');
foreach ($uploadFolders as $seedFolder) {
if (!is_array($uploadFolders)) {
$this->err('No folders found to process.');
$this->_stop();
}

foreach ((array)$uploadFolders as $seedFolder) {
// Does the seed exist in the db?
$seed = pathinfo($seedFolder, PATHINFO_BASENAME);
$seed = pathinfo((string)$seedFolder, PATHINFO_BASENAME);

foreach ($config as $field => $settings) {
if ($pathFieldName != $field) {
Expand All @@ -182,7 +195,8 @@ public function cleanup($table)

$targets = [];

$record = $this->{$this->Table->alias()}->find()
/** @var Entity|false $record */
$record = $this->{$this->Table->getAlias()}->find()
->select([
$field,
$settings['dir']
Expand All @@ -207,8 +221,8 @@ public function cleanup($table)
$this->out(__("Would remove folder `$seed`"));
}
} else {
array_map('unlink', glob($seedFolder . DS . '*'));
rmdir($seedFolder);
array_map('unlink', (array)glob($seedFolder . DS . '*'));
rmdir((string)$seedFolder);

if ($this->param('verbose')) {
$this->out(__("Remove `$seedFolder` folder and contents"));
Expand All @@ -217,7 +231,7 @@ public function cleanup($table)
}
}
} else {
$files = glob($seedFolder . DS . '*');
$files = (array)glob($seedFolder . DS . '*');

$filenames = array_map(function ($p) {
return pathinfo($p, PATHINFO_BASENAME);
Expand Down
11 changes: 9 additions & 2 deletions tests/TestCase/Lib/ProfferPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

use Cake\Core\Plugin;
use Cake\ORM\Entity;
use PHPUnit_Framework_TestCase;
use Cake\TestSuite\TestCase;
use Proffer\Lib\ProfferPath;

class ProfferPathTest extends PHPUnit_Framework_TestCase
class ProfferPathTest extends TestCase
{

/**
Expand All @@ -37,6 +37,13 @@ protected function _rrmdir($dir)
}
}

public function setUp()
{
parent::setUp();

$this->loadPlugins(['Proffer' => ['path' => ROOT]]);
}

/**
* Clear up any generated images after each test
*
Expand Down
35 changes: 12 additions & 23 deletions tests/TestCase/Model/Behavior/ProfferBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use Cake\Event\EventManager;
use Cake\ORM\Entity;
use Cake\ORM\Table;
use Cake\TestSuite\TestCase;
use Cake\Validation\Validator;
use PHPUnit_Framework_TestCase;
use Proffer\Lib\ProfferPath;
use Proffer\Model\Behavior\ProfferBehavior;
use Proffer\Tests\Stubs\TestPath;
Expand All @@ -26,7 +26,7 @@
*
* @package Proffer\Tests\Model\Behavior
*/
class ProfferBehaviorTest extends PHPUnit_Framework_TestCase
class ProfferBehaviorTest extends TestCase
{

private $config = [
Expand All @@ -45,6 +45,10 @@ class ProfferBehaviorTest extends PHPUnit_Framework_TestCase
*/
public function setUp()
{
$this->loadPlugins([
'Proffer' => ['path' => ROOT]
]);

$this->config['photo']['root'] = TMP . 'ProfferTests' . DS;
}

Expand Down Expand Up @@ -209,11 +213,11 @@ public function testBeforeMarshal(array $data, $allowEmpty, array $expected)
public function validFileProvider()
{
return [
[
'landscape image' => [
[
'photo' => [
'name' => 'image_640x480.jpg',
'tmp_name' => Plugin::path('Proffer') . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg',
'tmp_name' => ROOT . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg',
'size' => 33000,
'error' => UPLOAD_ERR_OK
],
Expand All @@ -224,26 +228,11 @@ public function validFileProvider()
'dir' => 'proffer_test'
]
],
[
[
'photo' => [
'name' => 'image_480x640.jpg',
'tmp_name' => Plugin::path('Proffer') . 'tests' . DS . 'Fixture' . DS . 'image_480x640.jpg',
'size' => 45704,
'error' => UPLOAD_ERR_OK
],
'photo_dir' => 'proffer_test'
],
[
'filename' => 'image_480x640.jpg',
'dir' => 'proffer_test'
]
],
[
'portrait image' => [
[
'photo' => [
'name' => 'image_480x640.jpg',
'tmp_name' => Plugin::path('Proffer') . 'tests' . DS . 'Fixture' . DS . 'image_480x640.jpg',
'tmp_name' => ROOT . 'tests' . DS . 'Fixture' . DS . 'image_480x640.jpg',
'size' => 45704,
'error' => UPLOAD_ERR_OK
],
Expand All @@ -253,7 +242,7 @@ public function validFileProvider()
'filename' => 'image_480x640.jpg',
'dir' => 'proffer_test'
]
],
]
];
}

Expand Down Expand Up @@ -290,7 +279,7 @@ public function testBeforeSaveWithValidFile(array $entityData, array $expected)
->willReturnCallback(function ($source, $destination) {
if (!file_exists(pathinfo($destination, PATHINFO_DIRNAME))) {
mkdir(pathinfo($destination, PATHINFO_DIRNAME), 0777, true);
}
}

return copy($source, $destination);
});
Expand Down
Loading

0 comments on commit d0fb682

Please sign in to comment.