Skip to content

Commit

Permalink
TE-8140 moved cache key generation to the separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
gechetspr committed Dec 29, 2020
1 parent 5862211 commit e9b8e59
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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 [];
}
Expand Down
18 changes: 14 additions & 4 deletions src/SprykerShop/Yves/ShopApplication/ShopApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerShop\Yves\ShopApplication\Twig\Widget\CacheKeyGenerator;

interface CacheKeyGeneratorInterface
{
/**
* @param string $widgetClassName
* @param array $arguments
*
* @return string|null
*/
public function generateCacheKey(string $widgetClassName, array $arguments): ?string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerShop\Yves\ShopApplication\Twig\Widget\CacheKeyGenerator;

class StrategyCacheKeyGenerator implements CacheKeyGeneratorInterface
{
/**
* @var \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorStrategyPluginInterface[]
*/
protected static $widgetCacheKeyGeneratorPlugins = [];

/**
* @param \SprykerShop\Yves\ShopApplicationExtension\Dependency\Plugin\WidgetCacheKeyGeneratorStrategyPluginInterface[] $widgetCacheKeyGeneratorPlugins
*/
public function __construct(array $widgetCacheKeyGeneratorPlugins = [])
{
if (!static::$widgetCacheKeyGeneratorPlugins) {
static::$widgetCacheKeyGeneratorPlugins = $this->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;
}
}
53 changes: 7 additions & 46 deletions src/SprykerShop/Yves/ShopApplication/Twig/Widget/WidgetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit e9b8e59

Please sign in to comment.