Skip to content

Commit

Permalink
NEXT-28562 - Allow manipulating cache invalidation subscriber event l…
Browse files Browse the repository at this point in the history
…isteners
  • Loading branch information
OliverSkroblin committed Jul 19, 2023
1 parent 3c71319 commit 4ee545f
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 183 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Cache invalidation manipulation
issue: NEXT-28562
author: Oliver Skroblin
author_email: [email protected]
author_github: OliverSkroblin
---
# Core
* Changed `\Shopware\Core\Framework\Adapter\Cache\CacheInvalidationSubscriber` to allow manipulation of the cache invalidation event via compiler passes. The class isn't @internal anymore and can be manipulated via compiler pass by removing the event listener tags.
* Added `\Shopware\Core\Framework\DependencyInjection\CompilerPass\RemoveEventListener` class, for easier removal of event listeners via compiler pass.
105 changes: 4 additions & 101 deletions src/Core/Framework/Adapter/Cache/CacheInvalidationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
use Shopware\Core\Content\Product\Aggregate\ProductCrossSelling\ProductCrossSellingDefinition;
use Shopware\Core\Content\Product\Aggregate\ProductManufacturer\ProductManufacturerDefinition;
use Shopware\Core\Content\Product\Aggregate\ProductProperty\ProductPropertyDefinition;
use Shopware\Core\Content\Product\Aggregate\ProductSearchConfig\ProductSearchConfigDefinition;
use Shopware\Core\Content\Product\Events\ProductChangedEventInterface;
use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
use Shopware\Core\Content\Product\Events\ProductNoLongerAvailableEvent;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Content\Product\SalesChannel\CrossSelling\CachedProductCrossSellingRoute;
use Shopware\Core\Content\Product\SalesChannel\Detail\CachedProductDetailRoute;
Expand All @@ -35,7 +32,6 @@
use Shopware\Core\Content\Property\Aggregate\PropertyGroupOptionTranslation\PropertyGroupOptionTranslationDefinition;
use Shopware\Core\Content\Property\Aggregate\PropertyGroupTranslation\PropertyGroupTranslationDefinition;
use Shopware\Core\Content\Property\PropertyGroupDefinition;
use Shopware\Core\Content\Rule\Event\RuleIndexerEvent;
use Shopware\Core\Content\Seo\CachedSeoResolver;
use Shopware\Core\Content\Seo\Event\SeoUrlUpdateEvent;
use Shopware\Core\Content\Sitemap\Event\SitemapGeneratedEvent;
Expand All @@ -45,11 +41,6 @@
use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Plugin\Event\PluginPostActivateEvent;
use Shopware\Core\Framework\Plugin\Event\PluginPostDeactivateEvent;
use Shopware\Core\Framework\Plugin\Event\PluginPostInstallEvent;
use Shopware\Core\Framework\Plugin\Event\PluginPostUninstallEvent;
use Shopware\Core\Framework\Plugin\Event\PluginPostUpdateEvent;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\Country\Aggregate\CountryState\CountryStateDefinition;
use Shopware\Core\System\Country\CountryDefinition;
Expand All @@ -76,14 +67,13 @@
use Shopware\Core\System\SystemConfig\Event\SystemConfigChangedHook;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\System\Tax\TaxDefinition;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @internal - The functions inside this class are no public-api and can be changed without previous deprecation
*/
#[Package('core')]
class CacheInvalidationSubscriber implements EventSubscriberInterface
class CacheInvalidationSubscriber
{
/**
* @internal
*/
public function __construct(
private readonly CacheInvalidator $cacheInvalidator,
private readonly Connection $connection,
Expand All @@ -92,93 +82,6 @@ public function __construct(
) {
}

/**
* @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
*/
public static function getSubscribedEvents(): array
{
return [
CategoryIndexerEvent::class => [
['invalidateCategoryRouteByCategoryIds', 2000],
['invalidateListingRouteByCategoryIds', 2001],
],
LandingPageIndexerEvent::class => [
['invalidateIndexedLandingPages', 2000],
],
ProductIndexerEvent::class => [
['invalidateSearch', 2000],
['invalidateListings', 2001],
['invalidateProductIds', 2002],
['invalidateDetailRoute', 2004],
['invalidateStreamsAfterIndexing', 2005],
['invalidateReviewRoute', 2006],
],
ProductNoLongerAvailableEvent::class => [
['invalidateSearch', 2000],
['invalidateListings', 2001],
['invalidateProductIds', 2002],
['invalidateDetailRoute', 2004],
['invalidateStreamsAfterIndexing', 2005],
['invalidateReviewRoute', 2006],
],
EntityWrittenContainerEvent::class => [
['invalidateCmsPageIds', 2001],
['invalidateCurrencyRoute', 2002],
['invalidateLanguageRoute', 2003],
['invalidateNavigationRoute', 2004],
['invalidatePaymentMethodRoute', 2005],
['invalidateProductAssignment', 2006],
['invalidateManufacturerFilters', 2007],
['invalidatePropertyFilters', 2008],
['invalidateCrossSellingRoute', 2009],
['invalidateContext', 2010],
['invalidateShippingMethodRoute', 2011],
['invalidateSnippets', 2012],
['invalidateStreamsBeforeIndexing', 2013],
['invalidateStreamIds', 2014],
['invalidateCountryRoute', 2015],
['invalidateSalutationRoute', 2016],
['invalidateInitialStateIdLoader', 2017],
['invalidateCountryStateRoute', 2018],
],
SeoUrlUpdateEvent::class => [
['invalidateSeoUrls', 2000],
],
RuleIndexerEvent::class => [
['invalidateRules', 2000],
],
PluginPostInstallEvent::class => [
['invalidateRules', 2000],
['invalidateConfig', 2001],
],
PluginPostActivateEvent::class => [
['invalidateRules', 2000],
['invalidateConfig', 2001],
],
PluginPostUpdateEvent::class => [
['invalidateRules', 2000],
['invalidateConfig', 2001],
],
PluginPostDeactivateEvent::class => [
['invalidateRules', 2000],
['invalidateConfig', 2001],
],
PluginPostUninstallEvent::class => [
['invalidateRules', 2000],
['invalidateConfig', 2001],
],
SystemConfigChangedHook::class => [
['invalidateConfigKey', 2000],
],
SitemapGeneratedEvent::class => [
['invalidateSitemap', 2000],
],
ProductSearchConfigDefinition::ENTITY_NAME . '.written' => [
['invalidateSearch', 2002],
],
];
}

public function invalidateInitialStateIdLoader(EntityWrittenContainerEvent $event): void
{
if (!$event->getPrimaryKeys(StateMachineDefinition::ENTITY_NAME)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\DependencyInjection\CompilerPass;

use Shopware\Core\Framework\Log\Package;
use Symfony\Component\DependencyInjection\ContainerBuilder;

#[Package('core')]
abstract class RemoveEventListener
{
/**
* @param array<string[]> $remove
*/
public static function remove(ContainerBuilder $builder, string $serviceId, array $remove): void
{
if (!$builder->hasDefinition($serviceId)) {
return;
}

$definition = $builder->getDefinition($serviceId);

$listeners = $definition->getTag('kernel.event_listener');

$definition->clearTag('kernel.event_listener');

$map = \array_map(function (array $item) {
return \implode('::', $item);
}, $remove);

foreach ($listeners as $listener) {
$key = $listener['event'] . '::' . $listener['method'];

if (!\in_array($key, $map, true)) {
$definition->addTag('kernel.event_listener', $listener);
}
}
}
}
Loading

0 comments on commit 4ee545f

Please sign in to comment.