Skip to content

Commit 4f21849

Browse files
authored
Merge pull request #250 from symfony-cmf/acrobat-symfony5-support-2
Symfony 5 support
2 parents d9450c6 + c969140 commit 4f21849

15 files changed

+292
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
phpunit.xml
22
composer.lock
33
/vendor/
4+
.phpunit.result.cache

.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

CHANGELOG.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
Changelog
22
=========
33

4+
2.3.0
5+
-----
6+
7+
* Dropped support for PHP 7.1 and Symfony 3.4 and 4.3.
8+
* Added support for Symfony 5.
9+
* Deprecated passing a route object (or anything else that is not a string) as
10+
the `$name` parameter in the `generate` method of the ChainRouter and the
11+
DynamicRouter. Symfony 5 enforces the `$name` parameter to be a string with
12+
static type declaration.
13+
The future proof way to generate a route from an object is to use the route
14+
name `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` (`cmf_routing_object`)
15+
and pass the route object in the parameters with key
16+
`RouteObjectInterface::ROUTE_OBJECT` (`_route_object`).
17+
* The VersatileGeneratorInterface::supports method is deprecated as it was used
18+
to avoid errors with routers not supporting objects in `$name`.
19+
20+
2.2.0
21+
-----
22+
23+
* Avoid Symfony 4.3 event dispatcher deprecation warnings.
24+
425
2.1.1
526
-----
627

@@ -36,7 +57,7 @@ Released.
3657
---------
3758

3859
* **2016-11-30**: Changed file structure to have all php code in src/
39-
60+
4061
1.4.0
4162
-----
4263

composer.json

Lines changed: 9 additions & 9 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": {
@@ -42,7 +42,7 @@
4242
},
4343
"extra": {
4444
"branch-alias": {
45-
"dev-master": "2.2-dev"
45+
"dev-master": "2.x-dev"
4646
}
4747
}
4848
}

src/ChainRouter.php

Lines changed: 4 additions & 0 deletions
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('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);
223+
}
224+
221225
$debug = [];
222226

223227
foreach ($this->all() as $router) {

src/ContentAwareGenerator.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +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);
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+
}
7381
} elseif (is_string($name) && $name) {
7482
$route = $this->getRouteByName($name, $parameters);
7583
} else {
@@ -164,7 +172,14 @@ protected function getBestLocaleRoute(SymfonyRoute $route, $parameters)
164172
protected function getRouteByContent($name, &$parameters)
165173
{
166174
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+
167177
$content = $name;
178+
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
179+
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
180+
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof RouteReferrersReadInterface
181+
) {
182+
$content = $parameters[RouteObjectInterface::ROUTE_OBJECT];
168183
} elseif (array_key_exists('content_id', $parameters)
169184
&& null !== $this->contentRepository
170185
) {
@@ -290,10 +305,20 @@ public function supports($name)
290305
*/
291306
public function getRouteDebugMessage($name, array $parameters = [])
292307
{
293-
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+
) {
294311
return 'Content id '.$parameters['content_id'];
295312
}
296313

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
297322
if ($name instanceof RouteReferrersReadInterface) {
298323
return 'Route aware content '.parent::getRouteDebugMessage($name, $parameters);
299324
}

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('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);
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,8 +11,8 @@
1111

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

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

1717
/**
1818
* Event fired before the dynamic router generates a url for a route.

src/Event/RouterMatchEvent.php

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

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

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

1717
class RouterMatchEvent extends Event
1818
{

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/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

src/VersatileGeneratorInterface.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +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.
17+
* This generator can provide additional information about the route that we wanted to generate.
1918
*/
2019
interface VersatileGeneratorInterface extends UrlGeneratorInterface
2120
{
@@ -26,6 +25,13 @@ interface VersatileGeneratorInterface extends UrlGeneratorInterface
2625
* resolved to a route, only whether the router can generate routes from
2726
* objects of this class.
2827
*
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+
*
2935
* @param mixed $name The route "name" which may also be an object or anything
3036
*
3137
* @return bool
@@ -36,9 +42,8 @@ public function supports($name);
3642
* Convert a route identifier (name, content object etc) into a string
3743
* usable for logging and other debug/error messages.
3844
*
39-
* @param mixed $name
40-
* @param array $parameters which should contain a content field containing
41-
* 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
4247
*
4348
* @return string
4449
*/

0 commit comments

Comments
 (0)