Move CollectionExtension

This commit is contained in:
philipp lang 2024-01-02 23:47:48 +01:00
parent 820a725517
commit 194f1f9c14
2 changed files with 83 additions and 88 deletions

View File

@ -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,
]);
};
}
}

View File

@ -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();
}
/**