diff --git a/src/Behaviours/CachedOnCDN.php b/src/Behaviours/CachedOnCDN.php index 70fc5ef..def6f7b 100644 --- a/src/Behaviours/CachedOnCDN.php +++ b/src/Behaviours/CachedOnCDN.php @@ -3,6 +3,7 @@ namespace A17\EdgeFlush\Behaviours; use A17\EdgeFlush\EdgeFlush; +use A17\EdgeFlush\Models\Builder; use A17\EdgeFlush\Services\Entity; use A17\EdgeFlush\Support\Helpers; use Illuminate\Database\Eloquent\Model; @@ -13,6 +14,11 @@ trait CachedOnCDN { protected array $edgeFlushCachedAttributes = []; + public function newEloquentBuilder($query) + { + return new \A17\EdgeFlush\Models\Builder($query); + } + public function invalidateCDNCache(Entity|Model $object): void { if (!$this->edgeFlushIsEnabled() || !$this->invalidationsAreEnabled()) { @@ -81,4 +87,9 @@ public function edgeFlushKeyWasAlreadyAdded(string $key): bool return $added; } + + public function setOriginalAttributes(array $attributes): void + { + $this->original = $attributes; + } } diff --git a/src/Listeners/EloquentObserver.php b/src/Listeners/EloquentObserver.php index 1aa4d0c..11174d9 100644 --- a/src/Listeners/EloquentObserver.php +++ b/src/Listeners/EloquentObserver.php @@ -5,6 +5,7 @@ use A17\EdgeFlush\Services\Entity; use A17\EdgeFlush\Support\Helpers; use A17\EdgeFlush\Behaviours\MakeTag; +use Illuminate\Support\Facades\Event; use Illuminate\Database\Eloquent\Model; use Illuminate\Queue\InteractsWithQueue; use A17\EdgeFlush\Behaviours\CachedOnCDN; @@ -19,6 +20,12 @@ class EloquentObserver public function __construct() { + Event::listen('eloquent.builder.*', function ($event, $model) { + $model = $this->createModelInstance(json_decode($model[0], true)); + + $this->invalidate($model, 'builder.updating'); + }); + $this->boot(); } @@ -32,6 +39,11 @@ public function updated(Model $model): void $this->invalidate($model, 'updated'); } + public function updating(Model $model): void + { + $this->invalidate($model, 'updating'); + } + public function deleted(Model $model): void { $this->invalidate($model, 'deleted'); @@ -72,11 +84,6 @@ public function invalidate(Model $model, string $event, array $relation = []): v $entity->setRelation($relation); - Helpers::debug( - "MODEL EVENT: {$event} on model ".$entity->modelName. - (($relation['name'] ?? null) ? " on relation {$relation['name']}" : '') - ); - if ($entity->mustInvalidate()) { $this->invalidateCDNCache($entity); } @@ -86,4 +93,19 @@ public function boot(): void { $this->dispatchedEvents = app('a17.edgeflush.dispatchedEvents'); } + + public function createModelInstance(array $model): Model + { + $newModel = new $model['model'](); + + $newModel->setRawAttributes($model['attributes']); + + $updates = collect($model['updates'])->mapWithKeys(function ($value, $key) { + return [$key => 'just-to-trigger-dirty-attributes']; + })->toArray(); + + $newModel->setOriginalAttributes(array_merge($model['attributes'], $updates)); + + return $newModel; + } } diff --git a/src/Models/Builder.php b/src/Models/Builder.php new file mode 100755 index 0000000..98434a7 --- /dev/null +++ b/src/Models/Builder.php @@ -0,0 +1,52 @@ +model->getAttributes()) > 0) + { + return; + } + + $eventData = json_encode(['model' => get_class($this->model), 'attributes' => $this->compileAttributesFromQuery($values), 'updates' => $values]); + + Event::dispatch('eloquent.builder.updating: ' . get_class($this->model), $eventData); + + $value = $this->toBase()->update($this->addUpdatedAtColumn($values)); + + Event::dispatch('eloquent.builder.updated: ' . get_class($this->model), $eventData); + } + + public function compileAttributesFromQuery(array $values): array + { + $data = []; + + foreach($this->query->wheres as $where) + { + if ($where['type'] == 'Basic') + { + if ($where['column'] !== 'id' || $where['value'] != 0) { + $data[$where['column']] = $where['value']; + } + } + } + + return array_merge($data, $values); + } +}