Skip to content

Commit b5c6b06

Browse files
acrobatdbu
authored andcommitted
Symfony 5 compatibility and deprecation of routename as object
1 parent d9450c6 commit b5c6b06

10 files changed

+74
-24
lines changed

.travis.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
language: php
1717

1818
php:
19-
- 7.1
2019
- 7.2
2120
- 7.3
2221

@@ -36,12 +35,12 @@ env:
3635

3736
matrix:
3837
include:
39-
- php: 7.1
40-
env: COMPOSER_FLAGS="--prefer-lowest" SYMFONY_REQUIRE=3.4.* SYMFONY_DEPRECATIONS_HELPER=weak
41-
- php: 7.4
42-
env: SYMFONY_REQUIRE=4.3.*
38+
- php: 7.2
39+
env: COMPOSER_FLAGS="--prefer-lowest" SYMFONY_REQUIRE=4.4.* SYMFONY_DEPRECATIONS_HELPER=weak
4340
- php: 7.4
4441
env: SYMFONY_REQUIRE=4.4.*
42+
- php: 7.4
43+
env: SYMFONY_REQUIRE=5.0.*
4544
fast_finish: true
4645
allow_failures:
4746

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
}
1616
],
1717
"require": {
18-
"php": "^7.1",
19-
"symfony/routing": "^3.4 || ^4.3",
20-
"symfony/http-kernel": "^3.4 || ^4.3",
18+
"php": "^7.2",
19+
"symfony/routing": "^4.4 || ^5.0",
20+
"symfony/http-kernel": "^4.4 || ^5.0",
2121
"psr/log": "^1.0"
2222
},
2323
"require-dev": {
24-
"symfony/phpunit-bridge": "^4.2.2",
25-
"symfony/dependency-injection": "^3.4 || ^4.3",
26-
"symfony/config": "^3.4 || ^4.3",
27-
"symfony/event-dispatcher": "^3.4 || ^4.3",
24+
"symfony/phpunit-bridge": "^5.0",
25+
"symfony/dependency-injection": "^4.4 || ^5.0",
26+
"symfony/config": "^4.4 || ^5.0",
27+
"symfony/event-dispatcher": "^4.4 || ^5.0",
2828
"symfony-cmf/testing": "^3@dev"
2929
},
3030
"suggest": {
31-
"symfony/event-dispatcher": "DynamicRouter can optionally trigger an event at the start of matching. Minimal version (^3.4 || ^4.3)"
31+
"symfony/event-dispatcher": "DynamicRouter can optionally trigger an event at the start of matching. Minimal version (^4.4 || ^5.0)"
3232
},
3333
"autoload": {
3434
"psr-4": {

src/ChainRouter.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ private function doMatch($pathinfo, Request $request = null)
218218
*/
219219
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
220220
{
221+
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);
223+
}
224+
221225
$debug = [];
222226

223227
foreach ($this->all() as $router) {
@@ -227,8 +231,19 @@ public function generate($name, $parameters = [], $absolute = UrlGeneratorInterf
227231
continue;
228232
}
229233

234+
// if $router does not announce it is capable of handling
235+
// non-string routes and the ROUTE_OBJECT is set in the parameters array, continue
236+
if (array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && is_object($parameters[RouteObjectInterface::ROUTE_OBJECT]) && !$router instanceof VersatileGeneratorInterface) {
237+
continue;
238+
}
239+
240+
$routeName = $name;
241+
if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name && array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && is_object($parameters[RouteObjectInterface::ROUTE_OBJECT])) {
242+
$routeName = $parameters[RouteObjectInterface::ROUTE_OBJECT];
243+
}
244+
230245
// If $router is versatile and doesn't support this route name, continue
231-
if ($router instanceof VersatileGeneratorInterface && !$router->supports($name)) {
246+
if ($router instanceof VersatileGeneratorInterface && !$router->supports($routeName)) {
232247
continue;
233248
}
234249

src/ContentAwareGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public function generate($name, $parameters = [], $absolute = UrlGeneratorInterf
7070
{
7171
if ($name instanceof SymfonyRoute) {
7272
$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);
7375
} elseif (is_string($name) && $name) {
7476
$route = $this->getRouteByName($name, $parameters);
7577
} else {
@@ -165,6 +167,8 @@ protected function getRouteByContent($name, &$parameters)
165167
{
166168
if ($name instanceof RouteReferrersReadInterface) {
167169
$content = $name;
170+
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name && array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof RouteReferrersReadInterface) {
171+
$content = $parameters[RouteObjectInterface::ROUTE_OBJECT];
168172
} elseif (array_key_exists('content_id', $parameters)
169173
&& null !== $this->contentRepository
170174
) {

src/DynamicRouter.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,13 @@ public function getGenerator()
174174
*/
175175
public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
176176
{
177+
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);
179+
}
180+
177181
if ($this->eventDispatcher) {
178182
$event = new RouterGenerateEvent($name, $parameters, $referenceType);
179-
$this->eventDispatcher->dispatch(Events::PRE_DYNAMIC_GENERATE, $event);
183+
$this->eventDispatcher->dispatch($event, Events::PRE_DYNAMIC_GENERATE);
180184
$name = $event->getRoute();
181185
$parameters = $event->getParameters();
182186
$referenceType = $event->getReferenceType();
@@ -225,7 +229,7 @@ public function match($pathinfo)
225229
$request = Request::create($pathinfo);
226230
if ($this->eventDispatcher) {
227231
$event = new RouterMatchEvent();
228-
$this->eventDispatcher->dispatch(Events::PRE_DYNAMIC_MATCH, $event);
232+
$this->eventDispatcher->dispatch($event, Events::PRE_DYNAMIC_MATCH);
229233
}
230234

231235
if (!empty($this->uriFilterRegexp) && !preg_match($this->uriFilterRegexp, $pathinfo)) {
@@ -261,7 +265,7 @@ public function matchRequest(Request $request)
261265
{
262266
if ($this->eventDispatcher) {
263267
$event = new RouterMatchEvent($request);
264-
$this->eventDispatcher->dispatch(Events::PRE_DYNAMIC_MATCH_REQUEST, $event);
268+
$this->eventDispatcher->dispatch($event, Events::PRE_DYNAMIC_MATCH_REQUEST);
265269
}
266270

267271
if ($this->uriFilterRegexp

src/Event/RouterGenerateEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Cmf\Component\Routing\Event;
1313

14-
use Symfony\Component\EventDispatcher\Event;
14+
use Symfony\Contracts\EventDispatcher\Event;
1515
use Symfony\Component\Routing\Route;
1616

1717
/**

src/Event/RouterMatchEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Cmf\Component\Routing\Event;
1313

14-
use Symfony\Component\EventDispatcher\Event;
14+
use Symfony\Contracts\EventDispatcher\Event;
1515
use Symfony\Component\HttpFoundation\Request;
1616

1717
class RouterMatchEvent extends Event

src/RouteObjectInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ interface RouteObjectInterface
5656
*/
5757
const CONTENT_ID = '_content_id';
5858

59+
/**
60+
* Route name used when passing a route object to the generator in $parameters[RouteObjectInterface::ROUTE_OBJECT].
61+
*/
62+
const OBJECT_BASED_ROUTE_NAME = 'cmf_routing_object';
63+
5964
/**
6065
* Get the content document this route entry stands for. If non-null,
6166
* the ControllerClassMapper uses it to identify a controller and

tests/Unit/Routing/ChainRouterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
2323
use Symfony\Component\Routing\Exception\RouteNotFoundException;
2424
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
25+
use Symfony\Component\Routing\Loader\ObjectRouteLoader;
2526
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
2627
use Symfony\Component\Routing\RequestContext;
2728
use Symfony\Component\Routing\Route;
@@ -613,9 +614,16 @@ public function testGenerateNotFound()
613614

614615
/**
615616
* Route is an object but no versatile generator around to do the debug message.
617+
*
618+
* @group legacy
619+
* @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.
616620
*/
617621
public function testGenerateObjectNotFound()
618622
{
623+
if (!class_exists(ObjectRouteLoader::class)) {
624+
$this->markTestSkipped('Skip this test on >= sf5. This will throw a \TypeError.');
625+
}
626+
619627
$name = new \stdClass();
620628
$parameters = ['test' => 'value'];
621629

@@ -634,9 +642,16 @@ public function testGenerateObjectNotFound()
634642

635643
/**
636644
* A versatile router will generate the debug message.
645+
*
646+
* @group legacy
647+
* @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.
637648
*/
638649
public function testGenerateObjectNotFoundVersatile()
639650
{
651+
if (!class_exists(ObjectRouteLoader::class)) {
652+
$this->markTestSkipped('Skip this test on >= sf5. This will throw a \TypeError.');
653+
}
654+
640655
$name = new \stdClass();
641656
$parameters = ['test' => 'value'];
642657

@@ -663,8 +678,16 @@ public function testGenerateObjectNotFoundVersatile()
663678
$this->router->generate($name, $parameters);
664679
}
665680

681+
/**
682+
* @group legacy
683+
* @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+
*/
666685
public function testGenerateObjectName()
667686
{
687+
if (!class_exists(ObjectRouteLoader::class)) {
688+
$this->markTestSkipped('Skip this test on >= sf5. This will throw a \TypeError.');
689+
}
690+
668691
$name = new \stdClass();
669692
$parameters = ['test' => 'value'];
670693

tests/Unit/Routing/DynamicRouterTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public function testEventHandler()
338338

339339
$eventDispatcher->expects($this->once())
340340
->method('dispatch')
341-
->with(Events::PRE_DYNAMIC_MATCH, $this->equalTo(new RouterMatchEvent()))
341+
->with($this->equalTo(new RouterMatchEvent()), Events::PRE_DYNAMIC_MATCH)
342342
;
343343

344344
$routeDefaults = ['foo' => 'bar'];
@@ -359,12 +359,12 @@ public function testEventHandlerRequest()
359359
$that = $this;
360360
$eventDispatcher->expects($this->once())
361361
->method('dispatch')
362-
->with(Events::PRE_DYNAMIC_MATCH_REQUEST, $this->callback(function ($event) use ($that) {
362+
->with($this->callback(function ($event) use ($that) {
363363
$that->assertInstanceOf(RouterMatchEvent::class, $event);
364364
$that->assertEquals($that->request, $event->getRequest());
365365

366366
return true;
367-
}))
367+
}), Events::PRE_DYNAMIC_MATCH_REQUEST)
368368
;
369369

370370
$routeDefaults = ['foo' => 'bar'];
@@ -392,7 +392,7 @@ public function testEventHandlerGenerate()
392392
$that = $this;
393393
$eventDispatcher->expects($this->once())
394394
->method('dispatch')
395-
->with(Events::PRE_DYNAMIC_GENERATE, $this->callback(function ($event) use ($that, $oldname, $newname, $oldparameters, $newparameters, $oldReferenceType, $newReferenceType) {
395+
->with($this->callback(function ($event) use ($that, $oldname, $newname, $oldparameters, $newparameters, $oldReferenceType, $newReferenceType) {
396396
$that->assertInstanceOf(RouterGenerateEvent::class, $event);
397397
if (empty($that->seen)) {
398398
// phpunit is calling the callback twice, and because we update the event the second time fails
@@ -408,7 +408,7 @@ public function testEventHandlerGenerate()
408408
$event->setReferenceType($newReferenceType);
409409

410410
return true;
411-
}))
411+
}), Events::PRE_DYNAMIC_GENERATE)
412412
;
413413

414414
$this->generator->expects($this->once())

0 commit comments

Comments
 (0)