From ca3862a5109cd64f11c8b50f353460801f31f3c8 Mon Sep 17 00:00:00 2001 From: hai Date: Tue, 11 Apr 2023 11:31:51 +0700 Subject: [PATCH 1/3] support after upload hook --- src/HasImageUploads.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/HasImageUploads.php b/src/HasImageUploads.php index f394352..197d7b9 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 * @@ -482,6 +486,28 @@ protected function triggerAfterSaveHook($image): self return $this; } + /** + * Trigger user defined after upload hook. + * + * @return $this + * @throws \Exception + */ + protected function triggerAfterUploadHook(): self + { + $hook = $this->afterUploadHook; + 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 +779,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 +801,10 @@ protected function autoUpload(): void $field ); } + $isUpload = true; + } + if ($isUpload) { + $this->triggerAfterUploadHook(); } } From 54ef8513a82c3c4d27ac2a14229d7a1c16a2132f Mon Sep 17 00:00:00 2001 From: hai Date: Tue, 11 Apr 2023 15:53:58 +0700 Subject: [PATCH 2/3] fix conflict --- src/HasImageUploads.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/HasImageUploads.php b/src/HasImageUploads.php index 197d7b9..77bdb61 100644 --- a/src/HasImageUploads.php +++ b/src/HasImageUploads.php @@ -158,6 +158,14 @@ public function getDefinedFileFields(): array { return static::$fileFields ?? $this->filesFields; } + /** + * Get after_upload hook defined on model + * + */ + public function getAfterUploadHook() + { + return static::$afterUploadHook ?? $this->afterUploadHook; + } /** * Get upload field options @@ -494,7 +502,7 @@ protected function triggerAfterSaveHook($image): self */ protected function triggerAfterUploadHook(): self { - $hook = $this->afterUploadHook; + $hook = $this->getAfterUploadHook(); if (isset($hook)) { if (is_callable($hook)) { $hook($this); From 03e8e6c01898a2b7e4aaf9499e85cea917c7a150 Mon Sep 17 00:00:00 2001 From: hai Date: Tue, 11 Apr 2023 16:41:55 +0700 Subject: [PATCH 3/3] fix hook name --- src/HasImageUploads.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HasImageUploads.php b/src/HasImageUploads.php index 77bdb61..fc90230 100644 --- a/src/HasImageUploads.php +++ b/src/HasImageUploads.php @@ -164,7 +164,7 @@ public function getDefinedFileFields(): array */ public function getAfterUploadHook() { - return static::$afterUploadHook ?? $this->afterUploadHook; + return static::$uploadedHook ?? $this->afterUploadHook; } /**