Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aed9bd7

Browse files
[Routing] dump static arrays instead of classes for both matcher and generator
1 parent 8cf8d16 commit aed9bd7

26 files changed

+1774
-8
lines changed

.php_cs.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ return PhpCsFixer\Config::create()
4040
->notPath('Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/default.phpt')
4141
->notPath('Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/weak.phpt')
4242
->notPath('Symfony/Component/Debug/Tests/DebugClassLoaderTest.php')
43+
// file autogenerated
44+
->notPath('Symfony/Component/Routing/Tests/Fixtures/dumper/static_url_matcher3.php')
4345
)
4446
;

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@
6363
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
6464
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
6565
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
66+
use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
6667
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
6768
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
69+
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
70+
use Symfony\Component\Routing\Matcher\StaticUrlMatcher;
6871
use Symfony\Component\Security\Core\Security;
6972
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
7073
use Symfony\Component\Serializer\Encoder\DecoderInterface;
@@ -642,6 +645,12 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
642645
if (isset($config['type'])) {
643646
$argument['resource_type'] = $config['type'];
644647
}
648+
if (!class_exists(StaticUrlMatcher::class)) {
649+
$argument['generator_class'] = $argument['generator_base_class'];
650+
$argument['generator_dumper_class'] = PhpGeneratorDumper::class;
651+
$argument['matcher_class'] = $argument['matcher_base_class'];
652+
$argument['matcher_dumper_class'] = PhpMatcherDumper::class;
653+
}
645654
$router->replaceArgument(2, $argument);
646655

647656
$container->setParameter('request_listener.http_port', $config['http_port']);

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@
5858
<argument type="collection">
5959
<argument key="cache_dir">%kernel.cache_dir%</argument>
6060
<argument key="debug">%kernel.debug%</argument>
61-
<argument key="generator_class">Symfony\Component\Routing\Generator\UrlGenerator</argument>
61+
<argument key="generator_class">Symfony\Component\Routing\Generator\StaticUrlGenerator</argument>
6262
<argument key="generator_base_class">Symfony\Component\Routing\Generator\UrlGenerator</argument>
63-
<argument key="generator_dumper_class">Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper</argument>
63+
<argument key="generator_dumper_class">Symfony\Component\Routing\Generator\Dumper\StaticUrlGeneratorDumper</argument>
6464
<argument key="generator_cache_class">%router.cache_class_prefix%UrlGenerator</argument>
65-
<argument key="matcher_class">Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher</argument>
65+
<argument key="matcher_class">Symfony\Bundle\FrameworkBundle\Routing\StaticUrlMatcher</argument>
6666
<argument key="matcher_base_class">Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher</argument>
67-
<argument key="matcher_dumper_class">Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper</argument>
67+
<argument key="matcher_dumper_class">Symfony\Component\Routing\Matcher\Dumper\StaticUrlMatcherDumper</argument>
6868
<argument key="matcher_cache_class">%router.cache_class_prefix%UrlMatcher</argument>
6969
</argument>
7070
<argument type="service" id="router.request_context" on-invalid="ignore" />

src/Symfony/Bundle/FrameworkBundle/Routing/RedirectableUrlMatcher.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Routing;
1313

14+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.1 and will be removed in 5.0. Use StaticUrlMatcher instead.', RedirectableUrlMatcher::class), E_USER_DEPRECATED);
15+
1416
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcher as BaseMatcher;
1517

1618
/**
1719
* @author Fabien Potencier <[email protected]>
20+
*
21+
* @deprecated since Symfony 4.1, to be removed in 5.0. Use StaticUrlMatcher instead.
1822
*/
1923
class RedirectableUrlMatcher extends BaseMatcher
2024
{
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Bundle\FrameworkBundle\Routing;
13+
14+
use Symfony\Component\Routing\Matcher\StaticUrlMatcher as BaseMatcher;
15+
16+
/**
17+
* @author Fabien Potencier <[email protected]>
18+
*/
19+
class StaticUrlMatcher extends BaseMatcher
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function redirect($path, $route, $scheme = null)
25+
{
26+
return array(
27+
'_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction',
28+
'path' => $path,
29+
'permanent' => true,
30+
'scheme' => $scheme,
31+
'httpPort' => $this->context->getHttpPort(),
32+
'httpsPort' => $this->context->getHttpsPort(),
33+
'_route' => $route,
34+
);
35+
}
36+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher;
1818
use Symfony\Component\Routing\RequestContext;
1919

20+
/**
21+
* @group legacy
22+
*/
2023
class RedirectableUrlMatcherTest extends TestCase
2124
{
2225
public function testRedirectWhenNoSlash()
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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\Bundle\FrameworkBundle\Tests\Routing;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Routing\Matcher\Dumper\StaticUrlMatcherDumper;
16+
use Symfony\Component\Routing\Route;
17+
use Symfony\Component\Routing\RouteCollection;
18+
use Symfony\Bundle\FrameworkBundle\Routing\StaticUrlMatcher;
19+
use Symfony\Component\Routing\RequestContext;
20+
21+
/**
22+
* @requires function \Symfony\Component\Routing\Matcher\StaticUrlMatcher::match
23+
*/
24+
class StaticUrlMatcherTest extends TestCase
25+
{
26+
public function testRedirectWhenNoSlash()
27+
{
28+
$routes = new RouteCollection();
29+
$routes->add('foo', new Route('/foo/'));
30+
31+
$matcher = $this->getMatcher($routes, $context = new RequestContext());
32+
33+
$this->assertEquals(array(
34+
'_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction',
35+
'path' => '/foo/',
36+
'permanent' => true,
37+
'scheme' => null,
38+
'httpPort' => $context->getHttpPort(),
39+
'httpsPort' => $context->getHttpsPort(),
40+
'_route' => 'foo',
41+
),
42+
$matcher->match('/foo')
43+
);
44+
}
45+
46+
public function testSchemeRedirect()
47+
{
48+
$routes = new RouteCollection();
49+
$routes->add('foo', new Route('/foo', array(), array(), array(), '', array('https')));
50+
51+
$matcher = $this->getMatcher($routes, $context = new RequestContext());
52+
53+
$this->assertEquals(array(
54+
'_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction',
55+
'path' => '/foo',
56+
'permanent' => true,
57+
'scheme' => 'https',
58+
'httpPort' => $context->getHttpPort(),
59+
'httpsPort' => $context->getHttpsPort(),
60+
'_route' => 'foo',
61+
),
62+
$matcher->match('/foo')
63+
);
64+
}
65+
66+
private function getMatcher(RouteCollection $routes, RequestContext $context)
67+
{
68+
$dumper = new StaticUrlMatcherDumper($routes);
69+
$path = sys_get_temp_dir().'/php_matcher.'.uniqid('StaticUrlMatcher').'.php';
70+
71+
file_put_contents($path, $dumper->dump());
72+
$matcher = new StaticUrlMatcher(require $path, $context);
73+
unlink($path);
74+
75+
return $matcher;
76+
}
77+
}

src/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111

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

14+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.1 and will be removed in 5.0. Use StaticUrlGeneratorDumper instead.', PhpGeneratorDumper::class), E_USER_DEPRECATED);
15+
1416
/**
1517
* PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.
1618
*
1719
* @author Fabien Potencier <[email protected]>
1820
* @author Tobias Schultze <http://tobion.de>
21+
*
22+
* @deprecated since Symfony 4.1, to be removed in 5.0. Use StaticUrlGeneratorDumper instead.
1923
*/
2024
class PhpGeneratorDumper extends GeneratorDumper
2125
{
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\StaticUrlMatcherDumper;
15+
16+
/**
17+
* StaticUrlGeneratorDumper creates a PHP array to be used with StaticUrlGenerator.
18+
*
19+
* @author Fabien Potencier <[email protected]>
20+
* @author Tobias Schultze <http://tobion.de>
21+
* @author Nicolas Grekas <[email protected]>
22+
*/
23+
class StaticUrlGeneratorDumper extends GeneratorDumper
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function dump(array $options = array())
29+
{
30+
return <<<EOF
31+
<?php
32+
33+
// This file has been auto-generated by the Symfony Routing Component.
34+
35+
return array({$this->generateDeclaredRoutes()}
36+
);
37+
38+
EOF;
39+
}
40+
41+
/**
42+
* Generates PHP code representing an array of defined routes
43+
* together with the routes properties (e.g. requirements).
44+
*/
45+
private function generateDeclaredRoutes(): string
46+
{
47+
$routes = '';
48+
foreach ($this->getRoutes()->all() as $name => $route) {
49+
$compiledRoute = $route->compile();
50+
51+
$properties = array();
52+
$properties[] = $compiledRoute->getVariables();
53+
$properties[] = $route->getDefaults();
54+
$properties[] = $route->getRequirements();
55+
$properties[] = $compiledRoute->getTokens();
56+
$properties[] = $compiledRoute->getHostTokens();
57+
$properties[] = $route->getSchemes();
58+
59+
$routes .= sprintf("\n '%s' => %s,", $name, StaticUrlMatcherDumper::export($properties));
60+
}
61+
62+
return $routes;
63+
}
64+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
class StaticUrlGenerator extends UrlGenerator
19+
{
20+
private $dumpedRoutes = array();
21+
22+
public function __construct(array $dumpedRoutes, RequestContext $context, LoggerInterface $logger = null)
23+
{
24+
$this->dumpedRoutes = $dumpedRoutes;
25+
$this->context = $context;
26+
$this->logger = $logger;
27+
}
28+
29+
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
30+
{
31+
if (!isset($this->dumpedRoutes[$name])) {
32+
throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
33+
}
34+
35+
list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes) = $this->dumpedRoutes[$name];
36+
37+
return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
38+
}
39+
}

src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

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

14+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.1 and will be removed in 5.0. Use StaticUrlMatcherDumper instead.', PhpMatcherDumper::class), E_USER_DEPRECATED);
15+
1416
use Symfony\Component\Routing\Route;
1517
use Symfony\Component\Routing\RouteCollection;
1618
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
@@ -22,6 +24,8 @@
2224
* @author Fabien Potencier <[email protected]>
2325
* @author Tobias Schultze <http://tobion.de>
2426
* @author Arnaud Le Blanc <[email protected]>
27+
*
28+
* @deprecated since Symfony 4.1, to be removed in 5.0. Use StaticUrlMatcherDumper instead.
2529
*/
2630
class PhpMatcherDumper extends MatcherDumper
2731
{

0 commit comments

Comments
 (0)