Ajout Exception.
This commit is contained in:
		
							
								
								
									
										8
									
								
								src/Exception/InvalidContent.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/Exception/InvalidContent.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| <?php | ||||
|  | ||||
| namespace Meoran\Images\Exception; | ||||
|  | ||||
| class InvalidContent extends \Exception | ||||
| { | ||||
|  | ||||
| } | ||||
| @@ -3,7 +3,10 @@ | ||||
| namespace Meoran\Images\Model; | ||||
|  | ||||
| use Illuminate\Database\Eloquent\Model; | ||||
| use Intervention\Image\Exception\NotReadableException; | ||||
| use Meoran\Images\Exception\InvalidContent; | ||||
| use Spatie\ImageOptimizer\OptimizerChain; | ||||
| use \Intervention\Image\Image as InterventionImage; | ||||
|  | ||||
| /** | ||||
|  * Class Image | ||||
| @@ -15,6 +18,8 @@ class Image extends Model | ||||
|  | ||||
|     protected $_content; | ||||
|  | ||||
|     protected $table = 'images'; | ||||
|  | ||||
|     public $fillable = [ | ||||
|         'content', | ||||
|         'position', | ||||
| @@ -27,16 +32,19 @@ class Image extends Model | ||||
|  | ||||
|     public function getUrlAttribute() | ||||
|     { | ||||
|         return app('url')->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; | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -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'); | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user