From e9b8e59a48c924c8161be69583c11923a8830975 Mon Sep 17 00:00:00 2001 From: gechetspr Date: Tue, 29 Dec 2020 10:14:04 +0200 Subject: [PATCH] TE-8140 moved cache key generation to the separate class --- .../ShopApplicationDependencyProvider.php | 14 ++--- .../ShopApplicationFactory.php | 18 ++++-- .../CacheKeyGeneratorInterface.php | 19 ++++++ .../StrategyCacheKeyGenerator.php | 63 +++++++++++++++++++ .../Twig/Widget/WidgetFactory.php | 53 +++------------- 5 files changed, 110 insertions(+), 57 deletions(-) create mode 100644 src/SprykerShop/Yves/ShopApplication/Twig/Widget/CacheKeyGenerator/CacheKeyGeneratorInterface.php create mode 100644 src/SprykerShop/Yves/ShopApplication/Twig/Widget/CacheKeyGenerator/StrategyCacheKeyGenerator.php diff --git a/src/SprykerShop/Yves/ShopApplication/ShopApplicationDependencyProvider.php b/src/SprykerShop/Yves/ShopApplication/ShopApplicationDependencyProvider.php index 5dbb5c9..7ab2cc4 100644 --- a/src/SprykerShop/Yves/ShopApplication/ShopApplicationDependencyProvider.php +++ b/src/SprykerShop/Yves/ShopApplication/ShopApplicationDependencyProvider.php @@ -33,7 +33,7 @@ class ShopApplicationDependencyProvider extends AbstractBundleDependencyProvider public const STORE = 'STORE'; public const PLUGINS_FILTER_CONTROLLER_EVENT_SUBSCRIBER = 'PLUGINS_FILTER_CONTROLLER_EVENT_SUBSCRIBER'; public const PLUGINS_APPLICATION = 'PLUGINS_APPLICATION'; - public const PLUGINS_WIDGET_CACHE_KEY_GENERATOR = 'PLUGINS_WIDGET_CACHE_KEY_GENERATOR'; + public const PLUGINS_WIDGET_CACHE_KEY_GENERATOR_STRATEGY = 'PLUGINS_WIDGET_CACHE_KEY_GENERATOR_STRATEGY'; /** * @param \Spryker\Yves\Kernel\Container $container @@ -49,7 +49,7 @@ public function provideDependencies(Container $container) $container = $this->addUtilTextService($container); $container = $this->addFilterControllerEventSubscriberPlugins($container); $container = $this->addApplicationPlugins($container); - $container = $this->addWidgetCacheKeyGeneratorPlugins($container); + $container = $this->addWidgetCacheKeyGeneratorStrategyPlugins($container); return $container; } @@ -150,10 +150,10 @@ protected function addGlobalWidgets(Container $container) * * @return \Spryker\Yves\Kernel\Container */ - protected function addWidgetCacheKeyGeneratorPlugins(Container $container): Container + protected function addWidgetCacheKeyGeneratorStrategyPlugins(Container $container): Container { - $container->set(static::PLUGINS_WIDGET_CACHE_KEY_GENERATOR, function () { - return $this->getWidgetCacheKeyGeneratorPlugins(); + $container->set(static::PLUGINS_WIDGET_CACHE_KEY_GENERATOR_STRATEGY, function () { + return $this->getWidgetCacheKeyGeneratorStrategyPlugins(); }); return $container; @@ -178,9 +178,9 @@ protected function getGlobalWidgets(): array } /** - * @return \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorPluginInterface[] + * @return \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorStrategyPluginInterface[] */ - protected function getWidgetCacheKeyGeneratorPlugins(): array + protected function getWidgetCacheKeyGeneratorStrategyPlugins(): array { return []; } diff --git a/src/SprykerShop/Yves/ShopApplication/ShopApplicationFactory.php b/src/SprykerShop/Yves/ShopApplication/ShopApplicationFactory.php index f2bb59b..75c6856 100644 --- a/src/SprykerShop/Yves/ShopApplication/ShopApplicationFactory.php +++ b/src/SprykerShop/Yves/ShopApplication/ShopApplicationFactory.php @@ -25,6 +25,8 @@ use SprykerShop\Yves\ShopApplication\Subscriber\ShopApplicationTwigEventSubscriber; use SprykerShop\Yves\ShopApplication\Twig\RoutingHelper; use SprykerShop\Yves\ShopApplication\Twig\TwigRenderer; +use SprykerShop\Yves\ShopApplication\Twig\Widget\CacheKeyGenerator\CacheKeyGeneratorInterface; +use SprykerShop\Yves\ShopApplication\Twig\Widget\CacheKeyGenerator\StrategyCacheKeyGenerator; use SprykerShop\Yves\ShopApplication\Twig\Widget\TokenParser\WidgetTagTokenParser; use SprykerShop\Yves\ShopApplication\Twig\Widget\TokenParser\WidgetTagTwigTokenParser; use SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetFactory; @@ -67,7 +69,15 @@ public function createLegacyWidgetFactory() */ public function createWidgetFactory() { - return new WidgetFactory($this->createLegacyWidgetFactory(), $this->getWidgetCacheKeyGeneratorPlugins()); + return new WidgetFactory($this->createLegacyWidgetFactory(), $this->createCacheKeyGenerator()); + } + + /** + * @return \SprykerShop\Yves\ShopApplication\Twig\Widget\CacheKeyGenerator\CacheKeyGeneratorInterface + */ + public function createCacheKeyGenerator(): CacheKeyGeneratorInterface + { + return new StrategyCacheKeyGenerator($this->getWidgetCacheKeyGeneratorStrategyPlugins()); } /** @@ -255,10 +265,10 @@ public function getApplicationPlugins(): array } /** - * @return \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorPluginInterface[] + * @return \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorStrategyPluginInterface[] */ - public function getWidgetCacheKeyGeneratorPlugins(): array + public function getWidgetCacheKeyGeneratorStrategyPlugins(): array { - return $this->getProvidedDependency(ShopApplicationDependencyProvider::PLUGINS_WIDGET_CACHE_KEY_GENERATOR); + return $this->getProvidedDependency(ShopApplicationDependencyProvider::PLUGINS_WIDGET_CACHE_KEY_GENERATOR_STRATEGY); } } diff --git a/src/SprykerShop/Yves/ShopApplication/Twig/Widget/CacheKeyGenerator/CacheKeyGeneratorInterface.php b/src/SprykerShop/Yves/ShopApplication/Twig/Widget/CacheKeyGenerator/CacheKeyGeneratorInterface.php new file mode 100644 index 0000000..0f94d91 --- /dev/null +++ b/src/SprykerShop/Yves/ShopApplication/Twig/Widget/CacheKeyGenerator/CacheKeyGeneratorInterface.php @@ -0,0 +1,19 @@ +indexWidgetCacheKeyGeneratorPlugins($widgetCacheKeyGeneratorPlugins); + } + } + + /** + * @param string $widgetClassName + * @param array $arguments + * + * @return string|null + */ + public function generateCacheKey(string $widgetClassName, array $arguments): ?string + { + if (isset(static::$widgetCacheKeyGeneratorPlugins[$widgetClassName])) { + $key = static::$widgetCacheKeyGeneratorPlugins[$widgetClassName]->generateCacheKey($arguments); + + if ($key === null) { + return null; + } + + return md5($widgetClassName . $key); + } + + return md5($widgetClassName . serialize($arguments)); + } + + /** + * @param \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorStrategyPluginInterface[] $widgetCacheKeyGeneratorPlugins + * + * @return \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorStrategyPluginInterface[] + */ + protected function indexWidgetCacheKeyGeneratorPlugins(array $widgetCacheKeyGeneratorPlugins): array + { + $indexedWidgetCacheKeyGeneratorPlugins = []; + + foreach ($widgetCacheKeyGeneratorPlugins as $widgetCacheKeyGeneratorPlugin) { + $indexedWidgetCacheKeyGeneratorPlugins[$widgetCacheKeyGeneratorPlugin->getWidgetClassName()] = $widgetCacheKeyGeneratorPlugin; + } + + return $indexedWidgetCacheKeyGeneratorPlugins; + } +} diff --git a/src/SprykerShop/Yves/ShopApplication/Twig/Widget/WidgetFactory.php b/src/SprykerShop/Yves/ShopApplication/Twig/Widget/WidgetFactory.php index 0f4fc87..acdee04 100644 --- a/src/SprykerShop/Yves/ShopApplication/Twig/Widget/WidgetFactory.php +++ b/src/SprykerShop/Yves/ShopApplication/Twig/Widget/WidgetFactory.php @@ -11,6 +11,7 @@ use Spryker\Yves\Kernel\Dependency\Widget\WidgetInterface; use Spryker\Yves\Kernel\Widget\WidgetFactoryInterface as LegacyWidgetFactoryInterface; use SprykerShop\Yves\ShopApplication\Exception\InvalidWidgetException; +use SprykerShop\Yves\ShopApplication\Twig\Widget\CacheKeyGenerator\CacheKeyGeneratorInterface; class WidgetFactory implements WidgetFactoryInterface { @@ -20,9 +21,9 @@ class WidgetFactory implements WidgetFactoryInterface protected $legacyWidgetPluginFactory; /** - * @var \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorPluginInterface[] + * @var \SprykerShop\Yves\ShopApplication\Twig\Widget\CacheKeyGenerator\CacheKeyGeneratorInterface */ - protected static $widgetCacheKeyGeneratorPlugins = []; + protected $cacheKeyGenerator; /** * @var array @@ -31,15 +32,12 @@ class WidgetFactory implements WidgetFactoryInterface /** * @param \Spryker\Yves\Kernel\Widget\WidgetFactoryInterface $legacyWidgetPluginFactory - * @param \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorPluginInterface[] $widgetCacheKeyGeneratorPlugins + * @param \SprykerShop\Yves\ShopApplication\Twig\Widget\CacheKeyGenerator\CacheKeyGeneratorInterface $cacheKeyGenerator */ - public function __construct(LegacyWidgetFactoryInterface $legacyWidgetPluginFactory, array $widgetCacheKeyGeneratorPlugins = []) + public function __construct(LegacyWidgetFactoryInterface $legacyWidgetPluginFactory, CacheKeyGeneratorInterface $cacheKeyGenerator) { $this->legacyWidgetPluginFactory = $legacyWidgetPluginFactory; - - if (!static::$widgetCacheKeyGeneratorPlugins) { - static::$widgetCacheKeyGeneratorPlugins = $this->indexWidgetCacheKeyGeneratorPlugins($widgetCacheKeyGeneratorPlugins); - } + $this->cacheKeyGenerator = $cacheKeyGenerator; } /** @@ -54,7 +52,7 @@ public function build(string $widgetClassName, array $arguments) return $this->legacyWidgetPluginFactory->build($widgetClassName, $arguments); } - $cacheKey = $this->generateCacheKey($widgetClassName, $arguments); + $cacheKey = $this->cacheKeyGenerator->generateCacheKey($widgetClassName, $arguments); if ($cacheKey === null) { return $this->createWidgetInstance($widgetClassName, $arguments); @@ -72,22 +70,6 @@ public function build(string $widgetClassName, array $arguments) return $widget; } - /** - * @param \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorPluginInterface[] $widgetCacheKeyGeneratorPlugins - * - * @return \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorPluginInterface[] - */ - protected function indexWidgetCacheKeyGeneratorPlugins(array $widgetCacheKeyGeneratorPlugins): array - { - $indexedWidgetCacheKeyGeneratorPlugins = []; - - foreach ($widgetCacheKeyGeneratorPlugins as $widgetCacheKeyGeneratorPlugin) { - $indexedWidgetCacheKeyGeneratorPlugins[$widgetCacheKeyGeneratorPlugin->getRelatedWidgetClassName()] = $widgetCacheKeyGeneratorPlugin; - } - - return $indexedWidgetCacheKeyGeneratorPlugins; - } - /** * @param string $widgetClassName * @param array $arguments @@ -119,27 +101,6 @@ protected function assertClassIsWidget(string $widgetClassName): void } } - /** - * @param string $widgetClassName - * @param array $arguments - * - * @return string|null - */ - protected function generateCacheKey(string $widgetClassName, array $arguments): ?string - { - if (isset(static::$widgetCacheKeyGeneratorPlugins[$widgetClassName])) { - $key = static::$widgetCacheKeyGeneratorPlugins[$widgetClassName]->generateCacheKey($arguments); - - if ($key === null) { - return null; - } - - return md5($widgetClassName . $key); - } - - return md5($widgetClassName . serialize($arguments)); - } - /** * @param string $cacheKey *