Skip to content

Commit

Permalink
TE-164 WidgetFactory refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasnyulas committed Sep 24, 2018
1 parent bec3c3c commit c61294a
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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\Exception;

use Exception;

class InvalidWidgetException extends Exception
{
}
29 changes: 9 additions & 20 deletions src/SprykerShop/Yves/ShopApplication/ShopApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@

use Silex\Provider\TwigServiceProvider;
use Spryker\Yves\Kernel\AbstractFactory;
use Spryker\Yves\Kernel\Widget\WidgetAbstractFactory;
use Spryker\Yves\Kernel\Widget\WidgetAbstractFactoryInterface;
use Spryker\Yves\Kernel\Widget\WidgetCollection;
use Spryker\Yves\Kernel\Widget\WidgetContainerInterface;
use Spryker\Yves\Kernel\Widget\WidgetContainerRegistry;
use Spryker\Yves\Kernel\Widget\WidgetFactory;
use Spryker\Yves\Kernel\Widget\WidgetFactory as LegacyWidgetFactory;
use SprykerShop\Yves\ShopApplication\Dependency\Service\ShopApplicationToUtilTextServiceInterface;
use SprykerShop\Yves\ShopApplication\Twig\RoutingHelper;
use SprykerShop\Yves\ShopApplication\Twig\TwigRenderer;
use SprykerShop\Yves\ShopApplication\Twig\Widget\TokenParser\WidgetTagTokenParser;
use SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetBuilder;
use SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetBuilderInterface;
use SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetFactory;
use SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetTagService;
use SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetTagServiceInterface;
use Twig_TokenParserInterface;
Expand All @@ -41,21 +38,21 @@ public function createWidgetContainerRegistry()
}

/**
* @deprecated Use createWidgetAbstractFactory() method instead.
* @deprecated Use createWidgetFactory() method instead.
*
* @return \Spryker\Yves\Kernel\Widget\WidgetFactoryInterface
*/
public function createWidgetFactory()
public function createLegacyWidgetFactory()
{
return new WidgetFactory();
return new LegacyWidgetFactory();
}

/**
* @return \Spryker\Yves\Kernel\Widget\WidgetAbstractFactoryInterface
* @return \SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetFactoryInterface
*/
public function createWidgetAbstractFactory(): WidgetAbstractFactoryInterface
public function createWidgetFactory()
{
return new WidgetAbstractFactory();
return new WidgetFactory($this->createLegacyWidgetFactory());
}

/**
Expand Down Expand Up @@ -170,15 +167,7 @@ public function createWidgetTagService(): WidgetTagServiceInterface
return new WidgetTagService(
$this->createWidgetContainerRegistry(),
$this->getGlobalWidgetCollection(),
$this->createWidgetBuilder()
$this->createWidgetFactory()
);
}

/**
* @return \SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetBuilderInterface
*/
protected function createWidgetBuilder(): WidgetBuilderInterface
{
return new WidgetBuilder($this->createWidgetAbstractFactory(), $this->createWidgetFactory());
}
}
50 changes: 0 additions & 50 deletions src/SprykerShop/Yves/ShopApplication/Twig/Widget/WidgetBuilder.php

This file was deleted.

111 changes: 111 additions & 0 deletions src/SprykerShop/Yves/ShopApplication/Twig/Widget/WidgetFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?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;

use Spryker\Yves\Kernel\Dependency\Plugin\WidgetPluginInterface;
use Spryker\Yves\Kernel\Dependency\Widget\WidgetInterface;
use Spryker\Yves\Kernel\Widget\WidgetFactoryInterface as LegacyWidgetFactoryInterface;
use SprykerShop\Yves\ShopApplication\Exception\InvalidWidgetException;

class WidgetFactory implements WidgetFactoryInterface
{
/**
* @var \Spryker\Yves\Kernel\Widget\WidgetFactoryInterface
*/
protected $legacyWidgetPluginFactory;

/**
* @var array
*/
protected static $widgetCache = [];

/**
* @param \Spryker\Yves\Kernel\Widget\WidgetFactoryInterface $legacyWidgetPluginFactory
*/
public function __construct(LegacyWidgetFactoryInterface $legacyWidgetPluginFactory)
{
$this->legacyWidgetPluginFactory = $legacyWidgetPluginFactory;
}

/**
* @param string $widgetClassName
* @param array $arguments
*
* @return \Spryker\Yves\Kernel\Dependency\Widget\WidgetInterface|\Spryker\Yves\Kernel\Dependency\Plugin\WidgetPluginInterface
*/
public function build(string $widgetClassName, array $arguments)
{
if (is_subclass_of($widgetClassName, WidgetPluginInterface::class)) {
return $this->legacyWidgetPluginFactory->build($widgetClassName, $arguments);
}

$cacheKey = $this->generateCacheKey($widgetClassName, $arguments);
$widget = $this->getCachedWidget($cacheKey);
if ($widget) {
return $widget;
}

$this->assertClassIsWidget($widgetClassName);

$widget = new $widgetClassName(...$arguments);

$this->cacheWidget($cacheKey, $widget);

return $widget;
}

/**
* @param string $widgetClassName
*
* @throws \SprykerShop\Yves\ShopApplication\Exception\InvalidWidgetException
*
* @return void
*/
protected function assertClassIsWidget(string $widgetClassName): void
{
if (!is_subclass_of($widgetClassName, WidgetInterface::class)) {
throw new InvalidWidgetException(sprintf(
'Invalid widget %s. This class needs to implement %s.',
$widgetClassName,
WidgetInterface::class
));
}
}

/**
* @param string $widgetClassName
* @param array $arguments
*
* @return string
*/
protected function generateCacheKey(string $widgetClassName, array $arguments): string
{
return md5($widgetClassName . serialize($arguments));
}

/**
* @param string $cacheKey
*
* @return \Spryker\Yves\Kernel\Dependency\Widget\WidgetInterface|null
*/
protected function getCachedWidget(string $cacheKey): ?WidgetInterface
{
return static::$widgetCache[$cacheKey] ?? null;
}

/**
* @param string $cacheKey
* @param \Spryker\Yves\Kernel\Dependency\Widget\WidgetInterface $widget
*
* @return void
*/
protected function cacheWidget(string $cacheKey, WidgetInterface $widget): void
{
static::$widgetCache[$cacheKey] = $widget;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

namespace SprykerShop\Yves\ShopApplication\Twig\Widget;

interface WidgetBuilderInterface
interface WidgetFactoryInterface
{
/**
* @param string $widgetClassName
* @param array $arguments
*
* @return \Spryker\Yves\Kernel\Dependency\Plugin\WidgetPluginInterface|\Spryker\Yves\Kernel\Dependency\Widget\WidgetInterface
* @return \Spryker\Yves\Kernel\Dependency\Widget\WidgetInterface|\Spryker\Yves\Kernel\Dependency\Plugin\WidgetPluginInterface
*/
public function build(string $widgetClassName, array $arguments);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ class WidgetTagService implements WidgetTagServiceInterface
protected $globalWidgetCollection;

/**
* @var \SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetBuilderInterface
* @var \SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetFactoryInterface
*/
protected $widgetBuilder;
protected $widgetFactory;

/**
* @param \Spryker\Yves\Kernel\Widget\WidgetContainerRegistryInterface $widgetContainerRegistry
* @param \Spryker\Yves\Kernel\Widget\WidgetContainerInterface $globalWidgetCollection
* @param \SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetBuilderInterface $widgetBuilder
* @param \SprykerShop\Yves\ShopApplication\Twig\Widget\WidgetFactoryInterface $widgetFactory
*/
public function __construct(
WidgetContainerRegistryInterface $widgetContainerRegistry,
WidgetContainerInterface $globalWidgetCollection,
WidgetBuilderInterface $widgetBuilder
WidgetFactoryInterface $widgetFactory
) {
$this->widgetContainerRegistry = $widgetContainerRegistry;
$this->globalWidgetCollection = $globalWidgetCollection;
$this->widgetBuilder = $widgetBuilder;
$this->widgetFactory = $widgetFactory;
}

/**
Expand Down Expand Up @@ -118,7 +118,7 @@ protected function createWidgetByName(string $widgetName, array $arguments)

$widgetClass = $widgetContainer->getWidgetClassName($widgetName);

return $this->widgetBuilder->build($widgetClass, $arguments);
return $this->widgetFactory->build($widgetClass, $arguments);
}

/**
Expand Down

0 comments on commit c61294a

Please sign in to comment.