eZ\Publish\Core\MVC\Symfony\Cache\Http\InstantCachePurger::purgeForContent PHP Method

purgeForContent() public method

public purgeForContent ( $contentId, $locationIds = [] )
    public function purgeForContent($contentId, $locationIds = [])
    {
        $contentInfo = $this->contentService->loadContentInfo($contentId);
        // Can only gather relevant locations using ContentCacheClearEvent on published content
        if ($contentInfo->published) {
            $event = new ContentCacheClearEvent($contentInfo);
            $this->eventDispatcher->dispatch(MVCEvents::CACHE_CLEAR_CONTENT, $event);
            foreach ($event->getLocationsToClear() as $location) {
                $locationIds[] = $location->id;
            }
        }
        $this->purgeClient->purge(array_unique($locationIds));
    }

Usage Example

 public function testPurgeForContent()
 {
     $contentId = 123;
     $contentInfo = new ContentInfo(['id' => $contentId]);
     // Assume listeners have added locations.
     // Adding duplicates on purpose.
     $locationIds = [123, 456, 789, 234, 567];
     $this->contentService->expects($this->once())->method('loadContentInfo')->with($contentId)->will($this->returnValue($contentInfo));
     $eventDispatcher = new EventDispatcher();
     $eventDispatcher->addListener(MVCEvents::CACHE_CLEAR_CONTENT, function (ContentCacheClearEvent $event) use($locationIds) {
         foreach ($locationIds as $id) {
             $event->addLocationToClear(new Location(['id' => $id]));
         }
         // Adding a few duplicates on purpose.
         $event->addLocationToClear(new Location(['id' => 123]));
         $event->addLocationToClear(new Location(['id' => 567]));
     });
     $this->purgeClient->expects($this->once())->method('purge')->with($locationIds);
     $purger = new InstantCachePurger($this->purgeClient, $this->contentService, $eventDispatcher);
     $purger->purgeForContent($contentId);
 }