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

Skip to content

Commit 56ebc5b

Browse files
author
Jelte Steijaert
committed
Bug #16343 [Router] Too many Routes ?
1 parent e2022ce commit 56ebc5b

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,45 @@ public function dump(array $options = array())
3838
'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
3939
), $options);
4040

41+
$routes = $this->generateDeclaredRoutes();
42+
43+
// Fix for https://bugs.php.net/bug.php?id=68057 - Bug #68057 Incorrect parsing of big arrays in PHP 5.6.0
44+
if (PHP_VERSION_ID >= 50600 && PHP_VERSION_ID < 50700 && $this->getRoutes()->count() > 32769) {
45+
return <<<EOF
46+
<?php
47+
48+
use Symfony\Component\Routing\RequestContext;
49+
use Symfony\Component\Routing\Exception\RouteNotFoundException;
50+
use Psr\Log\LoggerInterface;
51+
52+
/**
53+
* {$options['class']}
54+
*
55+
* This class has been auto-generated
56+
* by the Symfony Routing Component.
57+
*/
58+
class {$options['class']} extends {$options['base_class']}
59+
{
60+
private static \$declaredRoutes;
61+
62+
/**
63+
* Constructor.
64+
*/
65+
public function __construct(RequestContext \$context, LoggerInterface \$logger = null)
66+
{
67+
\$this->context = \$context;
68+
\$this->logger = \$logger;
69+
if ( null === self::\$declaredRoutes ) {
70+
self::\$declaredRoutes = {$routes};
71+
}
72+
}
73+
74+
{$this->generateGenerateMethod()}
75+
}
76+
77+
EOF;
78+
}
79+
4180
return <<<EOF
4281
<?php
4382

src/Symfony/Component/Routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,21 @@ class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
3434
*/
3535
private $testTmpFilepath;
3636

37+
/**
38+
* @var string
39+
*/
40+
private $largeTestTmpFilepath;
41+
3742
protected function setUp()
3843
{
3944
parent::setUp();
4045

4146
$this->routeCollection = new RouteCollection();
4247
$this->generatorDumper = new PhpGeneratorDumper($this->routeCollection);
4348
$this->testTmpFilepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_generator.'.$this->getName().'.php';
49+
$this->largeTestTmpFilepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_generator.'.$this->getName().'.large.php';
4450
@unlink($this->testTmpFilepath);
51+
@unlink($this->largeTestTmpFilepath);
4552
}
4653

4754
protected function tearDown()
@@ -76,6 +83,33 @@ public function testDumpWithRoutes()
7683
$this->assertEquals($relativeUrlWithoutParameter, '/app.php/testing2');
7784
}
7885

86+
public function testDumpWithTooManyRoutes()
87+
{
88+
$this->routeCollection->add('Test', new Route('/testing/{foo}'));
89+
for ( $i = 0; $i < 32769; $i++ ) {
90+
$this->routeCollection->add('route_'.$i, new Route('/route_'.$i));
91+
}
92+
$this->routeCollection->add('Test2', new Route('/testing2'));
93+
94+
$data = $this->generatorDumper->dump(array(
95+
'class' => 'ProjectLargeUrlGenerator'
96+
));
97+
file_put_contents($this->largeTestTmpFilepath, $data);
98+
include $this->largeTestTmpFilepath;
99+
100+
$projectUrlGenerator = new \ProjectLargeUrlGenerator(new RequestContext('/app.php'));
101+
102+
$absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_URL);
103+
$absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_URL);
104+
$relativeUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_PATH);
105+
$relativeUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
106+
107+
$this->assertEquals($absoluteUrlWithParameter, 'http://localhost/app.php/testing/bar');
108+
$this->assertEquals($absoluteUrlWithoutParameter, 'http://localhost/app.php/testing2');
109+
$this->assertEquals($relativeUrlWithParameter, '/app.php/testing/bar');
110+
$this->assertEquals($relativeUrlWithoutParameter, '/app.php/testing2');
111+
}
112+
79113
/**
80114
* @expectedException \InvalidArgumentException
81115
*/

0 commit comments

Comments
 (0)