forked from ezsystems/ezpublish-kernel
/
RelatedLocationsListener.php
75 lines (65 loc) · 2.7 KB
/
RelatedLocationsListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* This file is part of the eZ Publish Kernel package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\MVC\Symfony\Cache\Http\EventListener;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\Core\MVC\Symfony\Event\ContentCacheClearEvent;
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Adds locations from related content to the Http cache clear list, for given content.
* Both relation and reverse relation are taken into account.
*
* @deprecated since 6.8. Smart cache is replaced by http-cache multi-tagging from the platform-http-cache package.
*/
class RelatedLocationsListener implements EventSubscriberInterface
{
/**
* @var \eZ\Publish\Core\Repository\Repository
*/
private $repository;
/**
* @var \eZ\Publish\API\Repository\ContentService
*/
private $contentService;
/**
* @var \eZ\Publish\API\Repository\LocationService
*/
private $locationService;
public function __construct(Repository $repository)
{
$this->repository = $repository;
$this->contentService = $repository->getContentService();
$this->locationService = $repository->getLocationService();
}
public static function getSubscribedEvents()
{
return [MVCEvents::CACHE_CLEAR_CONTENT => ['onContentCacheClear', 100]];
}
public function onContentCacheClear(ContentCacheClearEvent $event)
{
$contentInfo = $event->getContentInfo();
$versionInfo = $this->contentService->loadVersionInfo($contentInfo);
foreach ($this->contentService->loadRelations($versionInfo) as $relation) {
foreach ($this->locationService->loadLocations($relation->getDestinationContentInfo()) as $relatedLocation) {
$event->addLocationToClear($relatedLocation);
}
}
// Using sudo since loading reverse relations is conditioned to content/reverserelatedlist permission and we don't need this check here.
/** @var \eZ\Publish\API\Repository\Values\Content\Relation[] $reverseRelations */
$reverseRelations = $this->repository->sudo(
function () use ($contentInfo) {
return $this->contentService->loadReverseRelations($contentInfo);
}
);
foreach ($reverseRelations as $reverseRelation) {
foreach ($this->locationService->loadLocations($reverseRelation->getSourceContentInfo()) as $relatedLocation) {
$event->addLocationToClear($relatedLocation);
}
}
}
}