8000 [Routing] allow using compiled matchers and generators without dumping PHP code by nicolas-grekas · Pull Request #28865 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] allow using compiled matchers and generators without dumping PHP code #28865

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

Merged
merged 1 commit into from
Jan 26, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@
use Symfony\Component\PropertyInfo\PropertyInitializableExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
Expand Down Expand Up @@ -754,6 +757,12 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
if (isset($config['type'])) {
$argument['resource_type'] = $config['type'];
}
if (!class_exists(CompiledUrlMatcher::class)) {
$argument['matcher_class'] = $argument['matcher_base_class'];
$argument['matcher_dumper_class'] = PhpMatcherDumper::class;
$argument['generator_class'] = $argument['generator_base_class'];
$argument['generator_dumper_class'] = PhpGeneratorDumper::class;
}
$router->replaceArgument(2, $argument);

$container->setParameter('request_listener.http_port', $config['http_port']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@
<argument type="collection">
<argument key="cache_dir">%kernel.cache_dir%</argument>
<argument key="debug">%kernel.debug%</argument>
<argument key="generator_class">Symfony\Component\Routing\Generator\UrlGenerator</argument>
<argument key="generator_class">Symfony\Component\Routing\Generator\CompiledUrlGenerator</argument>
<argument key="generator_base_class">Symfony\Component\Routing\Generator\UrlGenerator</argument>
<argument key="generator_dumper_class">Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper</argument>
<argument key="generator_dumper_class">Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper</argument>
<argument key="generator_cache_class">%router.cache_class_prefix%UrlGenerator</argument>
<argument key="matcher_class">Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher</argument>
<argument key="matcher_class">Symfony\Bundle\FrameworkBundle\Routing\RedirectableCompiledUrlMatcher</argument>
<argument key="matcher_base_class">Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher</argument>
<argument key="matcher_dumper_class">Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper</argument>
<argument key="matcher_dumper_class">Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper</argument>
<argument key="matcher_cache_class">%router.cache_class_prefix%UrlMatcher</argument>
</argument>
<argument type="service" id="router.request_context" on-invalid="ignore" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Routing;

use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface;

/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @internal
*/
class RedirectableCompiledUrlMatcher extends CompiledUrlMatcher implements RedirectableUrlMatcherInterface
{
/**
* {@inheritdoc}
*/
public function redirect($path, $route, $scheme = null)
{
return [
'_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction',
'path' => $path,
'permanent' => true,
'scheme' => $scheme,
'httpPort' => $this->context->getHttpPort(),
'httpsPort' => $this->context->getHttpsPort(),
'_route' => $route,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@

namespace Symfony\Bundle\FrameworkBundle\Routing;

@trigger_error(sprintf('The "%s" class is deprec F438 ated since Symfony 4.3.', RedirectableUrlMatcher::class), E_USER_DEPRECATED);

use Symfony\Component\Routing\Matcher\RedirectableUrlMatcher as BaseMatcher;

/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @deprecated since Symfony 4.3
*/
class RedirectableUrlMatcher extends BaseMatcher
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Routing;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableCompiledUrlMatcher;
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
* @requires function \Symfony\Component\Routing\Matcher\CompiledUrlMatcher::match
*/
class RedirectableCompiledUrlMatcherTest extends TestCase
{
public function testRedirectWhenNoSlash()
{
$routes = new RouteCollection();
$routes->add('foo', new Route('/foo/'));

$matcher = $this->getMatcher($routes, $context = new RequestContext());

$this->assertEquals([
'_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction',
'path' => '/foo/',
'permanent' => true,
'scheme' => null,
'httpPort' => $context->getHttpPort(),
'httpsPort' => $context->getHttpsPort(),
'_route' => 'foo',
],
$matcher->match('/foo')
);
}

public function testSchemeRedirect()
{
$routes = new RouteCollection();
$routes->add('foo', new Route('/foo', [], [], [], '', ['https']));

$matcher = $this->getMatcher($routes, $context = new RequestContext());

$this->assertEquals([
'_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction',
'path' => '/foo',
'permanent' => true,
'scheme' => 'https',
'httpPort' => $context->getHttpPort(),
'httpsPort' => $context->getHttpsPort(),
'_route' => 'foo',
],
$matcher->match('/foo')
);
}

private function getMatcher(RouteCollection $routes, RequestContext $context)
{
$dumper = new CompiledUrlMatcherDumper($routes);

return new RedirectableCompiledUrlMatcher($dumper->getCompiledRoutes(), $context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
* @group legacy
*/
class RedirectableUrlMatcherTest extends TestCase
{
public function testRedirectWhenNoSlash()
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
CHANGELOG
=========

4.3.0
-----

* added `CompiledUrlMatcher` and `CompiledUrlMatcherDumper`
* added `CompiledUrlGenerator` and `CompiledUrlGeneratorDumper`
* deprecated `PhpUrlGeneratorDumped` and `PhpMatcherDumper`

4.2.0
-----

Expand Down
58 changes: 58 additions & 0 deletions src/Symfony/Component/Routing/Generator/CompiledUrlGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Routing\Generator;

use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\RequestContext;

/**
* Generates URLs based on rules dumped by CompiledUrlGeneratorDumper.
*/
class CompiledUrlGenerator extends UrlGenerator
{
private $compiledRoutes = [];
private $defaultLocale;

public function __construct(array $compiledRoutes, RequestContext $context, LoggerInterface $logger = null, string $defaultLocale = null)
{
$this->compiledRoutes = $compiledRoutes;
$this->context = $context;
$this->logger = $logger;
$this->defaultLocale = $defaultLocale;
}

public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
{
$locale = $parameters['_locale']
?? $this->context->getParameter('_locale')
?: $this->defaultLocale;

if (null !== $locale) {
do {
if (($this->compiledRoutes[$name.'.'.$locale][1]['_canonical_route'] ?? null) === $name) {
unset($parameters['_locale']);
$name .= '.'.$locale;
break;
}
} while (false !== $locale = strstr($locale, '_', true));
}

if (!isset($this->compiledRoutes[$name])) {
throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
}

list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes) = $this->compiledRoutes[$name];

return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Routing\Generator\Dumper;

use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;

/**
* CompiledUrlGeneratorDumper creates a PHP array to be used with CompiledUrlGenerator.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Tobias Schultze <http://tobion.de>
* @author Nicolas Grekas <p@tchwork.com>
*/
class CompiledUrlGeneratorDumper extends GeneratorDumper
{
public function getCompiledRoutes(): array
{
$compiledRoutes = [];
foreach ($this->getRoutes()->all() as $name => $route) {
$compiledRoute = $route->compile();

$compiledRoutes[$name] = [
$compiledRoute->getVariables(),
$route->getDefaults(),
$route->getRequirements(),
$compiledRoute->getTokens(),
$compiledRoute->getHostTokens(),
$route->getSchemes(),
];
}

return $compiledRoutes;
}

/**
* {@inheritdoc}
*/
public function dump(array $options = [])
{
return <<<EOF
<?php

// This file has been auto-generated by the Symfony Routing Component.

return [{$this->generateDeclaredRoutes()}
];

EOF;
}

/**
* Generates PHP code representing an array of defined routes
* together with the routes properties (e.g. requirements).
*/
private function generateDeclaredRoutes(): string
{
$routes = '';
foreach ($this->getCompiledRoutes() as $name => $properties) {
$routes .= sprintf("\n '%s' => %s,", $name, CompiledUrlMatcherDumper::export($properties));
}

return $routes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

namespace Symfony\Component\Routing\Generator\Dumper;

use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "CompiledUrlGeneratorDumper" instead.', PhpGeneratorDumper::class), E_USER_DEPRECATED);

use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;

/**
* PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Tobias Schultze <http://tobion.de>
*
* @deprecated since Symfony 4.3, use CompiledUrlGeneratorDumper instead.
*/
class PhpGeneratorDumper extends GeneratorDumper
{
Expand Down Expand Up @@ -92,7 +96,7 @@ private function generateDeclaredRoutes()
$properties[] = $compiledRoute->getHostTokens();
$properties[] = $route->getSchemes();

$routes .= sprintf(" '%s' => %s,\n", $name, PhpMatcherDumper::export($properties));
$routes .= sprintf(" '%s' => %s,\n", $name, CompiledUrlMatcherDumper::export($properties));
}
$routes .= ' ]';

Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/Routing/Matcher/CompiledUrlMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Routing\Matcher;

use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherTrait;
use Symfony\Component\Routing\RequestContext;

/**
* Matches URLs based on rules dumped by CompiledUrlMatcherDumper.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class CompiledUrlMatcher extends UrlMatcher
{
use CompiledUrlMatcherTrait;

public function __construct(array $compiledRoutes, RequestContext $context)
{
$this->context = $context;
list($this->matchHost, $this->staticRoutes, $this->regexpList, $this->dynamicRoutes, $this->checkCondition) = $compiledRoutes;
}
}
Loading
0