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

Skip to content

Commit 8bb5266

Browse files
committed
deprecate non string requirement names
1 parent 02daeb2 commit 8bb5266

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed

src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,16 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
142142
}
143143
$name = $globals['name'].$name;
144144

145+
$requirements = $annot->getRequirements();
146+
147+
foreach ($requirements as $placeholder => $requirement) {
148+
if (is_int($placeholder)) {
149+
@trigger_error(sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" of route "%s" in "%s::%s()"?', $placeholder, $requirement, $name, $class->getName(), $method->getName()), E_USER_DEPRECATED);
150+
}
151+
}
152+
145153
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
146-
$requirements = array_replace($globals['requirements'], $annot->getRequirements());
154+
$requirements = array_replace($globals['requirements'], $requirements);
147155
$options = array_replace($globals['options'], $annot->getOptions());
148156
$schemes = array_merge($globals['schemes'], $annot->getSchemes());
149157
$methods = array_merge($globals['methods'], $annot->getMethods());
@@ -305,6 +313,12 @@ protected function getGlobals(\ReflectionClass $class)
305313
if (null !== $annot->getCondition()) {
306314
$globals['condition'] = $annot->getCondition();
307315
}
316+
317+
foreach ($globals['requirements'] as $placeholder => $requirement) {
318+
if (is_int($placeholder)) {
319+
@trigger_error(sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" in "%s"?', $placeholder, $requirement, $class->getName()), E_USER_DEPRECATED);
320+
}
321+
}
308322
}
309323

310324
return $globals;

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
116116
$methods = isset($config['methods']) ? $config['methods'] : array();
117117
$condition = isset($config['condition']) ? $config['condition'] : null;
118118

119+
foreach ($requirements as $placeholder => $requirement) {
120+
if (is_int($placeholder)) {
121+
@trigger_error(sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" of route "%s" in "%s"?', $placeholder, $requirement, $name, $path), E_USER_DEPRECATED);
122+
}
123+
}
124+
119125
if (isset($config['controller'])) {
120126
$defaults['_controller'] = $config['controller'];
121127
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\Tests\Fixtures\AnnotationFixtures;
13+
14+
use Symfony\Component\Routing\Annotation\Route;
15+
16+
/**
17+
* @Route("/", requirements={"foo", "\d+"})
18+
*/
19+
class RequirementsWithoutPlaceholderNameController
20+
{
21+
/**
22+
* @Route("/{foo}", name="foo", requirements={"foo", "\d+"})
23+
*/
24+
public function foo()
25+
{
26+
}
27+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
foo:
2+
path: '/{foo}'
3+
requirements:
4+
- '\d+'

src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\NothingButNameController;
3434
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\PrefixedActionLocalizedRouteController;
3535
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\PrefixedActionPathController;
36+
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController;
3637
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RouteWithPrefixController;
3738

3839
class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
@@ -87,6 +88,18 @@ public function testSimplePathRoute()
8788
$this->assertEquals('/path', $routes->get('action')->getPath());
8889
}
8990

91+
/**
92+
* @group legacy
93+
* @expectedDeprecation A placeholder name must be a string (0 given). Did you forget to specify the placeholder key for the requirement "foo" in "Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController"?
94+
* @expectedDeprecation A placeholder name must be a string (1 given). Did you forget to specify the placeholder key for the requirement "\d+" in "Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController"?
95+
* @expectedDeprecation A placeholder name must be a string (0 given). Did you forget to specify the placeholder key for the requirement "foo" of route "foo" in "Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController::foo()"?
96+
* @expectedDeprecation A placeholder name must be a string (1 given). Did you forget to specify the placeholder key for the requirement "\d+" of route "foo" in "Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController::foo()"?
97+
*/
98+
public function testRequirementsWithoutPlaceholderName()
99+
{
100+
$this->loader->load(RequirementsWithoutPlaceholderNameController::class);
101+
}
102+
90103
public function testInvokableControllerLoader()
91104
{
92105
$routes = $this->loader->load(InvokableController::class);

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,14 @@ public function testImportRouteWithNoTrailingSlash()
304304
$this->assertEquals('/slash/', $routeCollection->get('a_app_homepage')->getPath());
305305
$this->assertEquals('/no-slash', $routeCollection->get('b_app_homepage')->getPath());
306306
}
307+
308+
/**
309+
* @group legacy
310+
* @expectedDeprecation A placeholder name must be a string (0 given). Did you forget to specify the placeholder key for the requirement "\d+" of route "foo" in "%srequirements_without_placeholder_name.yml"?
311+
*/
312+
public function testRequirementsWithoutPlaceholderName()
313+
{
314+
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
315+
$loader->load('requirements_without_placeholder_name.yml');
316+
}
307317
}

0 commit comments

Comments
 (0)