Skip to content

Commit

Permalink
feature #28865 [Routing] allow using compiled matchers and generators…
Browse files Browse the repository at this point in the history
… without dumping PHP code (nicolas-grekas)

This PR was merged into the 4.3-dev branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #29590
| License       | MIT
| Doc PR        | symfony/symfony-docs#10790

This is a resurrection of #25909 to make matcher+generator dumpers output PHP arrays instead of PHP code.
Don't be fooled by the diff stats, it's mostly fixtures.

This PR should contribute to making the Routing component easier to use standalone.

On the way back from SFLive USA.

![image](https://user-images.githubusercontent.com/243674/46920076-784e1b80-cf9d-11e8-86e7-850fffb409de.png)

Commits
-------

f0a519a [Routing] allow using compiled matchers and generators without dumping PHP code
  • Loading branch information
nicolas-grekas committed Jan 26, 2019
2 parents 4fbb6e5 + f0a519a commit d0effcd
Show file tree
Hide file tree
Showing 51 changed files with 5,144 additions and 490 deletions.
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
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
@@ -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,
];
}
}
Expand Up @@ -11,10 +11,14 @@

namespace Symfony\Bundle\FrameworkBundle\Routing;

@trigger_error(sprintf('The "%s" class is deprecated 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
@@ -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);
}
}
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
@@ -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
@@ -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);
}
}
@@ -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;
}
}
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
@@ -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;
}
}

0 comments on commit d0effcd

Please sign in to comment.