Skip to content

Commit 996cfb5

Browse files
[Routing] allow using compiled matchers and generators without dumping PHP code
1 parent 176716e commit 996cfb5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+5013
-486
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* added `CompiledUrlMatcher` and `CompiledUrlMatcherDumper`
8+
* added `CompiledUrlGenerator` and `CompiledUrlGeneratorDumper`
9+
* deprecated `PhpUrlGeneratorDumped` and `PhpMatcherDumper`
10+
411
4.2.0
512
-----
613

Generator/CompiledUrlGenerator.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Generator;
13+
14+
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\Routing\Exception\RouteNotFoundException;
16+
use Symfony\Component\Routing\RequestContext;
17+
18+
/**
19+
* Generates URLs based on rules dumped by CompiledUrlGeneratorDumper.
20+
*/
21+
class CompiledUrlGenerator extends UrlGenerator
22+
{
23+
private $compiledRoutes = [];
24+
private $defaultLocale;
25+
26+
public function __construct(array $compiledRoutes, RequestContext $context, LoggerInterface $logger = null, string $defaultLocale = null)
27+
{
28+
$this->compiledRoutes = $compiledRoutes;
29+
$this->context = $context;
30+
$this->logger = $logger;
31+
$this->defaultLocale = $defaultLocale;
32+
}
33+
34+
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
35+
{
36+
$locale = $parameters['_locale']
37+
?? $this->context->getParameter('_locale')
38+
?: $this->defaultLocale;
39+
40+
if (null !== $locale) {
41+
do {
42+
if (($this->compiledRoutes[$name.'.'.$locale][1]['_canonical_route'] ?? null) === $name) {
43+
unset($parameters['_locale']);
44+
$name .= '.'.$locale;
45+
break;
46+
}
47+
} while (false !== $locale = strstr($locale, '_', true));
48+
}
49+
50+
if (!isset($this->compiledRoutes[$name])) {
51+
throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
52+
}
53+
54+
list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes) = $this->compiledRoutes[$name];
55+
56+
return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
57+
}
58+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Generator\Dumper;
13+
14+
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
15+
16+
/**
17+
* CompiledUrlGeneratorDumper creates a PHP array to be used with CompiledUrlGenerator.
18+
*
19+
* @author Fabien Potencier <[email protected]>
20+
* @author Tobias Schultze <http://tobion.de>
21+
* @author Nicolas Grekas <[email protected]>
22+
*/
23+
class CompiledUrlGeneratorDumper extends GeneratorDumper
24+
{
25+
public function getCompiledRoutes(): array
26+
{
27+
$compiledRoutes = [];
28+
foreach ($this->getRoutes()->all() as $name => $route) {
29+
$compiledRoute = $route->compile();
30+
31+
$compiledRoutes[$name] = [
32+
$compiledRoute->getVariables(),
33+
$route->getDefaults(),
34+
$route->getRequirements(),
35+
$compiledRoute->getTokens(),
36+
$compiledRoute->getHostTokens(),
37+
$route->getSchemes(),
38+
];
39+
}
40+
41+
return $compiledRoutes;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function dump(array $options = [])
48+
{
49+
return <<<EOF
50+
<?php
51+
52+
// This file has been auto-generated by the Symfony Routing Component.
53+
54+
return [{$this->generateDeclaredRoutes()}
55+
];
56+
57+
EOF;
58+
}
59+
60+
/**
61+
* Generates PHP code representing an array of defined routes
62+
* together with the routes properties (e.g. requirements).
63+
*/
64+
private function generateDeclaredRoutes(): string
65+
{
66+
$routes = '';
67+
foreach ($this->getCompiledRoutes() as $name => $properties) {
68+
$routes .= sprintf("\n '%s' => %s,", $name, CompiledUrlMatcherDumper::export($properties));
69+
}
70+
71+
return $routes;
72+
}
73+
}

Generator/Dumper/PhpGeneratorDumper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212
namespace Symfony\Component\Routing\Generator\Dumper;
1313

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

1618
/**
1719
* PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.
1820
*
1921
* @author Fabien Potencier <[email protected]>
2022
* @author Tobias Schultze <http://tobion.de>
23+
*
24+
* @deprecated since Symfony 4.3, use CompiledUrlGeneratorDumper instead.
2125
*/
2226
class PhpGeneratorDumper extends GeneratorDumper
2327
{
@@ -92,7 +96,7 @@ private function generateDeclaredRoutes()
9296
$properties[] = $compiledRoute->getHostTokens();
9397
$properties[] = $route->getSchemes();
9498

95-
$routes .= sprintf(" '%s' => %s,\n", $name, PhpMatcherDumper::export($properties));
99+
$routes .= sprintf(" '%s' => %s,\n", $name, CompiledUrlMatcherDumper::export($properties));
96100
}
97101
$routes .= ' ]';
98102

Matcher/CompiledUrlMatcher.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Matcher;
13+
14+
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherTrait;
15+
use Symfony\Component\Routing\RequestContext;
16+
17+
/**
18+
* Matches URLs based on rules dumped by CompiledUrlMatcherDumper.
19+
*
20+
* @author Nicolas Grekas <[email protected]>
21+
*/
22+
class CompiledUrlMatcher extends UrlMatcher
23+
{
24+
use CompiledUrlMatcherTrait;
25+
26+
public function __construct(array $compiledRoutes, RequestContext $context)
27+
{
28+
$this->context = $context;
29+
list($this->matchHost, $this->staticRoutes, $this->regexpList, $this->dynamicRoutes, $this->checkCondition) = $compiledRoutes;
30+
}
31+
}

0 commit comments

Comments
 (0)