diff --git a/src/CollectionExtension.php b/src/CollectionExtension.php index a268d6f..092cd25 100644 --- a/src/CollectionExtension.php +++ b/src/CollectionExtension.php @@ -2,96 +2,89 @@ namespace Zoomyboy\MedialibraryHelper; -use Spatie\MediaLibrary\MediaCollections\MediaCollection; - class CollectionExtension { - public function boot(): void + public function forceFileName() { - MediaCollection::mixin(new class() { - public function forceFileName() - { - return fn ($callback) => $this->registerCustomCallback('forceFileName', $callback); + return fn ($callback) => $this->registerCustomCallback('forceFileName', $callback); + } + + public function storing() + { + return fn ($callback) => $this->registerCustomCallback('storing', $callback); + } + + public function destroyed() + { + return fn ($callback) => $this->registerCustomCallback('destroyed', $callback); + } + + public function after() + { + return fn ($callback) => $this->registerCustomCallback('after', $callback); + } + + public function withDefaultProperties() + { + return fn ($callback) => $this->registerCustomCallback('withDefaultProperties', $callback); + } + + public function stored() + { + return fn ($callback) => $this->registerCustomCallback('stored', $callback); + } + + public function withPropertyValidation() + { + return fn ($callback) => $this->registerCustomCallback('withPropertyValidation', $callback); + } + + public function withFallback() + { + return fn ($callback) => $this->registerCustomCallback('withFallback', $callback); + } + + public function maxWidth() + { + return fn ($callback) => $this->registerCustomCallback('maxWidth', $callback); + } + + public function runCallback() + { + return function (string $callback, ...$parameters) { + $this->setDefaultCustomCallbacks(); + + return call_user_func($this->customCallbacks->get($callback), ...$parameters); + }; + } + + public function registerCustomCallback() + { + return function (string $name, callable $callback) { + $this->setDefaultCustomCallbacks(); + $this->customCallbacks->put($name, $callback); + + return $this; + }; + } + + public function setDefaultCustomCallbacks() + { + return function () { + if (property_exists($this, 'customCallbacks')) { + return; } - - public function storing() - { - return fn ($callback) => $this->registerCustomCallback('storing', $callback); - } - - public function destroyed() - { - return fn ($callback) => $this->registerCustomCallback('destroyed', $callback); - } - - public function after() - { - return fn ($callback) => $this->registerCustomCallback('after', $callback); - } - - public function withDefaultProperties() - { - return fn ($callback) => $this->registerCustomCallback('withDefaultProperties', $callback); - } - - public function stored() - { - return fn ($callback) => $this->registerCustomCallback('stored', $callback); - } - - public function withPropertyValidation() - { - return fn ($callback) => $this->registerCustomCallback('withPropertyValidation', $callback); - } - - public function withFallback() - { - return fn ($callback) => $this->registerCustomCallback('withFallback', $callback); - } - - public function maxWidth() - { - return fn ($callback) => $this->registerCustomCallback('maxWidth', $callback); - } - - public function runCallback() - { - return function (string $callback, ...$parameters) { - $this->setDefaultCustomCallbacks(); - - return call_user_func($this->customCallbacks->get($callback), ...$parameters); - }; - } - - public function registerCustomCallback() - { - return function (string $name, callable $callback) { - $this->setDefaultCustomCallbacks(); - $this->customCallbacks->put($name, $callback); - - return $this; - }; - } - - public function setDefaultCustomCallbacks() - { - return function () { - if (property_exists($this, 'customCallbacks')) { - return; - } - $this->customCallbacks = collect([ - 'forceFileName' => fn ($model, $name) => $name, - 'maxWidth' => fn ($size) => null, - 'stored' => fn ($event) => true, - 'after' => fn ($event) => true, - 'destroyed' => fn ($event) => true, - 'storing' => fn ($adder, $name) => $adder, - 'withDefaultProperties' => fn ($path) => [], - 'withPropertyValidation' => fn ($path) => [], - 'withFallback' => fn ($parent) => null, - ]); - }; - } - }); + $this->customCallbacks = collect([ + 'forceFileName' => fn ($model, $name) => $name, + 'maxWidth' => fn ($size) => null, + 'stored' => fn ($event) => true, + 'after' => fn ($event) => true, + 'destroyed' => fn ($event) => true, + 'storing' => fn ($adder, $name) => $adder, + 'withDefaultProperties' => fn ($path) => [], + 'withPropertyValidation' => fn ($path) => [], + 'withFallback' => fn ($parent) => null, + ]); + }; } } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 443b1ee..00da40e 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -4,6 +4,7 @@ namespace Zoomyboy\MedialibraryHelper; use Illuminate\Routing\Router; use Illuminate\Support\ServiceProvider as BaseServiceProvider; +use Spatie\MediaLibrary\MediaCollections\MediaCollection; class ServiceProvider extends BaseServiceProvider { @@ -23,7 +24,8 @@ class ServiceProvider extends BaseServiceProvider $router->patch('mediaupload/{media}', [MediaController::class, 'update'])->name('media.update'); }); - app(CollectionExtension::class)->boot(); + MediaCollection::mixin(app(CollectionExtension::class)); + // app(CollectionExtension::class)->boot(); } /**