Move CollectionExtension
This commit is contained in:
parent
820a725517
commit
194f1f9c14
|
@ -2,96 +2,89 @@
|
||||||
|
|
||||||
namespace Zoomyboy\MedialibraryHelper;
|
namespace Zoomyboy\MedialibraryHelper;
|
||||||
|
|
||||||
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
|
||||||
|
|
||||||
class CollectionExtension
|
class CollectionExtension
|
||||||
{
|
{
|
||||||
public function boot(): void
|
public function forceFileName()
|
||||||
{
|
{
|
||||||
MediaCollection::mixin(new class() {
|
return fn ($callback) => $this->registerCustomCallback('forceFileName', $callback);
|
||||||
public function forceFileName()
|
}
|
||||||
{
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
$this->customCallbacks = collect([
|
||||||
public function storing()
|
'forceFileName' => fn ($model, $name) => $name,
|
||||||
{
|
'maxWidth' => fn ($size) => null,
|
||||||
return fn ($callback) => $this->registerCustomCallback('storing', $callback);
|
'stored' => fn ($event) => true,
|
||||||
}
|
'after' => fn ($event) => true,
|
||||||
|
'destroyed' => fn ($event) => true,
|
||||||
public function destroyed()
|
'storing' => fn ($adder, $name) => $adder,
|
||||||
{
|
'withDefaultProperties' => fn ($path) => [],
|
||||||
return fn ($callback) => $this->registerCustomCallback('destroyed', $callback);
|
'withPropertyValidation' => fn ($path) => [],
|
||||||
}
|
'withFallback' => fn ($parent) => null,
|
||||||
|
]);
|
||||||
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,
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ namespace Zoomyboy\MedialibraryHelper;
|
||||||
|
|
||||||
use Illuminate\Routing\Router;
|
use Illuminate\Routing\Router;
|
||||||
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
|
||||||
|
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
||||||
|
|
||||||
class ServiceProvider extends BaseServiceProvider
|
class ServiceProvider extends BaseServiceProvider
|
||||||
{
|
{
|
||||||
|
@ -23,7 +24,8 @@ class ServiceProvider extends BaseServiceProvider
|
||||||
$router->patch('mediaupload/{media}', [MediaController::class, 'update'])->name('media.update');
|
$router->patch('mediaupload/{media}', [MediaController::class, 'update'])->name('media.update');
|
||||||
});
|
});
|
||||||
|
|
||||||
app(CollectionExtension::class)->boot();
|
MediaCollection::mixin(app(CollectionExtension::class));
|
||||||
|
// app(CollectionExtension::class)->boot();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue