diff --git a/src/HasImageUploads.php b/src/HasImageUploads.php index f394352..fc90230 100644 --- a/src/HasImageUploads.php +++ b/src/HasImageUploads.php @@ -26,6 +26,10 @@ trait HasImageUploads * @var string[] */ private $imagesFields = []; + /** + * After all upload hook + */ + private $afterUploadHook; /** * All the file fields for model * @@ -154,6 +158,14 @@ public function getDefinedFileFields(): array { return static::$fileFields ?? $this->filesFields; } + /** + * Get after_upload hook defined on model + * + */ + public function getAfterUploadHook() + { + return static::$uploadedHook ?? $this->afterUploadHook; + } /** * Get upload field options @@ -482,6 +494,28 @@ protected function triggerAfterSaveHook($image): self return $this; } + /** + * Trigger user defined after upload hook. + * + * @return $this + * @throws \Exception + */ + protected function triggerAfterUploadHook(): self + { + $hook = $this->getAfterUploadHook(); + if (isset($hook)) { + if (is_callable($hook)) { + $hook($this); + } + // We assume that the user is passing the hook class name + if (is_string($hook)) { + $instance = app($hook); + $instance->handle($this); + } + } + + return $this; + } /** * Process image upload @@ -753,6 +787,7 @@ public function enableAutoUpload(): self */ protected function autoUpload(): void { + $isUpload = false; foreach ($this->getDefinedUploadFields() as $key => $val) { $field = is_int($key) ? $val : $key; $options = Arr::wrap($val); @@ -774,6 +809,10 @@ protected function autoUpload(): void $field ); } + $isUpload = true; + } + if ($isUpload) { + $this->triggerAfterUploadHook(); } }