Skip to content

Commit

Permalink
Generate slug events
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Sep 10, 2024
1 parent 681f92e commit 356c657
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/Behaviours/CachedOnCDN.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down Expand Up @@ -81,4 +87,9 @@ public function edgeFlushKeyWasAlreadyAdded(string $key): bool

return $added;
}

public function setOriginalAttributes(array $attributes): void
{
$this->original = $attributes;
}
}
32 changes: 27 additions & 5 deletions src/Listeners/EloquentObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +20,12 @@ class EloquentObserver

public function __construct()
{
Event::listen('eloquent.builder.*', function ($event, $model) {
$model = $this->createModelInstance(json_decode($model[0], true));

Check failure on line 24 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Parameter #1 $model of method A17\EdgeFlush\Listeners\EloquentObserver::createModelInstance() expects array, mixed given.

Check failure on line 24 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Parameter #1 $model of method A17\EdgeFlush\Listeners\EloquentObserver::createModelInstance() expects array, mixed given.

Check failure on line 24 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Parameter #1 $model of method A17\EdgeFlush\Listeners\EloquentObserver::createModelInstance() expects array, mixed given.

$this->invalidate($model, 'builder.updating');
});

$this->boot();
}

Expand All @@ -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');
Expand Down Expand Up @@ -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);
}
Expand All @@ -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']);

Check failure on line 101 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Call to an undefined method object::setRawAttributes().

Check failure on line 101 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Call to an undefined method object::setRawAttributes().

Check failure on line 101 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Call to an undefined method object::setRawAttributes().

$updates = collect($model['updates'])->mapWithKeys(function ($value, $key) {

Check failure on line 103 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Unable to resolve the template type TKey in call to function collect

Check failure on line 103 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Unable to resolve the template type TValue in call to function collect

Check failure on line 103 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Unable to resolve the template type TKey in call to function collect

Check failure on line 103 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Unable to resolve the template type TValue in call to function collect

Check failure on line 103 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Unable to resolve the template type TKey in call to function collect

Check failure on line 103 in src/Listeners/EloquentObserver.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Unable to resolve the template type TValue in call to function collect
return [$key => 'just-to-trigger-dirty-attributes'];
})->toArray();

$newModel->setOriginalAttributes(array_merge($model['attributes'], $updates));

return $newModel;
}
}
52 changes: 52 additions & 0 deletions src/Models/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace A17\EdgeFlush\Models;

use Illuminate\Support\Facades\Event;
use Illuminate\Queue\SerializesModels;
use App\Twill\Capsules\People\Models\Person;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;

class Builder extends EloquentBuilder
{
use SerializesModels;

/**
* Update records in the database.
*
* @param array $values
* @return int
*/
public function update(array $values)
{
if (count($this->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);
}
}

0 comments on commit 356c657

Please sign in to comment.