From 8d6aeaa4c11ceb3892149bfc4ca45bea00713a24 Mon Sep 17 00:00:00 2001 From: Allan Mariucci Carvalho Date: Sat, 17 Mar 2018 11:17:24 -0300 Subject: [PATCH] Compatibility with cake 3.6.x and improviments --- src/File/Writer/DefaultWriter.php | 25 ++++++------ src/File/Writer/FileWriter.php | 13 ++++--- src/File/Writer/ImageWriter.php | 38 +++++++++--------- src/File/Writer/Traits/ImageTrait.php | 55 ++++++++++++++++----------- src/Model/Behavior/UploadBehavior.php | 21 ++++++---- src/Validation/DefaultValidation.php | 1 + src/Validation/ImageValidation.php | 1 + src/Validation/Traits/ImageTrait.php | 9 +++-- src/Validation/UploadValidation.php | 1 + 9 files changed, 95 insertions(+), 69 deletions(-) diff --git a/src/File/Writer/DefaultWriter.php b/src/File/Writer/DefaultWriter.php index 1188c1d..181b364 100644 --- a/src/File/Writer/DefaultWriter.php +++ b/src/File/Writer/DefaultWriter.php @@ -8,6 +8,7 @@ namespace Upload\File\Writer; +use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\Entity; use Cake\Filesystem\File; @@ -74,8 +75,8 @@ abstract class DefaultWriter implements WriterInterface * Construct Method * @param Table $table * @param Entity $entity - * @param type $field - * @param type $settings + * @param string $field + * @param array $settings */ public function __construct(Table $table, Entity $entity, $field, $settings) { @@ -89,7 +90,8 @@ public function __construct(Table $table, Entity $entity, $field, $settings) /** * Delete a file from path - * @param string $PathAndFilename + * @param $path + * @param $filename * @return boolean */ protected function _delete($path, $filename) @@ -99,12 +101,12 @@ protected function _delete($path, $filename) { if (!$file->delete()) { - \Cake\Log\Log::error(__d('upload', 'Unable to delete file "{0}" in entity id "{1}" from table "{2}" and path "{3}"', $filename, $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $path)); + Log::error(__d('upload', 'Unable to delete file "{0}" in entity id "{1}" from table "{2}" and path "{3}"', $filename, $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $path)); return false; } } else { - \Cake\Log\Log::error(__d('upload', 'Unable to delete file "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $filename, $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $path)); + Log::error(__d('upload', 'Unable to delete file "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $filename, $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $path)); return false; } return true; @@ -112,6 +114,7 @@ protected function _delete($path, $filename) /** * Get a path to save file + * @param null|string $subDirectory * @return string */ protected function getPath($subDirectory = null) @@ -146,13 +149,13 @@ protected function createFolderIfItNotExists($path) { if (!new Folder($path, true)) { - \Cake\Log\Log::error(__d('upload', 'Unable to create directory: {0}', $path)); + Log::error(__d('upload', 'Unable to create directory: {0}', $path)); } } /** * get a image save format from behavior config - * @return type + * @return string */ protected function getConfigFileFormat() { @@ -188,13 +191,13 @@ protected function createFilename($ifExistCreateNew = false) if ($this->filename === null) { $filePrefix = Hash::get($this->settings, 'prefix', ''); - $fileUniqidMoreEntropy = Hash::get($this->settings, 'more_entropy', true); - $this->filename = Hash::get($this->settings, 'filename', uniqid($filePrefix, $fileUniqidMoreEntropy)) . $this->getConfigFileFormat(); + $fileUniqueMoreEntropy = Hash::get($this->settings, 'more_entropy', true); + $this->filename = Hash::get($this->settings, 'filename', uniqid($filePrefix, $fileUniqueMoreEntropy)) . $this->getConfigFileFormat(); } elseif ($ifExistCreateNew === true) { $filePrefix = Hash::get($this->settings, 'prefix', ''); - $fileUniqidMoreEntropy = Hash::get($this->settings, 'more_entropy', true); - $this->filename = Hash::get($this->settings, 'filename', uniqid($filePrefix, $fileUniqidMoreEntropy)) . $this->getConfigFileFormat(); + $fileUniqueMoreEntropy = Hash::get($this->settings, 'more_entropy', true); + $this->filename = Hash::get($this->settings, 'filename', uniqid($filePrefix, $fileUniqueMoreEntropy)) . $this->getConfigFileFormat(); } return $this->filename; } diff --git a/src/File/Writer/FileWriter.php b/src/File/Writer/FileWriter.php index 293ea17..e95b59f 100644 --- a/src/File/Writer/FileWriter.php +++ b/src/File/Writer/FileWriter.php @@ -8,6 +8,7 @@ namespace Upload\File\Writer; +use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\Entity; use Cake\Filesystem\File; @@ -30,8 +31,8 @@ public function __construct(Table $table, Entity $entity, $field, $settings) /** * write a file - * @return boolean - */ + * @return boolean|string +*/ public function write() { if (!$this->entity->isNew()) @@ -47,19 +48,19 @@ public function write() return $this->entity->set($this->field, "{$this->getFileName()}"); } else { - \Cake\Log\Log::error(__d('upload', 'Unable to salve file "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $this->getFileName(), $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $this->getPath())); + Log::error(__d('upload', 'Unable to salve file "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $this->getFileName(), $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $this->getPath())); return false; } } /** * Delete method that delete primary and thumbnails images - * @param bool $isUptade + * @param bool $isUpdate * @return bool */ - public function delete($isUptade = false) + public function delete($isUpdate = false) { - if($isUptade === false) + if($isUpdate === false) { $entity = &$this->entity; }else diff --git a/src/File/Writer/ImageWriter.php b/src/File/Writer/ImageWriter.php index edb50c7..fa75571 100644 --- a/src/File/Writer/ImageWriter.php +++ b/src/File/Writer/ImageWriter.php @@ -8,6 +8,7 @@ namespace Upload\File\Writer; +use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\Entity; use Cake\Utility\Hash; @@ -29,7 +30,7 @@ class ImageWriter extends DefaultWriter * value of resize height * @var int */ - private $resize_heigth = false; + private $resize_height = false; /** * value of resize width @@ -47,7 +48,7 @@ class ImageWriter extends DefaultWriter * value of crop height * @var int */ - private $crop_heigth = false; + private $crop_height = false; /** * value of crop width @@ -107,18 +108,18 @@ class ImageWriter extends DefaultWriter * Constructor method * @param Table $table * @param Entity $entity - * @param type $field - * @param type $settings + * @param string $field + * @param array $settings */ public function __construct(Table $table, Entity $entity, $field, $settings) { parent::__construct($table, $entity, $field, $settings); $this->defaultPath = WWW_ROOT . 'img' . DS . $this->table->getAlias() . DS; - $this->resize_heigth = Hash::get($this->settings, 'image.resize.height', false); + $this->resize_height = Hash::get($this->settings, 'image.resize.height', false); $this->resize_width = Hash::get($this->settings, 'image.resize.width', false); $this->resize_min_size = Hash::get($this->settings, 'image.resize.min_size', false); - $this->crop_heigth = Hash::get($this->settings, 'image.crop.height', false); + $this->crop_height = Hash::get($this->settings, 'image.crop.height', false); $this->crop_width = Hash::get($this->settings, 'image.crop.width', false); $this->crop_x = Hash::get($this->settings, 'image.crop.x', null); $this->crop_y = Hash::get($this->settings, 'image.crop.y', null); @@ -132,7 +133,7 @@ public function __construct(Table $table, Entity $entity, $field, $settings) /** * write a image - * @return boolean + * @return boolean|string */ public function write() { @@ -151,7 +152,7 @@ public function write() return $this->entity->set($this->field, "{$this->getFileName()}"); } else { - \Cake\Log\Log::error(__d('upload', 'Unable to salve image "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $this->getFileName(), $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $this->getPath())); + Log::error(__d('upload', 'Unable to salve image "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $this->getFileName(), $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $this->getPath())); return false; } } else @@ -166,7 +167,7 @@ public function write() return $this->entity->set($this->field, "{$this->getFileName()}"); } else { - \Cake\Log\Log::error(__d('upload', 'Unable to salve image "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $this->getFileName(), $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $this->getPath())); + Log::error(__d('upload', 'Unable to salve image "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $this->getFileName(), $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $this->getPath())); return false; } } @@ -174,11 +175,12 @@ public function write() /** * delete images - * @param bool $isUptade + * @param bool $isUpdate + * @return bool */ - public function delete($isUptade = false) + public function delete($isUpdate = false) { - if ($isUptade === false) + if ($isUpdate === false) { $entity = &$this->entity; } else @@ -204,7 +206,9 @@ public function delete($isUptade = false) /** * Delete image thumbnails - * @param \Intervention\Image\Image $image + * @param $path + * @param $filename + * @return bool */ public function deleteThubnails($path, $filename) { @@ -234,13 +238,13 @@ public function deleteThubnails($path, $filename) /** * Modifier function calls * @param \Intervention\Image\Image $image - * @return \Intervention\Image\Image + * @return void */ private function modifyImage($image) { - $this->resize($image, $this->resize_width, $this->resize_heigth, $this->resize_min_size); + $this->resize($image, $this->resize_width, $this->resize_height, $this->resize_min_size); - $this->crop($image, $this->crop_width, $this->crop_heigth, $this->crop_x, $this->crop_y); + $this->crop($image, $this->crop_width, $this->crop_height, $this->crop_x, $this->crop_y); $this->createThumbnails(); @@ -252,7 +256,7 @@ private function modifyImage($image) /** * get a image quality from behavior config - * @return type + * @return int */ private function getConfigImageQuality() { diff --git a/src/File/Writer/Traits/ImageTrait.php b/src/File/Writer/Traits/ImageTrait.php index 6e427d8..46e2d32 100644 --- a/src/File/Writer/Traits/ImageTrait.php +++ b/src/File/Writer/Traits/ImageTrait.php @@ -8,6 +8,7 @@ namespace Upload\File\Writer\Traits; +use Cake\Log\Log; use Intervention\Image\ImageManager; use Intervention\Image\Image; use Cake\Utility\Hash; @@ -40,11 +41,12 @@ protected function getImage($path) } /** - * Resize a imagem based in + * Resize a imagem based in * @param Image $image * @param mixed $width must by smaller than the original * @param mixed $height must by smaller than the original - * @return Image + * @param $minSize + * @return void */ protected function resize($image, $width, $height, $minSize) { @@ -87,7 +89,9 @@ protected function resize($image, $width, $height, $minSize) * @param Image $image * @param mixed $width * @param mixed $height - * @return Image + * @param $x + * @param $y + * @return void */ protected function crop($image, $width, $height, $x, $y) { @@ -126,23 +130,24 @@ protected function crop($image, $width, $height, $x, $y) * @param \Intervention\Image\Image $image * @param string $path * @param string $position - * @return \Intervention\Image\Image + * @param $opacity + * @return void */ protected function insertWatermark($image, $path, $position, $opacity) { $watermark = $this->getImage($path); $targetHeight = intval($image->height() * 0.07); - $targetWarthermarkHeight = $targetHeight <= $watermark->height() ? $targetHeight : $watermark->height(); - $targetWarthermarkWidth = $this->getEquivalentResizeWidth($watermark, $targetWarthermarkHeight); + $targetWatermarkHeight = $targetHeight <= $watermark->height() ? $targetHeight : $watermark->height(); + $targetWatermarkWidth = $this->getEquivalentResizeWidth($watermark, $targetWatermarkHeight); - if ($targetWarthermarkWidth > $image->width()) + if ($targetWatermarkWidth > $image->width()) { $targetWidth = intval($image->width() * 0.8); - $targetWarthermarkWidth = $targetWidth <= $watermark->width() ? $targetWidth : $watermark->width(); - $targetWarthermarkHeight = $this->getEquivalentResizeHeight($image, $targetWarthermarkWidth); + $targetWatermarkWidth = $targetWidth <= $watermark->width() ? $targetWidth : $watermark->width(); + $targetWatermarkHeight = $this->getEquivalentResizeHeight($image, $targetWatermarkWidth); } - $this->resize($watermark, $targetWarthermarkWidth, $targetWarthermarkHeight, false); + $this->resize($watermark, $targetWatermarkWidth, $targetWatermarkHeight, false); $watermark->opacity($opacity); @@ -150,8 +155,7 @@ protected function insertWatermark($image, $path, $position, $opacity) } /** - * - * @param \Intervention\Image\Image $thumbnailImage + * * @return boolean */ protected function createThumbnails() @@ -227,7 +231,7 @@ protected function createThumbnails() $newThumbnail->interlace(true); if (!$newThumbnail->save($this->getPath($label) . $this->getFilename(), $this->getConfigImageQuality())) { - \Cake\Log\Log::error(__d('upload', 'Unable to salve thumbnail "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $this->getFileName(), $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $this->getPath())); + Log::error(__d('upload', 'Unable to salve thumbnail "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $this->getFileName(), $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $this->getPath())); } unset($newThumbnail); } @@ -252,22 +256,27 @@ private function greatestCommonDivisor($dividend_a, $dividend_b) */ private function getEquivalentResizeWidth($image, $newImageHeight) { - $imageWidht = $image->width(); + $imageWidth = $image->width(); $imageHeight = $image->height(); - $gcd = round($this->greatestCommonDivisor($imageWidht, $imageHeight)); - $widthRadio = round($imageWidht / $gcd); - $heigthRadio = round($imageHeight / $gcd); - return round(($newImageHeight / $heigthRadio) * $widthRadio); + $gcd = round($this->greatestCommonDivisor($imageWidth, $imageHeight)); + $widthRadio = round($imageWidth / $gcd); + $heightRadio = round($imageHeight / $gcd); + return round(($newImageHeight / $heightRadio) * $widthRadio); } + /** + * @param Image $image + * @param $newImageWidth + * @return float + */ private function getEquivalentResizeHeight($image, $newImageWidth) { - $imageWidht = $image->width(); + $imageWidth = $image->width(); $imageHeight = $image->height(); - $gcd = round($this->greatestCommonDivisor($imageWidht, $imageHeight)); - $widthRadio = round($imageWidht / $gcd); - $heigthRadio = round($imageHeight / $gcd); - return round(($newImageWidth / $widthRadio) * $heigthRadio); + $gcd = round($this->greatestCommonDivisor($imageWidth, $imageHeight)); + $widthRadio = round($imageWidth / $gcd); + $heightRadio = round($imageHeight / $gcd); + return round(($newImageWidth / $widthRadio) * $heightRadio); } } diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index a435bb9..4097791 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -8,14 +8,15 @@ namespace Upload\Model\Behavior; +use Cake\Log\Log; use Cake\ORM\Behavior; -use Intervention\Image\Image; use Cake\Event\Event; -use Cake\ORM\Query; use Cake\Datasource\EntityInterface; use Cake\Utility\Hash; use Cake\Database\Type; use Cake\ORM\Entity; +use Psr\Log\LogLevel; +use UnexpectedValueException; /** * CakePHP UploadBehavior @@ -46,14 +47,14 @@ public function initialize(array $config) $schema = $this->_table->getSchema(); foreach (array_keys($this->getConfig()) as $field) { - $schema->columnType($field, 'upload.file'); + $schema->setColumnType($field, 'upload.file'); } - $this->_table->schema($schema); + $this->_table->setSchema($schema); } public function beforeMarshal(Event $event, \ArrayObject $data, \ArrayObject $options) { - $validator = $this->_table->validator(); + $validator = $this->_table->getValidator(); $dataArray = $data->getArrayCopy(); foreach (array_keys($this->getConfig()) as $field) { @@ -70,21 +71,22 @@ public function beforeMarshal(Event $event, \ArrayObject $data, \ArrayObject $op } /** - * + * * @param Event $event * @param EntityInterface $entity * @param \ArrayObject $options * @return boolean + * @throws UnexpectedValueException */ public function beforeSave(Event $event, EntityInterface $entity, \ArrayObject $options) { foreach ($this->getConfig() as $field => $settings) { - if ($entity->has($field) and $entity->dirty($field)) + if ($entity->has($field) and $entity->isDirty($field)) { if (Hash::get((array) $entity->get($field), 'error') != UPLOAD_ERR_OK) { - \Cake\Log\Log::write(\Psr\Log\LogLevel::ERROR, __d('upload', 'File upload had the following error: {0}', $this->getUploadError($entity->get($field)['error']))); + Log::write(LogLevel::ERROR, __d('upload', 'File upload had the following error: {0}', $this->getUploadError($entity->get($field)['error']))); return false; } @@ -95,6 +97,7 @@ public function beforeSave(Event $event, EntityInterface $entity, \ArrayObject $ } } } + return null; } public function afterDelete(Event $event, Entity $entity, \ArrayObject $options) @@ -114,6 +117,7 @@ public function afterDelete(Event $event, Entity $entity, \ArrayObject $options) { return $result; } + return null; } /** @@ -124,6 +128,7 @@ public function afterDelete(Event $event, Entity $entity, \ArrayObject $options) */ public function deleteFiles($entity, $fields = []) { + $configs = []; if (empty($fields)) { $configs = $this->getConfig(); diff --git a/src/Validation/DefaultValidation.php b/src/Validation/DefaultValidation.php index 4c304d1..0adf6ed 100644 --- a/src/Validation/DefaultValidation.php +++ b/src/Validation/DefaultValidation.php @@ -8,6 +8,7 @@ namespace Upload\Validation; +use Cake\Core\Exception\Exception; use Upload\Validation\Traits\ImageTrait; use Upload\Validation\Traits\UploadTrait; diff --git a/src/Validation/ImageValidation.php b/src/Validation/ImageValidation.php index 66c8a92..1070cb8 100644 --- a/src/Validation/ImageValidation.php +++ b/src/Validation/ImageValidation.php @@ -8,6 +8,7 @@ namespace Upload\Validation; +use Cake\Core\Exception\Exception; use Upload\Validation\Traits\ImageTrait; /** diff --git a/src/Validation/Traits/ImageTrait.php b/src/Validation/Traits/ImageTrait.php index 6712e81..a5ced4e 100644 --- a/src/Validation/Traits/ImageTrait.php +++ b/src/Validation/Traits/ImageTrait.php @@ -123,13 +123,13 @@ public static function isBelowMaxHeight($check, $height) return $image->height() > $height ? false : true; } - - + + /** * Check that the file has exact width requirement * * @param mixed $check Value to check - * @param int $height Height of Image + * @param $width * @return bool Success */ public static function isThisWidth($check, $width) @@ -161,11 +161,12 @@ public static function isThisHeight($check, $height) $image = self::getImage($check['tmp_name']); return $image->height() == $height ? true : false; } - + /** * Check that the file has exact height requirement * * @param mixed $check Value to check + * @param $width * @param int $height Height of Image * @return bool Success */ diff --git a/src/Validation/UploadValidation.php b/src/Validation/UploadValidation.php index 6620928..51c25a8 100644 --- a/src/Validation/UploadValidation.php +++ b/src/Validation/UploadValidation.php @@ -8,6 +8,7 @@ namespace Upload\Validation; +use Cake\Core\Exception\Exception; use Upload\Validation\Traits\UploadTrait; /**