src/AppBundle/EventListener/CacheListener.php line 39

Open in your IDE?
  1. <?php
  2. namespace AppBundle\EventListener;
  3. use AppBundle\Core\UserPermissionAware;
  4. use eZ\Publish\API\Repository\ContentService;
  5. use eZ\Publish\API\Repository\LocationService;
  6. use eZ\Publish\Core\MVC\Symfony\Event\ContentCacheClearEvent;
  7. use eZ\Publish\Core\MVC\Symfony\MVCEvents;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class CacheListener implements EventSubscriberInterface
  10. {
  11.     use UserPermissionAware;
  12.     /**
  13.      * @var LocationService
  14.      */
  15.     protected $locationService;
  16.     /**
  17.      * @var ContentService
  18.      */
  19.     protected $contentService;
  20.     public function __construct(
  21.         ContentService $contentService,
  22.         LocationService $locationService
  23.     ) {
  24.         $this->contentService $contentService;
  25.         $this->locationService $locationService;
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [MVCEvents::CACHE_CLEAR_CONTENT => ['onContentCacheClear'255]];
  30.     }
  31.     public function onContentCacheClear(ContentCacheClearEvent $event)
  32.     {
  33.         $contentInfo $event->getContentInfo();
  34.         $locations $this->locationService->loadLocations($contentInfo);
  35.         $reverseRelations $this->contentService->loadReverseRelations($contentInfo);
  36.         foreach ($reverseRelations as $reverseRelation) {
  37.             foreach ($this->locationService->loadLocations($reverseRelation->getSourceContentInfo()) as $relatedLocation) {
  38.                 $locations[] = $relatedLocation;
  39.             }
  40.         }
  41.         $this->addParentsLocationsToClear($event$locations);
  42.     }
  43.     private function addParentsLocationsToClear(ContentCacheClearEvent $event$locations)
  44.     {
  45.         foreach ($locations as $location) {
  46.             $parents array_slice(array_reverse(explode("/"$location->pathString)), 23);
  47.             foreach ($parents as $parent) {
  48.                 if ($parent 1) {
  49.                     $parentLocation $this->locationService->loadLocation($parent);
  50.                     $event->addLocationToClear($parentLocation);
  51.                 }
  52.             }
  53.         }
  54.     }
  55. }