diff --git a/src/Uploadify/AbstractDriver.php b/src/Uploadify/AbstractDriver.php index c7a987e..3f1c026 100644 --- a/src/Uploadify/AbstractDriver.php +++ b/src/Uploadify/AbstractDriver.php @@ -145,7 +145,8 @@ protected function getSourceType($source) protected function setFileInfo() { switch ($this->sourceType) { - case 'path': case 'url': + case 'path': + case 'url': $this->setBasename(pathinfo($this->source, PATHINFO_FILENAME)); $this->setExtension(pathinfo($this->source, PATHINFO_EXTENSION)); break; @@ -239,7 +240,7 @@ public function delete() */ public function getName() { - return $this->basename.'.'.$this->extension; + return $this->basename . '.' . $this->extension; } /** @@ -251,7 +252,7 @@ protected function uploadFromPath() { $path = $this->storage->disk($this->getDisk())->getDriver()->getAdapter()->getPathPrefix(); - return copy($this->source, $path.DIRECTORY_SEPARATOR.$this->getPath().DIRECTORY_SEPARATOR.$this->getName()); + return copy($this->source, $path . DIRECTORY_SEPARATOR . $this->getPath() . DIRECTORY_SEPARATOR . $this->getName()); } /** @@ -263,7 +264,7 @@ protected function uploadFromUrl() { $contents = file_get_contents($this->source); - return $this->storage->disk($this->getDisk())->put($this->getPath().DIRECTORY_SEPARATOR.$this->getName(), $contents); + return $this->storage->disk($this->getDisk())->put($this->getPath() . DIRECTORY_SEPARATOR . $this->getName(), $contents); } /** @@ -289,13 +290,13 @@ protected function rename() $name = $this->getName(); $i = 1; - while ($this->storage->disk($this->getDisk())->exists($this->getPath().DIRECTORY_SEPARATOR.$name)) { - $name = $this->basename.'-'.$i.'.'.$this->extension; + while ($this->storage->disk($this->getDisk())->exists($this->getPath() . DIRECTORY_SEPARATOR . $name)) { + $name = $this->basename . '-' . $i . '.' . $this->extension; $i++; } - $this->setBasename(rtrim($name, '.'.$this->extension)); + $this->setBasename(rtrim($name, '.' . $this->extension)); $this->setModelInfo(); return $name; diff --git a/src/Uploadify/Casts/Cast.php b/src/Uploadify/Casts/Cast.php index faf03ce..eb06a82 100644 --- a/src/Uploadify/Casts/Cast.php +++ b/src/Uploadify/Casts/Cast.php @@ -8,169 +8,77 @@ abstract class Cast { - /** - * The full file name with extension - * - * @var string - */ - protected $name; - - /** - * The path to file - * - * @var string - */ - protected $path; - - /** - * The filesystems disk name - * - * @var string - */ - protected $disk; - - /** - * Create new cast instance - * - * @param string $name The full file name with extension - * @param array $settings List of settings => path, path_thumb, disk... - * @return void - */ + protected $name; // Nome completo do arquivo + protected $path; // Caminho para o arquivo + protected $disk; // Nome do disco do sistema de arquivos + public function __construct($name, array $settings = []) { $this->name = $name; $this->saveSettings($settings); } - /** - * Save setting values from array - * - * @param array $settings - * @return void - */ protected function saveSettings(array $settings = []) { - $this->setPath(Arr::has($settings, 'path') ? $settings['path'] : ''); - $this->setDisk(Arr::has($settings, 'disk') ? $settings['disk'] : null); + $this->setPath(Arr::get($settings, 'path', '')); + $this->setDisk(Arr::get($settings, 'disk', null)); } - /** - * Set path value - * - * @param string $path - * @return void - */ protected function setPath($path) { $this->path = trim($path, '/'); } - /** - * Set disk value - * - * @param string $disk - * @return void - */ protected function setDisk($disk = null) { $this->disk = $disk ?: Config::get('uploadify.disk'); } - /** - * Get file name with extension - * - * @return string - */ public function name() { return $this->name; } - /** - * Get file base name without extension - * - * @return string - */ public function basename() { return pathinfo($this->name(), PATHINFO_FILENAME); } - /** - * Get file extension - * - * @return string - */ public function extension() { return pathinfo($this->name(), PATHINFO_EXTENSION); } - /** - * Get file size in bytes - * - * @return string - */ public function filesize() { - return $this->getStorage()->size($this->path().DIRECTORY_SEPARATOR.$this->name()); + return $this->getStorage()->size($this->path() . '/' . $this->name()); } - /** - * Get path - * - * @return string - */ public function path() { return $this->path; } - /** - * Delete file from filesystem - * - * @return bool - */ public function delete() { - return $this->getStorage()->delete($this->path().DIRECTORY_SEPARATOR.$this->name()); + return $this->getStorage()->delete($this->path() . '/' . $this->name()); } - /** - * Get filesystem disk name - * - * @return string - */ public function disk() { return $this->getDisk(); } - /** - * Get filesystems storage - * - * @return \Illuminate\Contracts\Filesystem\Filesystem - */ protected function getStorage() { return Storage::disk($this->getDisk()); } - /** - * Get filesystem disk name - * - * @return string - */ protected function getDisk() { return $this->disk; } - /** - * Get file name with extension - * - * @return string - */ public function __toString() { return $this->name(); diff --git a/src/Uploadify/Casts/FileCast.php b/src/Uploadify/Casts/FileCast.php index 8b15468..34d755a 100644 --- a/src/Uploadify/Casts/FileCast.php +++ b/src/Uploadify/Casts/FileCast.php @@ -2,17 +2,10 @@ namespace Uploadify\Casts; -use Uploadify\Casts\Cast as BaseCast; - -class FileCast extends BaseCast +class FileCast extends Cast { - /** - * Get full url to file - * - * @return string - */ public function url() { - return $this->getStorage()->url($this->path().'/'.$this->name()); + return $this->getStorage()->url($this->path() . '/' . $this->name()); } } diff --git a/src/Uploadify/Casts/ImageCast.php b/src/Uploadify/Casts/ImageCast.php index 8063ed1..1e6a30b 100644 --- a/src/Uploadify/Casts/ImageCast.php +++ b/src/Uploadify/Casts/ImageCast.php @@ -2,18 +2,8 @@ namespace Uploadify\Casts; -use Uploadify\Casts\Cast as BaseCast; - -class ImageCast extends BaseCast +class ImageCast extends Cast { - /** - * Get full url to file - * - * @param int|array $width - * @param int $height - * @param array $options - * @return string - */ public function url($width = null, $height = null, array $options = []) { if (is_array($width)) { @@ -26,30 +16,24 @@ public function url($width = null, $height = null, array $options = []) $options = array_merge(['w' => $width], $options); } - if (! empty($options)) { - return $this->getStorage()->url($this->path().'/'.$this->prepareOptions($options).'/'.$this->name()); + if (!empty($options)) { + return $this->getStorage()->url($this->path() . '/' . $this->prepareOptions($options) . '/' . $this->name()); } - return $this->getStorage()->url($this->path().'/'.$this->name()); + return $this->getStorage()->url($this->path() . '/' . $this->name()); } - /** - * Prepare and convert options from array to string - * - * @param array $options - */ protected function prepareOptions(array $options = []) { $string = implode(',', array_map( function ($value, $key) { - return $key.'_'.$value; + return $key . '_' . $value; }, $options, array_keys($options) )); $from = ['width', 'height']; - $to = ['w', 'h']; return str_replace($from, $to, $string); diff --git a/src/Uploadify/Http/Controllers/ImageController.php b/src/Uploadify/Http/Controllers/ImageController.php index a5702d2..0530426 100644 --- a/src/Uploadify/Http/Controllers/ImageController.php +++ b/src/Uploadify/Http/Controllers/ImageController.php @@ -86,8 +86,8 @@ public function show($path, $options, $name, $extension = null) { $originalPath = $this->getPath($path); - $imagePath = $originalPath.'/'.$name.($extension ? '.'.$extension : ''); - $imageSmallPath = $originalPath.'/'.$this->config->get('uploadify.path').'/'.$this->slugifyName($name.','.$options).'.'.($extension ?: ''); + $imagePath = $originalPath . '/' . $name . ($extension ? '.' . $extension : ''); + $imageSmallPath = $originalPath . '/' . $this->config->get('uploadify.path') . '/' . $this->slugifyName($name . ',' . $options) . '.' . ($extension ?: ''); if ($this->exists($imagePath, $imageSmallPath)) { $image = $this->getDisk()->get($imageSmallPath); @@ -98,7 +98,7 @@ public function show($path, $options, $name, $extension = null) try { $imageNew = $this->imageManager->make($this->getDisk()->get($imagePath)); - } catch (FileNotFoundException $e) { + } catch (FileNotFoundException $e) { $imageNew = null; abort(404); @@ -112,7 +112,7 @@ public function show($path, $options, $name, $extension = null) if ($this->config->get('uploadify.cache')) { try { - $imageNew->save($this->getDisk()->getDriver()->getAdapter()->getPathPrefix().$imageSmallPath, 85); + $imageNew->save($this->getDisk()->getDriver()->getAdapter()->getPathPrefix() . $imageSmallPath, 85); } catch (NotWritableException $e) { $context = [ 'file' => $e->getFile(), @@ -227,7 +227,7 @@ protected function processImage(Image $image, $options) } if (Arr::has($options, 'effect')) { - switch($options['effect']) { + switch ($options['effect']) { case 'greyscale': $image->greyscale(); break; diff --git a/src/Uploadify/Providers/UploadifyServiceProvider.php b/src/Uploadify/Providers/UploadifyServiceProvider.php index d78c8ba..ff3601d 100644 --- a/src/Uploadify/Providers/UploadifyServiceProvider.php +++ b/src/Uploadify/Providers/UploadifyServiceProvider.php @@ -18,7 +18,7 @@ class UploadifyServiceProvider extends ServiceProvider public function boot() { $this->publishes([ - __DIR__.'/../../config/uploadify.php' => config_path('uploadify.php'), + __DIR__ . '/../../config/uploadify.php' => config_path('uploadify.php'), ], 'uploadify'); } @@ -30,7 +30,8 @@ public function boot() public function register() { $this->mergeConfigFrom( - __DIR__.'/../../config/uploadify.php', 'uploadify' + __DIR__ . '/../../config/uploadify.php', + 'uploadify' ); $this->app->singleton(UploadifyManager::class, function ($app) { diff --git a/src/Uploadify/Traits/UploadifyTrait.php b/src/Uploadify/Traits/UploadifyTrait.php index 081f6f3..2de21d1 100644 --- a/src/Uploadify/Traits/UploadifyTrait.php +++ b/src/Uploadify/Traits/UploadifyTrait.php @@ -8,6 +8,58 @@ trait UploadifyTrait { + /** + * Initialize the trait for an instance. + * + * @return void + */ + public function initializeUploadifyTrait() + { + if ($this->hasFileCasts()) { + foreach (array_keys($this->uploadifyFiles) as $key) { + if (! isset($this->attributes[$key])) { + $this->casts[$key] = 'filecast'; + } + } + } + + if ($this->hasImageCasts()) { + foreach (array_keys($this->uploadifyImages) as $key) { + if (! isset($this->attributes[$key])) { + $this->casts[$key] = 'imagecast'; + } + } + } + } + + /** + * Set a given attribute on the model. + * + * @param string $key + * @param mixed $value + * @return mixed + */ + public function setAttribute($key, $value) + { + if (is_string($value)) { + if ($this->hasFileCasts()) { + if (array_key_exists($key, $this->uploadifyFiles)) { + $this->attributes[$key] = $value; + return $this; + } + } + + if ($this->hasImageCasts()) { + if (array_key_exists($key, $this->uploadifyImages)) { + $this->attributes[$key] = $value; + return $this; + } + } + } + + return parent::setAttribute($key, $value); + } + /** * Cast an attribute to a native PHP type. * @@ -22,70 +74,46 @@ protected function castAttribute($key, $value) } switch ($this->getCastType($key)) { - case 'int': - case 'integer': - return (int) $value; - case 'real': - case 'float': - case 'double': - return (float) $value; - case 'string': - return (string) $value; - case 'bool': - case 'boolean': - return (bool) $value; - case 'object': - return $this->fromJson($value, true); - case 'array': - case 'json': - return $this->fromJson($value); - case 'collection': - return new BaseCollection($this->fromJson($value)); - case 'date': - return $this->asDate($value); - case 'datetime': - return $this->asDateTime($value); - case 'timestamp': - return $this->asTimestamp($value); - case 'file': + case 'filecast': if ($value) { return new FileCast($value, $this->uploadifyFiles[$key]); + } else { + return null; } - case 'image': + case 'imagecast': if ($value) { return new ImageCast($value, $this->uploadifyImages[$key]); + } else { + return null; } - default: - return $value; } + + return parent::castAttribute($key, $value); } /** - * Get the casts array. + * Determine if the given key is cast using a custom class. + * + * @param string $key + * @return bool * - * @return array + * @throws \Illuminate\Database\Eloquent\InvalidCastException */ - public function getCasts() + protected function isClassCastable($key) { - $casts = $this->casts; - - if ($this->getIncrementing()) { - $casts = array_merge([$this->getKeyName() => $this->getKeyType()], $casts); - } - if ($this->hasFileCasts()) { - foreach (array_keys($this->uploadifyFiles) as $key) { - $casts = array_merge([$key => 'file'], $casts); + if (array_key_exists($key, $this->uploadifyFiles)) { + return false; } } if ($this->hasImageCasts()) { - foreach (array_keys($this->uploadifyImages) as $key) { - $casts = array_merge([$key => 'image'], $casts); + if (array_key_exists($key, $this->uploadifyImages)) { + return false; } } - return $casts; + return parent::isClassCastable($key); } /** diff --git a/src/Uploadify/UploadifyManager.php b/src/Uploadify/UploadifyManager.php index cb02af0..5ec97f8 100644 --- a/src/Uploadify/UploadifyManager.php +++ b/src/Uploadify/UploadifyManager.php @@ -62,12 +62,12 @@ public function create($file, Eloquent $model, $field) private function getDriver(Eloquent $model, $field) { if ($model->hasFileCasts() && array_key_exists($field, $model->uploadifyFiles)) { - return 'file'; + return 'filecast'; } elseif ($model->hasImageCasts() && array_key_exists($field, $model->uploadifyImages)) { - return 'image'; + return 'imagecast'; } - throw new InvalidFieldException('Field "'.$field.'" is not defined as Uploadify field!'); + throw new InvalidFieldException('Field "' . $field . '" is not defined as Uploadify field!'); } /** @@ -83,10 +83,10 @@ private function getDriver(Eloquent $model, $field) private function createDriver($driver, $file, Eloquent $model, $field) { $name = ucfirst($driver); - $class = '\\Uploadify\\Driver\\'.$name; + $class = '\\Uploadify\\Driver\\' . $name; if (! class_exists($class)) { - throw new InvalidDriverException('Driver "'.$class.'" does not exists!'); + throw new InvalidDriverException('Driver "' . $class . '" does not exists!'); } return new $class($this->storage, $this->settings, $file, $model, $field);