Skip to content

Commit c969140

Browse files
committed
more places to support the route object parameter
1 parent 22c0940 commit c969140

9 files changed

+158
-33
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Changelog
1414
name `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` (`cmf_routing_object`)
1515
and pass the route object in the parameters with key
1616
`RouteObjectInterface::ROUTE_OBJECT` (`_route_object`).
17-
* The VersatileGeneratorInterface is deprecated as it was used to avoid errors
18-
with routers not supporting objects in `$name`.
17+
* The VersatileGeneratorInterface::supports method is deprecated as it was used
18+
to avoid errors with routers not supporting objects in `$name`.
1919

2020
2.2.0
2121
-----

src/ChainRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private function doMatch($pathinfo, Request $request = null)
219219
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
220220
{
221221
if (is_object($name)) {
222-
@trigger_error(sprintf('Passing an object as the route name is deprecated in symfony-cmf/Routing v2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` constant as the route name and the object as "%s" parameter in the parameters array.', RouteObjectInterface::ROUTE_OBJECT), E_USER_DEPRECATED);
222+
@trigger_error('Passing an object as route name is deprecated since version 2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.', E_USER_DEPRECATED);
223223
}
224224

225225
$debug = [];

src/ContentAwareGenerator.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ public function setContentRepository(ContentRepositoryInterface $contentReposito
6969
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
7070
{
7171
if ($name instanceof SymfonyRoute) {
72+
@trigger_error('Passing an object as route name is deprecated since version 2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.', E_USER_DEPRECATED);
73+
7274
$route = $this->getBestLocaleRoute($name, $parameters);
73-
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name && array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute) {
74-
$route = $this->getBestLocaleRoute($parameters[RouteObjectInterface::ROUTE_OBJECT], $parameters);
75+
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name) {
76+
if (array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute) {
77+
$route = $this->getBestLocaleRoute($parameters[RouteObjectInterface::ROUTE_OBJECT], $parameters);
78+
} else {
79+
$route = $this->getRouteByContent($name, $parameters);
80+
}
7581
} elseif (is_string($name) && $name) {
7682
$route = $this->getRouteByName($name, $parameters);
7783
} else {
@@ -166,8 +172,13 @@ protected function getBestLocaleRoute(SymfonyRoute $route, $parameters)
166172
protected function getRouteByContent($name, &$parameters)
167173
{
168174
if ($name instanceof RouteReferrersReadInterface) {
175+
@trigger_error('Passing an object as route name is deprecated since version 2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.', E_USER_DEPRECATED);
176+
169177
$content = $name;
170-
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name && array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof RouteReferrersReadInterface) {
178+
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
179+
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
180+
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof RouteReferrersReadInterface
181+
) {
171182
$content = $parameters[RouteObjectInterface::ROUTE_OBJECT];
172183
} elseif (array_key_exists('content_id', $parameters)
173184
&& null !== $this->contentRepository
@@ -294,10 +305,20 @@ public function supports($name)
294305
*/
295306
public function getRouteDebugMessage($name, array $parameters = [])
296307
{
297-
if (!$name && array_key_exists('content_id', $parameters)) {
308+
if ((!$name || RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name)
309+
&& array_key_exists('content_id', $parameters)
310+
) {
298311
return 'Content id '.$parameters['content_id'];
299312
}
300313

314+
if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
315+
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
316+
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof RouteReferrersReadInterface
317+
) {
318+
return 'Route aware content '.parent::getRouteDebugMessage($name, $parameters);
319+
}
320+
321+
// legacy
301322
if ($name instanceof RouteReferrersReadInterface) {
302323
return 'Route aware content '.parent::getRouteDebugMessage($name, $parameters);
303324
}

src/DynamicRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function getGenerator()
175175
public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
176176
{
177177
if (is_object($name)) {
178-
@trigger_error(sprintf('Passing an object as the route name is deprecated in symfony-cmf/Routing v2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` constant as the route name and the object as "%s" parameter in the parameters array.', RouteObjectInterface::ROUTE_OBJECT), E_USER_DEPRECATED);
178+
@trigger_error('Passing an object as route name is deprecated since version 2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT', E_USER_DEPRECATED);
179179
}
180180

181181
if ($this->eventDispatcher) {

src/ProviderBasedGenerator.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,30 @@ public function supports($name)
7777
*/
7878
public function getRouteDebugMessage($name, array $parameters = [])
7979
{
80+
if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
81+
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
82+
) {
83+
$routeObject = $parameters[RouteObjectInterface::ROUTE_OBJECT];
84+
if ($routeObject instanceof RouteObjectInterface) {
85+
return 'Route with key '.$routeObject->getRouteKey();
86+
}
87+
88+
if ($routeObject instanceof SymfonyRoute) {
89+
return 'Route with path '.$routeObject->getPath();
90+
}
91+
92+
if (is_object($routeObject)) {
93+
return get_class($routeObject);
94+
}
95+
96+
return 'Null route';
97+
}
98+
8099
if (is_scalar($name)) {
81100
return $name;
82101
}
83102

103+
// legacy
84104
if (is_array($name)) {
85105
return serialize($name);
86106
}

src/VersatileGeneratorInterface.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1515

1616
/**
17-
* This generator is able to handle more than string route names as symfony
18-
* core supports them.
19-
*
20-
* @deprecated The "Symfony\Cmf\Component\Routing\VersatileGeneratorInterface" is deprecated in symfony-cmf/Routing v2.3 and will be removed in symfony-cmf/Routing v3.O. Use the "_route_object" parameter instead to handle route objects
17+
* This generator can provide additional information about the route that we wanted to generate.
2118
*/
2219
interface VersatileGeneratorInterface extends UrlGeneratorInterface
2320
{
@@ -28,6 +25,13 @@ interface VersatileGeneratorInterface extends UrlGeneratorInterface
2825
* resolved to a route, only whether the router can generate routes from
2926
* objects of this class.
3027
*
28+
* @deprecated This method is deprecated since version 2.3 and will be
29+
* removed in version 3.O.
30+
*
31+
* This method was used to not call generators that can not handle objects
32+
* in $name. With Symfony 5, this becomes obsolete as the strict type
33+
* declaration prevents passing anything else than a string as $name.
34+
*
3135
* @param mixed $name The route "name" which may also be an object or anything
3236
*
3337
* @return bool
@@ -38,9 +42,8 @@ public function supports($name);
3842
* Convert a route identifier (name, content object etc) into a string
3943
* usable for logging and other debug/error messages.
4044
*
41-
* @param mixed $name
42-
* @param array $parameters which should contain a content field containing
43-
* a RouteReferrersReadInterface object
45+
* @param mixed $name In Symfony 5, the name can only be a string
46+
* @param array $parameters Which might hold a route object or content id or similar to include in the debug message
4447
*
4548
* @return string
4649
*/

tests/Unit/Routing/ChainRouterTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -617,12 +617,12 @@ public function testGenerateNotFound()
617617
* Route is an object but no versatile generator around to do the debug message.
618618
*
619619
* @group legacy
620-
* @expectedDeprecation Passing an object as the route name is deprecated in symfony-cmf/Routing v2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` constant as the route name and the object as "_route_object" parameter in the parameters array.
620+
* @expectedDeprecation Passing an object as route name is deprecated since version 2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.
621621
*/
622622
public function testGenerateObjectNotFound()
623623
{
624624
if (!class_exists(ObjectRouteLoader::class)) {
625-
$this->markTestSkipped('Skip this test on >= sf5. This will throw a \TypeError.');
625+
$this->markTestSkipped('Symfony 5 would throw a TypeError.');
626626
}
627627

628628
$name = new \stdClass();
@@ -645,12 +645,12 @@ public function testGenerateObjectNotFound()
645645
* A versatile router will generate the debug message.
646646
*
647647
* @group legacy
648-
* @expectedDeprecation Passing an object as the route name is deprecated in symfony-cmf/Routing v2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` constant as the route name and the object as "_route_object" parameter in the parameters array.
648+
* @expectedDeprecation Passing an object as route name is deprecated since version 2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.
649649
*/
650650
public function testGenerateObjectNotFoundVersatile()
651651
{
652652
if (!class_exists(ObjectRouteLoader::class)) {
653-
$this->markTestSkipped('Skip this test on >= sf5. This will throw a \TypeError.');
653+
$this->markTestSkipped('Symfony 5 would throw a TypeError.');
654654
}
655655

656656
$name = new \stdClass();
@@ -681,12 +681,12 @@ public function testGenerateObjectNotFoundVersatile()
681681

682682
/**
683683
* @group legacy
684-
* @expectedDeprecation Passing an object as the route name is deprecated in symfony-cmf/Routing v2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` constant as the route name and the object as "_route_object" parameter in the parameters array.
684+
* @expectedDeprecation Passing an object as route name is deprecated since version 2.3 and will not work in Symfony 5.0. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.
685685
*/
686686
public function testGenerateObjectName()
687687
{
688688
if (!class_exists(ObjectRouteLoader::class)) {
689-
$this->markTestSkipped('Skip this test on >= sf5. This will throw a \TypeError.');
689+
$this->markTestSkipped('Symfony 5 would throw a TypeError.');
690690
}
691691

692692
$name = new \stdClass();
@@ -718,6 +718,9 @@ public function testGenerateObjectName()
718718
$this->assertEquals($name, $result);
719719
}
720720

721+
/**
722+
* This test currently triggers a deprecation notice because of ChainRouter BC.
723+
*/
721724
public function testGenerateWithObjectNameInParametersNotFoundVersatile()
722725
{
723726
$name = RouteObjectInterface::OBJECT_BASED_ROUTE_NAME;
@@ -757,13 +760,13 @@ public function testGenerateWithObjectNameInParameters()
757760
->expects($this->once())
758761
->method('generate')
759762
->with($name, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH)
760-
->willReturn($name)
763+
->willReturn('/foo/bar')
761764
;
762765

763766
$this->router->add($defaultRouter, 200);
764767

765768
$result = $this->router->generate($name, $parameters);
766-
$this->assertEquals($name, $result);
769+
$this->assertEquals('/foo/bar', $result);
767770
}
768771

769772
public function testWarmup()
@@ -817,6 +820,9 @@ public function testRouteCollection()
817820
$this->assertEquals(['high', 'low'], $names);
818821
}
819822

823+
/**
824+
* @group legacy
825+
*/
820826
public function testSupport()
821827
{
822828
$router = $this->createMock(VersatileRouter::class);

0 commit comments

Comments
 (0)