Skip to content

Commit

Permalink
TE-966: Create TwigApplicationPlugin (#5091)
Browse files Browse the repository at this point in the history
* TE-966: Create TwigApplicationPlugin (#5017)

* TE-968 Created TwigExtensionInterface.
* TE-1192 Added EventDispatcherExtension module.
* TE-1191 Adding EventDispatcher module.
* TE-1192 Added Container for EventDispatcherExtensionPlugin.
* TE-1191 Added EventDispatcherExtension as dependency.
* TE-1191 Fixed Symfony version constraints.
* TE-967 Create Twig ApplicationExtension plugin (#4830).
* TE-967 Added shared Twig application plugin.
* TE-967 Moved logic to separate application extensions and twig extension plugins.
* TE-967 Created event subscriber for Zed twig service.
* TE-967 Removed all twig container id's that should not be in the application and moved logic to plugins.
* TE-967: EventDispatcherExtensionPluginInterface -> EventDispatcherPluginInterface
* TE-977 Added UserTwigPlugin.
* TE-976 Added MoneyTwigPlugin.
* TE-975 Added CurrencyTwigPlugin.
* TE-974 Added ZedNavigationTwigPlugin.
* TE-983: Added CmsContentWidgetPlugin (#4930).

* TE-983:
* Deprecated CmsContentWidgetServiceProvider
* Created CmsContentWidgetTwigPlugin as replacement for CmsContentWidgetTwigPlugin
* Updated composer dependencies

* TE-969: Move TwigChartFunctionServiceProvider (#4937)

* TE-969

* Deprecated TwigChartFunctionServiceProvider
* Created ChartTwigPlugin as replacement for TwigChartFunctionServiceProvider
* Updated composer.json

* TE-992

* Created KernelApplicationPlugin as replacement of application functionality of ShopAppliactionServiceProvider
* Created StoreApplicationPlugin as replacement of store functionality of ShopApplicaitonServiceProvider

* Revert "TE-992"

This reverts commit 247ba8e970b6e6aa55af034f1049efc55f9db8bd.

* TE-972: Move DateTimeFormatterServiceProvider (#4935)

* TE-972

* Deprecated DateTimeFormatterServiceProvider
* Created DateTimeFormatterTwigPlugin as replacement for DateTimeFormatterServiceProvider
* Updated composer.json for UtilDateTime

* TE-970: Move TwigGlobalVariablesServiceProvider (#4944)

* TE-970

* Deprecated EnvironmentInformationServiceProvider
* Depracated TwigGlobalVariablesServiceProvider
* Deprecated EnfironmentInfo
* Created ApplicationTwigPlugin
* Updated copmoser.json

* TE-994: Added LocaleExtensionModule (#4973).

* TE-994: Added LocaleExtension module.

* TE-966: Removed adding global variables from TwigApplicationPlugins (#4998)

* TE-994: MoveShopApplicationServiceProvider (#4968)

TE-994
* Created ShopApplicationApplicationPlugin as replacement of application functionality of ShopAppliactionServiceProvider
* Created StoreApplicationPlugin as replacement of store functionality of ShopApplicaitonServiceProvider
* Created LocaleApplicationPlugin as replacement of locale functionality of ShopApplicaitonServiceProvider
* Added getName to zed's AbstractTwigExtensionPlugin
* Added FLAG_USE_LOCALE_LISTENER

* TE-973 Move TranslationServiceProvider (#4953)

* added new translator plugin
* refactored Translator and UserLocale application plugins
* created LocaleApplicationPlugin
* refactored event dispatcher. Added new LocaleListener substitution for Zed
* added locale listener substitution plugin for Yves

* te-966 Fixed Translator performance by moving plugin stack execution inside of Translator to not run on instantiation.

* TE-966:
* Fixed doc block typehints for container. Ignored not found exception
*  LocalePlugin -> LocaleLocalePlugin
* Introduced `Spryker/Yves/CmsBlock/Plugin/Twig/CmsBlockPlaceholderTwigPlugin` instead of `Spryker/Yves/CmsBlock/Twig/Plugin/TwigCmsBlockPlaceholder`.
* Deprecated `Spryker/Yves/CmsBlock/Twig/Plugin/TwigCmsBlockPlaceholder`.
* Deprecated `Spryker/Yves/CmsBlock/Twig/Plugin/TwigCmsBlock`.
* Modified typehints `Spryker/Yves/Kernel/Plugin/Pimple` it is able to take Container as well as Application.
* Introduced `Spryker/Yves/Twig/Plugin/AbstractTwigExtensionPlugin`.
* te-966 Added TwigGlobalVariablesServiceProvider to ApplicationDependencyProvider for BC
* te-966 Added check with instance of which translator is added as service for BC reasons.

* TE-966 Fixed code review findings

* TE-966 Updated composer constraints
  • Loading branch information
stereomon authored Mar 26, 2019
1 parent e15d35b commit c65e6f2
Show file tree
Hide file tree
Showing 15 changed files with 660 additions and 2 deletions.
15 changes: 13 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@
"license": "proprietary",
"require": {
"php": ">=7.1",
"spryker/kernel": "^3.0.0"
"spryker/application-extension": "^1.0.0",
"spryker/event-dispatcher-extension": "^1.0.0",
"spryker/kernel": "^3.0.0",
"spryker/locale-extension": "^1.0.0"
},
"require-dev": {
"spryker/config": "*",
"spryker/container": "*",
"spryker/event-dispatcher": "*",
"spryker/installer": "*",
"spryker/propel": "*",
"spryker/symfony": "*",
"spryker/testify": "*"
},
"suggest": {
"spryker/installer": "If you want to use Installer plugins."
"spryker/config": "If you want to use LocaleApplicationPlugin",
"spryker/container": "If you want to use LocalePlugin.",
"spryker/event-dispatcher": "If you want to use EventDispatcher plugin.",
"spryker/installer": "If you want to use Installer plugins.",
"spryker/symfony": "If you want to use EventDispatcher plugin."
},
"autoload": {
"psr-4": {
Expand Down
77 changes: 77 additions & 0 deletions src/Spryker/Yves/Locale/LocaleDependencyProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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 Spryker\Yves\Locale;

use Spryker\Shared\Kernel\Store;
use Spryker\Shared\LocaleExtension\Dependency\Plugin\LocalePluginInterface;
use Spryker\Yves\Kernel\AbstractBundleDependencyProvider;
use Spryker\Yves\Kernel\Container;
use Spryker\Yves\Locale\Plugin\Locale\LocaleLocalePlugin;

class LocaleDependencyProvider extends AbstractBundleDependencyProvider
{
public const PLUGIN_LOCALE = 'PLUGIN_LOCALE';
public const STORE = 'STORE';

/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
public function provideDependencies(Container $container)
{
$container = $this->addLocalePlugin($container);
$container = $this->addStore($container);

return $container;
}

/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
public function addLocalePlugin(Container $container): Container
{
$container->set(static::PLUGIN_LOCALE, function () {
return $this->getLocalePlugin();
});

return $container;
}

/**
* @return \Spryker\Shared\LocaleExtension\Dependency\Plugin\LocalePluginInterface
*/
public function getLocalePlugin(): LocalePluginInterface
{
return new LocaleLocalePlugin();
}

/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
public function addStore(Container $container): Container
{
$container->set(static::STORE, function () {
return $this->getStore();
});

return $container;
}

/**
* @return \Spryker\Shared\Kernel\Store
*/
public function getStore(): Store
{
return Store::getInstance();
}
}
31 changes: 31 additions & 0 deletions src/Spryker/Yves/Locale/LocaleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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 Spryker\Yves\Locale;

use Spryker\Shared\Kernel\Store;
use Spryker\Shared\LocaleExtension\Dependency\Plugin\LocalePluginInterface;
use Spryker\Yves\Kernel\AbstractFactory;

class LocaleFactory extends AbstractFactory
{
/**
* @return \Spryker\Shared\LocaleExtension\Dependency\Plugin\LocalePluginInterface
*/
public function getLocalePlugin(): LocalePluginInterface
{
return $this->getProvidedDependency(LocaleDependencyProvider::PLUGIN_LOCALE);
}

/**
* @return \Spryker\Shared\Kernel\Store
*/
public function getStore(): Store
{
return $this->getProvidedDependency(LocaleDependencyProvider::STORE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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 Spryker\Yves\Locale\Plugin\Application;

use Generated\Shared\Transfer\LocaleTransfer;
use Spryker\Service\Container\ContainerInterface;
use Spryker\Shared\ApplicationExtension\Dependency\Plugin\ApplicationPluginInterface;
use Spryker\Shared\Config\Application\Environment as ApplicationEnvironment;
use Spryker\Yves\Kernel\AbstractPlugin;

/**
* @method \Spryker\Yves\Locale\LocaleFactory getFactory()
*/
class LocaleApplicationPlugin extends AbstractPlugin implements ApplicationPluginInterface
{
protected const SERVICE_LOCALE = 'locale';

/**
* Added for BC reason only.
*/
protected const BC_FEATURE_FLAG_LOCALE_LISTENER = 'BC_FEATURE_FLAG_LOCALE_LISTENER';

/**
* {@inheritdoc}
*
* @api
*
* @param \Spryker\Service\Container\ContainerInterface $container
*
* @return \Spryker\Service\Container\ContainerInterface
*/
public function provide(ContainerInterface $container): ContainerInterface
{
$container = $this->addLocale($container);

return $container;
}

/**
* @param \Spryker\Service\Container\ContainerInterface $container
*
* @return \Spryker\Service\Container\ContainerInterface
*/
protected function addLocale(ContainerInterface $container): ContainerInterface
{
$container->set(static::BC_FEATURE_FLAG_LOCALE_LISTENER, false);
$container->set(static::SERVICE_LOCALE, function (ContainerInterface $container) {
$localeName = $this->getLocaleTransfer($container)->getLocaleName();
$this->getFactory()->getStore()->setCurrentLocale($localeName);
ApplicationEnvironment::initializeLocale($localeName);

return $localeName;
});

return $container;
}

/**
* @param \Spryker\Service\Container\ContainerInterface $container
*
* @return \Generated\Shared\Transfer\LocaleTransfer
*/
protected function getLocaleTransfer(ContainerInterface $container): LocaleTransfer
{
return $this->getFactory()->getLocalePlugin()->getLocaleTransfer($container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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 Spryker\Yves\Locale\Plugin\EventDispatcher;

use Spryker\Service\Container\ContainerInterface;
use Spryker\Shared\EventDispatcher\EventDispatcherInterface;
use Spryker\Shared\EventDispatcherExtension\Dependency\Plugin\EventDispatcherPluginInterface;
use Spryker\Yves\Kernel\AbstractPlugin;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class LocaleEventDispatcherPlugin extends AbstractPlugin implements EventDispatcherPluginInterface
{
protected const SERVICE_LOCALE = 'locale';
protected const EVENT_PRIORITY = 16;

/**
* {@inheritdoc}
* - Adds locale listener.
*
* @api
*
* @param \Spryker\Shared\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @param \Spryker\Service\Container\ContainerInterface $container
*
* @return \Spryker\Shared\EventDispatcher\EventDispatcherInterface
*/
public function extend(EventDispatcherInterface $eventDispatcher, ContainerInterface $container): EventDispatcherInterface
{
$eventDispatcher->addListener(
KernelEvents::REQUEST,
function (GetResponseEvent $event) use ($container) {
$request = $event->getRequest();
$request->setDefaultLocale($this->getLocaleFromContainer($container));

$this->setRequestLocale($request);
},
static::EVENT_PRIORITY
);

return $eventDispatcher;
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Symfony\Component\HttpFoundation\Request
*/
protected function setRequestLocale(Request $request): Request
{
$locale = $request->attributes->get('_locale');
if ($locale) {
$request->setLocale($locale);
}

return $request;
}

/**
* @param \Spryker\Service\Container\ContainerInterface $container
*
* @return string
*/
protected function getLocaleFromContainer(ContainerInterface $container): string
{
return $container->get(static::SERVICE_LOCALE);
}
}
91 changes: 91 additions & 0 deletions src/Spryker/Yves/Locale/Plugin/Locale/LocaleLocalePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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 Spryker\Yves\Locale\Plugin\Locale;

use Generated\Shared\Transfer\LocaleTransfer;
use Spryker\Service\Container\ContainerInterface;
use Spryker\Shared\LocaleExtension\Dependency\Plugin\LocalePluginInterface;
use Spryker\Yves\Kernel\AbstractPlugin;
use Symfony\Component\HttpFoundation\Request;

/**
* @method \Spryker\Client\Locale\LocaleClientInterface getClient()
* @method \Spryker\Yves\Locale\LocaleFactory getFactory()
*/
class LocaleLocalePlugin extends AbstractPlugin implements LocalePluginInterface
{
public const REQUEST_URI = 'REQUEST_URI';

/**
* {@inheritdoc}
*
* @api
*
* @param \Spryker\Service\Container\ContainerInterface $container
*
* @return \Generated\Shared\Transfer\LocaleTransfer
*/
public function getLocaleTransfer(ContainerInterface $container): LocaleTransfer
{
return $this->buildLocaleTransfer();
}

/**
* @return \Generated\Shared\Transfer\LocaleTransfer
*/
protected function buildLocaleTransfer(): LocaleTransfer
{
$localeTransfer = new LocaleTransfer();
$localeTransfer->setLocaleName($this->getLocaleName());

return $localeTransfer;
}

/**
* @return string
*/
protected function getLocaleName(): string
{
$currentLocale = $this->getClient()->getCurrentLocale();

$requestUri = $this->getRequestUri();

if ($requestUri) {
$locales = $this->getFactory()->getStore()->getLocales();
$localeCode = $this->extractLocaleCode($requestUri);
if ($localeCode !== false && isset($locales[$localeCode])) {
return $locales[$localeCode];
}
}

return $currentLocale;
}

/**
* @return string|null
*/
protected function getRequestUri(): ?string
{
$requestUri = Request::createFromGlobals()
->server->get(static::REQUEST_URI);

return $requestUri;
}

/**
* @param string $requestUri
*
* @return string
*/
protected function extractLocaleCode(string $requestUri): string
{
$pathElements = explode('/', trim($requestUri, '/'));

return $pathElements[0];
}
}
Loading

0 comments on commit c65e6f2

Please sign in to comment.