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

Skip to content

[Routing] dump static arrays instead of classes for both matcher and generator #25909

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

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ return PhpCsFixer\Config::create()
->notPath('Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/default.phpt')
->notPath('Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/weak.phpt')
->notPath('Symfony/Component/Debug/Tests/DebugClassLoaderTest.php')
// file autogenerated
->notPath('Symfony/Component/Routing/Tests/Fixtures/dumper/static_url_matcher3.php')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dunno why this is ignored by fabbot, which raises a false positive for now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does fabbot use a separate config file maybe?

)
;
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
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\Dumper\PhpMatcherDumper;
use Symfony\Component\Routing\Matcher\StaticUrlMatcher;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
Expand Down Expand Up @@ -642,6 +645,12 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
if (isset($config['type'])) {
$argument['resource_type'] = $config['type'];
}
if (!class_exists(StaticUrlMatcher::class)) {
$argument['generator_class'] = $argument['generator_base_class'];
$argument['generator_dumper_class'] = PhpGeneratorDumper::class;
$argument['matcher_class'] = $argument['matcher_base_class'];
$argument['matcher_dumper_class'] = PhpMatcherDumper::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 @@ -58,13 +58,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\StaticUrlGenerator</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\StaticUrlGeneratorDumper</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\RedirectableStaticUrlMatcher</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\StaticUrlMatcherDumper</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,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\RedirectableUrlMatcherInterface;
use Symfony\Component\Routing\Matcher\StaticUrlMatcher;

/**
* @author Fabien Potencier <[email protected]>
*/
class RedirectableStaticUrlMatcher extends StaticUrlMatcher implements RedirectableUrlMatcherInterface
{
/**
* {@inheritdoc}
*/
public function redirect($path, $route, $scheme = null)
{
return array(
'_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 deprecated since Symfony 4.1 and will be removed in 5.0. Use RedirectableStaticUrlMatcher instead.', RedirectableUrlMatcher::class), E_USER_DEPRECATED);

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

/**
* @author Fabien Potencier <[email protected]>
*
* @deprecated since Symfony 4.1, to be removed in 5.0. Use RedirectableStaticUrlMatcher instead.
*/
class RedirectableUrlMatcher extends BaseMatcher
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Component\Routing\Matcher\Dumper\StaticUrlMatcherDumper;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableStaticUrlMatcher;
use Symfony\Component\Routing\RequestContext;

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

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

$this->assertEquals(array(
'_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', array(), array(), array(), '', array('https')));

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

$this->assertEquals(array(
'_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 StaticUrlMatcherDumper($routes);
$path = sys_get_temp_dir().'/php_matcher.'.uniqid('StaticUrlMatcher').'.php';

file_put_contents($path, $dumper->dump());
$matcher = new RedirectableStaticUrlMatcher(require $path, $context);
unlink($path);

return $matcher;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher;
use Symfony\Component\Routing\RequestContext;

/**
* @group legacy
*/
class RedirectableUrlMatcherTest extends TestCase
{
public function testRedirectWhenNoSlash()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@

namespace Symfony\Component\Routing\Generator\Dumper;

@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);

/**
* PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes.
*
* @author Fabien Potencier <[email protected]>
* @author Tobias Schultze <http://tobion.de>
*
* @deprecated since Symfony 4.1, to be removed in 5.0. Use StaticUrlGeneratorDumper instead.
*/
class PhpGeneratorDumper extends GeneratorDumper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\StaticUrlMatcherDumper;

/**
* StaticUrlGeneratorDumper creates a PHP array to be used with StaticUrlGenerator.
*
* @author Fabien Potencier <[email protected]>
* @author Tobias Schultze <http://tobion.de>
* @author Nicolas Grekas <[email protected]>
*/
class StaticUrlGeneratorDumper extends GeneratorDumper
{
/**
* {@inheritdoc}
*/
public function dump(array $options = array())
{
return <<<EOF
<?php

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

return array({$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->getRoutes()->all() as $name => $route) {
$compiledRoute = $route->compile();

$properties = array();
$properties[] = $compiledRoute->getVariables();
$properties[] = $route->getDefaults();
$properties[] = $route->getRequirements();
$properties[] = $compiledRoute->getTokens();
$properties[] = $compiledRoute->getHostTokens();
$properties[] = $route->getSchemes();

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

return $routes;
}
}
39 changes: 39 additions & 0 deletions src/Symfony/Component/Routing/Generator/StaticUrlGenerator.php
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 <[email protected]>
*
* 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;

class StaticUrlGenerator extends UrlGenerator
{
private $dumpedRoutes = array();

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

public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
{
if (!isset($this->dumpedRoutes[$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->dumpedRoutes[$name];

return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Routing\Matcher\Dumper;

@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);

use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
Expand All @@ -22,6 +24,8 @@
* @author Fabien Potencier <[email protected]>
* @author Tobias Schultze <http://tobion.de>
* @author Arnaud Le Blanc <[email protected]>
*
* @deprecated since Symfony 4.1, to be removed in 5.0. Use StaticUrlMatcherDumper instead.
*/
class PhpMatcherDumper extends MatcherDumper
{
Expand Down
Loading