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\Core\HttpCache; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
17
|
|
|
use ApiPlatform\Core\JsonLd\Action\ContextAction; |
18
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
19
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
20
|
|
|
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Purges Varnish on cache clearing only for doc tags. |
24
|
|
|
* |
25
|
|
|
* @author Florent Mata <[email protected]> |
26
|
|
|
* |
27
|
|
|
* @experimental |
28
|
|
|
*/ |
29
|
|
|
final class VarnishClearer implements CacheClearerInterface |
30
|
|
|
{ |
31
|
|
|
private $resourceNameCollectionFactory; |
32
|
|
|
private $resourceMetadataFactory; |
33
|
|
|
private $iriConverter; |
34
|
|
|
private $purger; |
35
|
|
|
|
36
|
|
|
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, PurgerInterface $purger) |
37
|
|
|
{ |
38
|
|
|
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory; |
39
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
40
|
|
|
$this->iriConverter = $iriConverter; |
41
|
|
|
$this->purger = $purger; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function clear($cacheDir) |
45
|
|
|
{ |
46
|
|
|
$iri = $this->iriConverter->getApiDocIri(); |
47
|
|
|
$iris = [$iri => $iri]; |
48
|
|
|
|
49
|
|
|
foreach (array_keys(['Entrypoint' => true] + ContextAction::RESERVED_SHORT_NAMES) as $shortName) { |
50
|
|
|
$iri = $this->iriConverter->getContextIriFromShortName($shortName); |
51
|
|
|
$iris[$iri] = $iri; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
55
|
|
|
$iri = $this->iriConverter->getContextIriFromShortName($this->resourceMetadataFactory->create($resourceClass)->getShortName()); |
56
|
|
|
$iris[$iri] = $iri; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->purger->purge($iris); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|