Skip to content

Commit

Permalink
Merge branch 'master' of github.com:spryker/spryker into feature/sc-4…
Browse files Browse the repository at this point in the history
…055/master-pre-generated-code
  • Loading branch information
alexanderM91 committed Jul 27, 2020
2 parents 3ca588d + c44634f commit 6d702ed
Show file tree
Hide file tree
Showing 14 changed files with 670 additions and 24 deletions.
1 change: 0 additions & 1 deletion codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ paths:
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
suite_class: \PHPUnit\Framework\TestSuite
colors: true
memory_limit: 1024M
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
"spryker/zend": "^2.1.0"
},
"require-dev": {
"spryker/application": "*",
"spryker/code-sniffer": "*",
"spryker/event-dispatcher": "*",
"spryker/silex": "*",
"spryker/testify": "*",
"spryker/util-text": "*"
},
"suggest": {
"spryker/event-dispatcher": "If you want to use the RouterLocaleEventDispatcher plugin.",
"spryker/silex": "Deprecated dependency try to not use it anymore.",
"spryker/util-text": "If you want to use the RouterEnhancer plugins."
},
"autoload": {
"psr-4": {
"Spryker\\": "src/Spryker/",
"SprykerTest\\Yves\\Router\\Helper\\": "tests/SprykerTest/Yves/Router/_support/Helper/"
"SprykerTest\\Yves\\Router\\Helper\\": "tests/SprykerTest/Yves/Router/_support/Helper/",
"SprykerTest\\Zed\\Router\\Helper\\": "tests/SprykerTest/Zed/Router/_support/Helper/"
}
},
"autoload-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Spryker\Glue\Router\Plugin\EventDispatcher;

use Silex\Provider\RoutingServiceProvider;
use Spryker\Glue\Kernel\AbstractPlugin;
use Spryker\Glue\Router\Plugin\Application\RouterApplicationPlugin;
use Spryker\Glue\Router\Router\ChainRouter;
Expand All @@ -23,6 +22,8 @@
*/
class RouterListenerEventDispatcherPlugin extends AbstractPlugin implements EventDispatcherPluginInterface
{
protected const BC_FEATURE_FLAG_ROUTER_LISTENER = 'BC_FEATURE_FLAG_ROUTER_LISTENER';

/**
* {@inheritDoc}
* - Adds a RouteListener to the EventDispatcher.
Expand All @@ -36,13 +37,27 @@ class RouterListenerEventDispatcherPlugin extends AbstractPlugin implements Even
*/
public function extend(EventDispatcherInterface $eventDispatcher, ContainerInterface $container): EventDispatcherInterface
{
$container->set(RoutingServiceProvider::BC_FEATURE_FLAG_ROUTER_LISTENER, false); // disables Silex RouterListener
$container = $this->disableSilexRouter($container);

$eventDispatcher = $this->addSubscriber($eventDispatcher, $container);

return $eventDispatcher;
}

/**
* @deprecated Will be removed without replacement.
*
* @param \Spryker\Service\Container\ContainerInterface $container
*
* @return \Spryker\Service\Container\ContainerInterface
*/
protected function disableSilexRouter(ContainerInterface $container): ContainerInterface
{
$container->set(static::BC_FEATURE_FLAG_ROUTER_LISTENER, false);

return $container;
}

/**
* @param \Spryker\Shared\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @param \Spryker\Service\Container\ContainerInterface $container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Spryker\Yves\Router\Plugin\EventDispatcher;

use Silex\Provider\RoutingServiceProvider;
use Spryker\Service\Container\ContainerInterface;
use Spryker\Shared\EventDispatcher\EventDispatcherInterface;
use Spryker\Shared\EventDispatcherExtension\Dependency\Plugin\EventDispatcherPluginInterface;
Expand All @@ -23,6 +22,8 @@
*/
class RouterListenerEventDispatcherPlugin extends AbstractPlugin implements EventDispatcherPluginInterface
{
protected const BC_FEATURE_FLAG_ROUTER_LISTENER = 'BC_FEATURE_FLAG_ROUTER_LISTENER';

/**
* {@inheritDoc}
* - Adds a RouteListener to the EventDispatcher.
Expand All @@ -36,13 +37,27 @@ class RouterListenerEventDispatcherPlugin extends AbstractPlugin implements Even
*/
public function extend(EventDispatcherInterface $eventDispatcher, ContainerInterface $container): EventDispatcherInterface
{
$container->set(RoutingServiceProvider::BC_FEATURE_FLAG_ROUTER_LISTENER, false); // disables Silex RouterListener
$container = $this->disableSilexRouter($container);

$eventDispatcher = $this->addSubscriber($eventDispatcher, $container);

return $eventDispatcher;
}

/**
* @deprecated Will be removed without replacement.
*
* @param \Spryker\Service\Container\ContainerInterface $container
*
* @return \Spryker\Service\Container\ContainerInterface
*/
protected function disableSilexRouter(ContainerInterface $container): ContainerInterface
{
$container->set(static::BC_FEATURE_FLAG_ROUTER_LISTENER, false);

return $container;
}

/**
* @param \Spryker\Shared\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @param \Spryker\Service\Container\ContainerInterface $container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
*/
class LanguageDefaultPostAddRouteManipulatorPlugin extends AbstractPlugin implements PostAddRouteManipulatorPluginInterface
{
/**
* @var string
*/
protected $allowedLocalesPattern;

/**
* @param string $routeName
* @param \Symfony\Component\Routing\Route $route
Expand Down
2 changes: 1 addition & 1 deletion src/Spryker/Yves/Router/Router/ChainRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Cmf\Component\Routing\ChainRouter as SymfonyChainRouter;

class ChainRouter extends SymfonyChainRouter
class ChainRouter extends SymfonyChainRouter implements RouterInterface
{
/**
* @param \Spryker\Yves\RouterExtension\Dependency\Plugin\RouterPluginInterface[] $routerPlugins
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<?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\Zed\Router\Communication\Plugin\EventDispatcher;

use InvalidArgumentException;
use Spryker\Service\Container\ContainerInterface;
use Spryker\Shared\EventDispatcher\EventDispatcherInterface;
use Spryker\Shared\EventDispatcherExtension\Dependency\Plugin\EventDispatcherPluginInterface;
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* @method \Spryker\Zed\Http\HttpConfig getConfig()
* @method \Spryker\Zed\Http\Communication\HttpCommunicationFactory getFactory()
* @method \Spryker\Zed\Router\Business\RouterFacadeInterface getFacade()
*/
class RequestAttributesEventDispatcherPlugin extends AbstractPlugin implements EventDispatcherPluginInterface
{
protected const EARLY_EVENT = 512;

protected const MODULE = 'module';
protected const CONTROLLER = 'controller';
protected const ACTION = 'action';

protected const DEFAULT_MODULE = 'application';
protected const DEFAULT_CONTROLLER = 'index';
protected const DEFAULT_ACTION = 'index';

protected const POSITION_OF_ACTION = 2;
protected const POSITION_OF_CONTROLLER = 1;
protected const POSITION_OF_MODULE = 0;

/**
* {@inheritDoc}
* - Adds a listener to the `\Symfony\Component\HttpKernel\KernelEvents::REQUEST` event to extract request specific attributes.
*
* @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 (RequestEvent $event) {
return $this->addRequestAttributes($event);
}, static::EARLY_EVENT);

return $eventDispatcher;
}

/**
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
*
* @return \Symfony\Component\HttpKernel\Event\RequestEvent
*/
protected function addRequestAttributes(RequestEvent $event): RequestEvent
{
if (!$this->isCli($event)) {
return $this->parseRequestData($event);
}

return $this->parseCliRequestData($event);
}

/**
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
*
* @return bool
*/
protected function isCli(RequestEvent $event): bool
{
return (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') && $event->getRequest()->server->get('argv', false);
}

/**
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
*
* @return \Symfony\Component\HttpKernel\Event\RequestEvent
*/
protected function parseRequestData(RequestEvent $event): RequestEvent
{
$request = $event->getRequest();

$requestUriFragments = $this->getRequestUriFragments($request);

$request->attributes->set(static::ACTION, $this->getAction($request, $requestUriFragments));
$request->attributes->set(static::CONTROLLER, $this->getController($request, $requestUriFragments));
$request->attributes->set(static::MODULE, $this->getModule($request, $requestUriFragments));

return $event;
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return array
*/
protected function getRequestUriFragments(Request $request): array
{
$requestUriWithoutParameters = strtok($request->server->get('REQUEST_URI'), '?');
$requestUriWithoutParameters = trim($requestUriWithoutParameters, '/');
$requestUriWithoutParameters = str_replace('//', '/', $requestUriWithoutParameters);

return explode('/', $requestUriWithoutParameters);
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param array $requestUriFragments
*
* @return string
*/
protected function getAction(Request $request, array $requestUriFragments): string
{
if (count($requestUriFragments) < 3) {
return static::DEFAULT_ACTION;
}

return $requestUriFragments[static::POSITION_OF_ACTION];
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param array $requestUriFragments
*
* @return string
*/
protected function getController(Request $request, array $requestUriFragments): string
{
if (count($requestUriFragments) < 2) {
return static::DEFAULT_CONTROLLER;
}

return $requestUriFragments[static::POSITION_OF_CONTROLLER];
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param array $requestUriFragments
*
* @return string
*/
protected function getModule(Request $request, array $requestUriFragments): string
{
if (count($requestUriFragments) < 1) {
return static::DEFAULT_MODULE;
}

return $requestUriFragments[static::POSITION_OF_MODULE];
}

/**
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
*
* @return \Symfony\Component\HttpKernel\Event\RequestEvent
*/
protected function parseCliRequestData(RequestEvent $event): RequestEvent
{
$request = $event->getRequest();
$request = $this->setCliRequestAttributes($request);

$this->assertCliRequestAttributes($request);

return $event;
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Symfony\Component\HttpFoundation\Request
*/
protected function setCliRequestAttributes(Request $request): Request
{
foreach ($request->server->get('argv') as $argument) {
preg_match_all('/--(.*)=(.*)/', $argument, $matches);

if ($matches[0]) {
$key = $matches[1][0];
$value = $matches[static::POSITION_OF_ACTION][0];
$request->attributes->set($key, $value);
}
}

return $request;
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @throws \InvalidArgumentException
*
* @return void
*/
protected function assertCliRequestAttributes(Request $request): void
{
$requiredParameters = [
static::MODULE,
static::CONTROLLER,
static::ACTION,
];

foreach ($requiredParameters as $parameter) {
if (!$request->attributes->has($parameter)) {
throw new InvalidArgumentException(sprintf('Required parameter --%s is missing!', $parameter));
}
}
}
}
Loading

0 comments on commit 6d702ed

Please sign in to comment.