add after event

This commit is contained in:
Philipp Lang 2023-03-13 10:34:52 +01:00
parent f2218d83ad
commit fb15678d4e
7 changed files with 46 additions and 2 deletions

View File

@ -24,6 +24,11 @@ class CollectionExtension
return fn ($callback) => $this->registerCustomCallback('destroyed', $callback); return fn ($callback) => $this->registerCustomCallback('destroyed', $callback);
} }
public function after()
{
return fn ($callback) => $this->registerCustomCallback('after', $callback);
}
public function withDefaultProperties() public function withDefaultProperties()
{ {
return fn ($callback) => $this->registerCustomCallback('withDefaultProperties', $callback); return fn ($callback) => $this->registerCustomCallback('withDefaultProperties', $callback);
@ -72,6 +77,7 @@ class CollectionExtension
$this->customCallbacks = collect([ $this->customCallbacks = collect([
'forceFileName' => fn ($model, $name) => $name, 'forceFileName' => fn ($model, $name) => $name,
'stored' => fn ($event) => true, 'stored' => fn ($event) => true,
'after' => fn ($event) => true,
'destroyed' => fn ($event) => true, 'destroyed' => fn ($event) => true,
'storing' => fn ($adder, $name) => $adder, 'storing' => fn ($adder, $name) => $adder,
'withDefaultProperties' => fn ($path) => [], 'withDefaultProperties' => fn ($path) => [],

View File

@ -58,6 +58,8 @@ class MediaController
); );
}); });
$collection->runCallback('after', $model->fresh());
return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias); return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias);
} }
@ -71,6 +73,7 @@ class MediaController
$validated = $request->validate($rules); $validated = $request->validate($rules);
$media->update(['custom_properties' => data_get($validated, 'properties', [])]); $media->update(['custom_properties' => data_get($validated, 'properties', [])]);
$media->model->getMediaCollection($media->collection_name)->runCallback('after', $media->model->fresh());
return MediaData::from($media); return MediaData::from($media);
} }
@ -101,6 +104,7 @@ class MediaController
$collection = $model->getMediaCollection($media->collection_name); $collection = $model->getMediaCollection($media->collection_name);
$media->delete(); $media->delete();
$collection->runCallback('destroyed', $media->model->fresh()); $collection->runCallback('destroyed', $media->model->fresh());
$collection->runCallback('after', $media->model->fresh());
return response()->json([]); return response()->json([]);
} }

View File

@ -0,0 +1,22 @@
<?php
namespace Zoomyboy\MedialibraryHelper\Tests\Events;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Spatie\MediaLibrary\HasMedia;
class MediaChange
{
use Dispatchable;
use SerializesModels;
public function __construct(public HasMedia $model)
{
}
public function broadcastOn()
{
return [];
}
}

View File

@ -3,6 +3,7 @@
namespace Zoomyboy\MedialibraryHelper\Tests\Feature; namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaDestroyed; use Zoomyboy\MedialibraryHelper\Tests\Events\MediaDestroyed;
test('it deletes multiple media', function () { test('it deletes multiple media', function () {
@ -47,4 +48,5 @@ test('it fires event', function () {
$this->deleteJson("/mediaupload/{$media->id}")->assertStatus(200); $this->deleteJson("/mediaupload/{$media->id}")->assertStatus(200);
Event::assertDispatched(MediaDestroyed::class, fn ($event) => $event->model->is($post)); Event::assertDispatched(MediaDestroyed::class, fn ($event) => $event->model->is($post));
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
}); });

View File

@ -2,7 +2,11 @@
namespace Zoomyboy\MedialibraryHelper\Tests\Feature; namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
use Illuminate\Support\Facades\Event;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
test('it updates a single files properties', function () { test('it updates a single files properties', function () {
Event::fake();
$this->auth()->registerModel(); $this->auth()->registerModel();
$post = $this->newPost(); $post = $this->newPost();
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleProperties'); $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleProperties');
@ -20,6 +24,7 @@ test('it updates a single files properties', function () {
$this->assertEquals(null, $media->fresh()->getCustomProperty('missing')); $this->assertEquals(null, $media->fresh()->getCustomProperty('missing'));
$response->assertJsonPath('properties.test', 'new'); $response->assertJsonPath('properties.test', 'new');
$response->assertJsonMissingPath('properties.missing'); $response->assertJsonMissingPath('properties.missing');
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
}); });
test('it validates a single files properties', function () { test('it validates a single files properties', function () {

View File

@ -4,6 +4,7 @@ namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored; use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
test('it uploads a single file to a single file collection', function () { test('it uploads a single file to a single file collection', function () {
@ -140,6 +141,7 @@ test('it throws event when file has been uploaded', function () {
$response->assertStatus(201); $response->assertStatus(201);
Event::assertDispatched(MediaStored::class, fn ($event) => $event->media->id === $response->json('id')); Event::assertDispatched(MediaStored::class, fn ($event) => $event->media->id === $response->json('id'));
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
}); });
test('it throws event when multiple files uploaded', function () { test('it throws event when multiple files uploaded', function () {

View File

@ -8,6 +8,7 @@ use Illuminate\Support\Str;
use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia; use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media; use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaDestroyed; use Zoomyboy\MedialibraryHelper\Tests\Events\MediaDestroyed;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored; use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
@ -48,7 +49,8 @@ class Post extends Model implements HasMedia
$this->addMediaCollection('singleWithEvent')->singleFile()->stored(function (Media $media) { $this->addMediaCollection('singleWithEvent')->singleFile()->stored(function (Media $media) {
Event::dispatch(new MediaStored($media)); Event::dispatch(new MediaStored($media));
}) })
->destroyed(fn ($model) => Event::dispatch(new MediaDestroyed($model))); ->destroyed(fn ($model) => Event::dispatch(new MediaDestroyed($model)))
->after(fn ($model) => Event::dispatch(new MediaChange($model)));
$this->addMediaCollection('multipleFilesWithEvent')->stored(function (Media $media) { $this->addMediaCollection('multipleFilesWithEvent')->stored(function (Media $media) {
Event::dispatch(new MediaStored($media)); Event::dispatch(new MediaStored($media));
@ -58,6 +60,7 @@ class Post extends Model implements HasMedia
'test' => Str::camel($path), 'test' => Str::camel($path),
])->withPropertyValidation(fn ($path) => [ ])->withPropertyValidation(fn ($path) => [
'test' => 'string|max:10', 'test' => 'string|max:10',
]); ])
->after(fn ($model) => Event::dispatch(new MediaChange($model)));
} }
} }