Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor from getRouteCollection to generate and match to improve performance #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": "^7.2.5",
"php": ">=7.2.5",
"symfony/dependency-injection": "^4.1.12|^5.0",
"symfony/config": "~4.4|~5.0",
"doctrine/annotations": "^1.6",
Expand Down
27 changes: 18 additions & 9 deletions src/Decorator/RouterDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Pgs\HashIdBundle\ParametersProcessor\Factory\EncodeParametersProcessorFactory;
use Pgs\HashIdBundle\Traits\DecoratorTrait;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;


class RouterDecorator implements RouterInterface, WarmableInterface
{
use DecoratorTrait;
Expand Down Expand Up @@ -38,23 +40,30 @@ public function generate(
return $this->getRouter()->generate($name, $parameters, $referenceType);
}

private function getRoute(string $name, array $parameters): ?Route
private function getRoute(string $name, array $parameters): ?array
{
$routeCollection = $this->getRouter()->getRouteCollection();
$route = $routeCollection->get($name);
$url = null;

if (null === $route) {
try {
$url = $this->getRouter()->generate($name, $parameters);
} catch (RouteNotFoundException $e) {
$locale = $parameters['_locale'] ?? $this->getRouter()->getContext()->getParameter('_locale');
$route = $routeCollection->get(sprintf('%s.%s', $name, $locale));
$url = $this->getRouter()->generate(sprintf('%s.%s', $name, $locale));
}

try {
return $this->getRouter()->match($url);
} catch (\Exception $e) {
// Ignore method not allow, no route found, etc.
}

return $route;
return null;
}

private function processParameters(?Route $route, array &$parameters): void
private function processParameters(?array $route, array &$parameters): void
{
if (null !== $route) {
$parametersProcessor = $this->parametersProcessorFactory->createRouteEncodeParametersProcessor($route);
$parametersProcessor = $this->parametersProcessorFactory->createRouteEncodeParametersProcessor($route['_controller']);
if ($parametersProcessor->needToProcess()) {
$parameters = $parametersProcessor->process($parameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Pgs\HashIdBundle\Exception\InvalidControllerException;
use Pgs\HashIdBundle\Exception\MissingClassOrMethodException;
use Pgs\HashIdBundle\ParametersProcessor\ParametersProcessorInterface;
use Symfony\Component\Routing\Route;

class EncodeParametersProcessorFactory extends AbstractParametersProcessorFactory
{
Expand All @@ -27,9 +26,8 @@ public function __construct(
$this->encodeParametersProcessor = $encodeParametersProcessor;
}

public function createRouteEncodeParametersProcessor(Route $route)
public function createRouteEncodeParametersProcessor(string $controller)
{
$controller = $route->getDefault('_controller');
try {
/** @var Hash $annotation */
$annotation = $this->getAnnotationProvider()->getFromString($controller, Hash::class);
Expand Down
2 changes: 1 addition & 1 deletion tests/AnnotationProvider/AnnotationProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AnnotationProviderTest extends TestCase
*/
protected $controllerAnnotationProvider;

protected function setUp()
protected function setUp(): void
{
$this->controllerAnnotationProvider = new AnnotationProvider(
$this->getReaderMock(),
Expand Down
2 changes: 1 addition & 1 deletion tests/Decorator/RouterDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RouterDecoratorTest extends WebTestCase
*/
protected static $container;

protected function setUp()
protected function setUp(): void
{
$this::$container = static::createClient()->getContainer();
$this->router = self::$container->get('router');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EventSubscriberCompilerPassTest extends TestCase
*/
private $decodeControllerParametersDefinition;

protected function setUp()
protected function setUp(): void
{
$this->pass = new EventSubscriberCompilerPass();
$this->container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class HashidsConverterCompilerPassTest extends TestCase
*/
private $container;

protected function setUp()
protected function setUp(): void
{
$this->compilerPass = new HashidsConverterCompilerPass();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class ParametersProcessorFactoryTest extends TestCase

protected $parametersProcessorMockProvider;

protected function setUp()
protected function setUp(): void
{
$this->controllerMockProvider = new ControllerMockProvider();
$this->controllerAnnotationMockProvider = new ControllerAnnotationProviderMockProvider();
Expand Down
2 changes: 1 addition & 1 deletion tests/ParametersProcessor/NoOpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class NoOpTest extends TestCase
*/
protected $parametersProcessor;

protected function setUp()
protected function setUp(): void
{
$this->parametersProcessor = new NoOp($this->getHashidMock(), []);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Service/DecodeControllerParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DecodeControllerParametersTest extends TestCase

protected $parametersProcessorMockProvider;

protected function setUp()
protected function setUp(): void
{
$this->parametersProcessorMockProvider = new ParametersProcessorMockProvider();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Traits/DecoratorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DecoratorTraitTest extends TestCase
{
protected $decorateClass;

protected function setUp()
protected function setUp(): void
{
$baseClass = new BaseTestClass();
$this->decorateClass = new DecorateTestClass($baseClass);
Expand Down