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\Api\IriConverterInterface; |
17
|
|
|
use ApiPlatform\Api\ResourceClassResolverInterface; |
18
|
|
|
use ApiPlatform\Api\UrlGeneratorInterface; |
19
|
|
|
use ApiPlatform\Exception\InvalidArgumentException; |
20
|
|
|
use ApiPlatform\Exception\OperationNotFoundException; |
21
|
|
|
use ApiPlatform\Exception\RuntimeException; |
22
|
|
|
use ApiPlatform\HttpCache\PurgerInterface; |
23
|
|
|
use ApiPlatform\Metadata\GetCollection; |
24
|
|
|
use Doctrine\ORM\PersistentCollection; |
25
|
|
|
|
26
|
|
|
class HttpCachePurger implements ResourceChangedPropagatorInterface |
27
|
|
|
{ |
28
|
|
|
private array $tags = []; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
private readonly IriConverterInterface $iriConverter, |
32
|
|
|
private readonly ResourceClassResolverInterface $resourceClassResolver, |
33
|
|
|
private readonly PurgerInterface $httpCachePurger, |
34
|
|
|
) { |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function collectResource($entity): void |
38
|
|
|
{ |
39
|
|
|
if (null === $entity) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
$resourceClass = $this->resourceClassResolver->getResourceClass($entity); |
45
|
|
|
$resourceIri = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, (new GetCollection())->withClass($resourceClass)); |
46
|
|
|
$this->collectIri([$resourceIri]); |
47
|
|
|
} catch (OperationNotFoundException|InvalidArgumentException $e) { |
|
|
|
|
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function collectItems($value): void |
52
|
|
|
{ |
53
|
|
|
if (!$value) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!is_iterable($value)) { |
58
|
|
|
$this->collectItem($value); |
59
|
|
|
|
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($value instanceof PersistentCollection) { |
64
|
|
|
$value = clone $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($value as $v) { |
68
|
|
|
$this->collectItem($v); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function collectItem($value): void |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
|
|
$iri = $this->iriConverter->getIriFromResource($value); |
76
|
|
|
$this->collectIri([$iri]); |
77
|
|
|
} catch (InvalidArgumentException $e) { |
|
|
|
|
78
|
|
|
} catch (RuntimeException $e) { |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function collectIri(array $iris): void |
83
|
|
|
{ |
84
|
|
|
foreach ($iris as $iri) { |
85
|
|
|
if (!\in_array($iri, $this->tags, true)) { |
86
|
|
|
$this->tags[$iri] = $iri; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function propagate(): void |
92
|
|
|
{ |
93
|
|
|
if (empty($this->tags)) { |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->httpCachePurger->purge(array_values($this->tags)); |
98
|
|
|
$this->tags = []; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|