Skip to content

Instantly share code, notes, and snippets.

@gggeek
Created January 12, 2018 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gggeek/cfff2a602496c83dd30a70c7941e362b to your computer and use it in GitHub Desktop.
Save gggeek/cfff2a602496c83dd30a70c7941e362b to your computer and use it in GitHub Desktop.
Fix for ezp-28736
<?php
namespace MyCustomer\VendorOverrideBundle\Cache;
use eZ\Bundle\EzPublishLegacyBundle\Cache\PersistenceCachePurger as BasePersistenceCachePurger;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
use eZ\Publish\SPI\Persistence\Content\Location;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
class PersistenceCachePurger extends BasePersistenceCachePurger
{
/**
* @var \Closure
*/
protected $legacyKernelClosure;
/**
* Clear all content persistence cache, or by locationIds (legacy content/cache mechanism is location based).
*
* Either way all location and urlAlias cache is cleared as well.
*
* @param int|int[]|null $locationIds Ids of location we need to purge content cache for. Purges all content cache if null
*
* @return array|int|\int[]|null
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType On invalid $id type
*/
public function content($locationIds = null)
{
if ($this->allCleared === true || $this->enabled === false) {
return;
}
if ($locationIds === null) {
$this->cache->clear('content');
goto relatedCache;
} elseif (!is_array($locationIds)) {
$locationIds = array($locationIds);
}
foreach ($locationIds as $id) {
if (!is_scalar($id)) {
throw new InvalidArgumentType('$id', 'int[]|null', $id);
}
try {
$location = $this->locationHandler->load($id);
$contentId = $location->contentId;
} catch (NotFoundException $e) {
// See if we get it by doing a cache lookup
$item = $this->cache->getItem('location', $id);
$location = $item->get();
if (!$location instanceof Location) {
// See if this a transaction problem, where the legacy kernel can see the Location but the ez5 kernel not yet...
$contentId = $this->getContentIdViaLegacyKernel($id);
if (!$contentId) {
$this->logger->notice("Unable to load the location with the id '$id' to clear its cache");
continue;
}
} else {
$contentId = $location->contentId;
}
}
$this->cache->clear('content', $contentId);
$this->cache->clear('content', 'info', $contentId);
$this->cache->clear('content', 'info', 'remoteId');
$this->cache->clear('content', 'locations', $contentId);
$this->cache->clear('user', 'role', 'assignments', 'byGroup', $contentId);
$this->cache->clear('user', 'role', 'assignments', 'byGroup', 'inherited', $contentId);
}
// clear content related cache as well
relatedCache:
$this->cache->clear('urlAlias');
$this->cache->clear('location');
return $locationIds;
}
public function setLegacyKernel($legacyKernelClosure)
{
$this->legacyKernelClosure = $legacyKernelClosure;
}
protected function getLegacyKernel()
{
$closure = $this->legacyKernelClosure;
return $closure();
}
/**
* Loads the content Id of a Location via the legacy kernel. Helps in case the db transaction which creates the node is not yet committed
* @param int $locationId
* @return int|null
*/
protected function getContentIdViaLegacyKernel($locationId)
{
return $this->getLegacyKernel()->runCallback(
function () use($locationId) {
$node = \eZContentObjectTreeNode::fetch($locationId);
if ($node) {
return $node->attribute('contentobject_id');
}
return null;
},
false
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment