From 9d418d5623ac31d512e840f9f5953a2094ea362f Mon Sep 17 00:00:00 2001 From: Cassandre Cantet Date: Tue, 3 Oct 2017 18:10:59 +0200 Subject: [PATCH] Ajout Exception. --- src/Exception/InvalidContent.php | 8 ++++++++ src/Model/Image.php | 23 +++++++++++++++++++---- src/Providers/ImagesServiceProvider.php | 1 - 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 src/Exception/InvalidContent.php diff --git a/src/Exception/InvalidContent.php b/src/Exception/InvalidContent.php new file mode 100644 index 0000000..a6db666 --- /dev/null +++ b/src/Exception/InvalidContent.php @@ -0,0 +1,8 @@ +to('/'.config('image.route').'/original/' . $this->filename); + return app('url')->to('/' . config('image.route') . '/original/' . $this->filename); } protected static function boot() { parent::boot(); - static::creating(function(Image $model) { + static::creating(function (Image $model) { if (empty($model->content)) { - throw new \Exception("Content of picture can't be empty at creation"); + throw new InvalidContent("Content must be defined to save image"); + } + if (!($model->content instanceof InterventionImage)) { + throw new InvalidContent("Content must be an instance of Intervention\Image"); } $model->savePicture(); }); @@ -66,7 +74,7 @@ class Image extends Model $parts = array_slice(str_split($filename, 2), 0, 2); - $path = $basePath.'/'.implode('/', $parts).'/'.$filename; + $path = $basePath . '/' . implode('/', $parts) . '/' . $filename; return $path; } @@ -91,6 +99,13 @@ class Image extends Model public function getContentAttribute($value) { + if (empty($this->_content)) { + try { + $this->_content = app('image')->make($this->getPath()); + } catch (NotReadableException $e) { + return null; + } + } return $this->_content; } diff --git a/src/Providers/ImagesServiceProvider.php b/src/Providers/ImagesServiceProvider.php index 1fdd754..0dc5902 100755 --- a/src/Providers/ImagesServiceProvider.php +++ b/src/Providers/ImagesServiceProvider.php @@ -14,7 +14,6 @@ class ImagesServiceProvider extends ServiceProvider require_once __DIR__ . '/../functions.php'; $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations'); - $this->mergeConfigFrom(__DIR__ . '/../../config/image.php', 'image');