1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[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 Silverback\ApiComponentsBundle\HttpCache; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Exception\InvalidArgumentException as LegacyInvalidArgumentException; |
17
|
|
|
use ApiPlatform\Exception\OperationNotFoundException as LegacyOperationNotFoundException; |
18
|
|
|
use ApiPlatform\Exception\RuntimeException as LegacyRuntimeException; |
19
|
|
|
use ApiPlatform\HttpCache\PurgerInterface; |
20
|
|
|
use ApiPlatform\Metadata\Exception\InvalidArgumentException; |
21
|
|
|
use ApiPlatform\Metadata\Exception\OperationNotFoundException; |
22
|
|
|
use ApiPlatform\Metadata\Exception\RuntimeException; |
23
|
|
|
use ApiPlatform\Metadata\GetCollection; |
24
|
|
|
use ApiPlatform\Metadata\IriConverterInterface; |
25
|
|
|
use ApiPlatform\Metadata\ResourceClassResolverInterface; |
26
|
|
|
use ApiPlatform\Metadata\UrlGeneratorInterface; |
27
|
|
|
use Doctrine\ORM\PersistentCollection; |
28
|
|
|
|
29
|
|
|
class HttpCachePurger implements ResourceChangedPropagatorInterface |
30
|
|
|
{ |
31
|
|
|
private array $tags; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
private readonly IriConverterInterface $iriConverter, |
35
|
|
|
private readonly ResourceClassResolverInterface $resourceClassResolver, |
36
|
|
|
private readonly PurgerInterface $httpCachePurger, |
37
|
|
|
) { |
38
|
|
|
$this->reset(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function add(object $item, ?string $type = null): void |
42
|
|
|
{ |
43
|
|
|
if (!is_iterable($item)) { |
44
|
|
|
$this->collectResource($item); |
45
|
|
|
|
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($item instanceof PersistentCollection) { |
50
|
|
|
$item = clone $item; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($item as $i) { |
54
|
|
|
$this->collectResource($i); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function collectResource($entity): void |
59
|
|
|
{ |
60
|
|
|
if (null === $entity) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
$resourceClass = $this->resourceClassResolver->getResourceClass($entity); |
66
|
|
|
|
67
|
|
|
// collect cache of any collections being fetched |
68
|
|
|
$resourceIri = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, (new GetCollection())->withClass($resourceClass)); |
69
|
|
|
$this->collectIri($resourceIri); |
70
|
|
|
|
71
|
|
|
// clear cache of anything containing this item |
72
|
|
|
$this->collectItem($entity); |
73
|
|
|
} catch (OperationNotFoundException|InvalidArgumentException|LegacyOperationNotFoundException|LegacyInvalidArgumentException) { |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function collectItem($item): void |
78
|
|
|
{ |
79
|
|
|
try { |
80
|
|
|
$iri = $this->iriConverter->getIriFromResource($item); |
81
|
|
|
$this->collectIri($iri); |
82
|
|
|
} catch (InvalidArgumentException|RuntimeException|LegacyInvalidArgumentException|LegacyRuntimeException) { |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function collectIri($iri): void |
87
|
|
|
{ |
88
|
|
|
if (!\in_array($iri, $this->tags, true)) { |
89
|
|
|
$this->tags[$iri] = $iri; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function propagate(): void |
94
|
|
|
{ |
95
|
|
|
if (empty($this->tags)) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->httpCachePurger->purge(array_values($this->tags)); |
100
|
|
|
$this->reset(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function reset(): void |
104
|
|
|
{ |
105
|
|
|
$this->tags = []; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|