|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Laravel\Eloquent\Listener; |
| 15 | + |
| 16 | +use ApiPlatform\HttpCache\PurgerInterface; |
| 17 | +use ApiPlatform\Metadata\IriConverterInterface; |
| 18 | +use ApiPlatform\Metadata\ResourceClassResolverInterface; |
| 19 | +use Illuminate\Database\Eloquent\Model; |
| 20 | +use Illuminate\Database\Eloquent\Relations\Relation; |
| 21 | +use Illuminate\Support\Collection; |
| 22 | + |
| 23 | +final class PurgeHttpCacheListener |
| 24 | +{ |
| 25 | + private array $tags = []; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private readonly PurgerInterface $purger, |
| 29 | + private readonly IriConverterInterface $iriConverter, |
| 30 | + private readonly ResourceClassResolverInterface $resourceClassResolver, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Handle the "saved" event for any model. |
| 36 | + */ |
| 37 | + public function handleModelSaved(Model $model): void |
| 38 | + { |
| 39 | + if (!$this->resourceClassResolver->isResourceClass($model::class)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + $this->gatherResourceAndItemTags($model, true); |
| 44 | + $this->gatherDirtyRelationTags($model); |
| 45 | + $this->gatherRelationTags($model); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Handle the "deleted" event for any model. |
| 50 | + */ |
| 51 | + public function handleModelDeleted(Model $model): void |
| 52 | + { |
| 53 | + if (!$this->resourceClassResolver->isResourceClass($model::class)) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + $this->gatherResourceAndItemTags($model, true); |
| 58 | + $this->gatherRelationTags($model); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Purges all collected tags at the end of the request. |
| 63 | + */ |
| 64 | + public function postFlush(): void |
| 65 | + { |
| 66 | + if (empty($this->tags)) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + $this->purger->purge(array_values(array_unique($this->tags))); |
| 71 | + $this->tags = []; |
| 72 | + } |
| 73 | + |
| 74 | + private function gatherResourceAndItemTags(Model $model, bool $purgeItem): void |
| 75 | + { |
| 76 | + try { |
| 77 | + $collectionIri = $this->iriConverter->getCollectionIriFromResourceClass($model::class); |
| 78 | + $this->tags[] = $collectionIri; |
| 79 | + |
| 80 | + if ($purgeItem) { |
| 81 | + $this->addTagForItem($model); |
| 82 | + } |
| 83 | + } catch (\Exception) { |
| 84 | + // Ignore if IRI cannot be generated |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private function gatherDirtyRelationTags(Model $model): void |
| 89 | + { |
| 90 | + foreach ($model->getDirty() as $key => $value) { |
| 91 | + // Check if the dirty attribute is a foreign key |
| 92 | + if (!str_ends_with($key, '_id')) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + $originalId = $model->getOriginal($key); |
| 97 | + $relationName = str_replace('_id', '', $key); |
| 98 | + |
| 99 | + if ($originalId && method_exists($model, $relationName)) { |
| 100 | + $relation = $model->{$relationName}(); |
| 101 | + if ($relation instanceof Relation) { |
| 102 | + $relatedModelClass = \get_class($relation->getRelated()); |
| 103 | + if ($oldRelated = $relatedModelClass::find($originalId)) { |
| 104 | + $this->addTagForItem($oldRelated); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private function gatherRelationTags(Model $model): void |
| 112 | + { |
| 113 | + $reflection = new \ReflectionClass($model); |
| 114 | + $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); |
| 115 | + |
| 116 | + foreach ($methods as $method) { |
| 117 | + if ($method->getNumberOfParameters() > 0 || $method->isStatic()) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + try { |
| 122 | + $returnValue = $method->invoke($model); |
| 123 | + if ($returnValue instanceof Relation) { |
| 124 | + $related = $model->getRelationValue($method->getName()); |
| 125 | + $this->addTagsFor($related); |
| 126 | + } |
| 127 | + } catch (\Throwable) { |
| 128 | + // Not a relation method |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private function addTagsFor(mixed $value): void |
| 134 | + { |
| 135 | + if (null === $value) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + if ($value instanceof Model) { |
| 140 | + $this->addTagForItem($value); |
| 141 | + |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + if ($value instanceof Collection) { |
| 146 | + foreach ($value as $item) { |
| 147 | + $this->addTagForItem($item); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private function addTagForItem(mixed $value): void |
| 153 | + { |
| 154 | + if (!$value instanceof Model || !$this->resourceClassResolver->isResourceClass($value::class)) { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + try { |
| 159 | + $iri = $this->iriConverter->getIriFromResource($value); |
| 160 | + $this->tags[] = $iri; |
| 161 | + } catch (\Exception) { |
| 162 | + // Ignore if IRI cannot be generated |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments