Skip to content

Commit

Permalink
more places to support the route object parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Apr 20, 2020
1 parent 22c0940 commit c969140
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 33 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Changelog
name `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` (`cmf_routing_object`)
and pass the route object in the parameters with key
`RouteObjectInterface::ROUTE_OBJECT` (`_route_object`).
* The VersatileGeneratorInterface is deprecated as it was used to avoid errors
with routers not supporting objects in `$name`.
* The VersatileGeneratorInterface::supports method is deprecated as it was used
to avoid errors with routers not supporting objects in `$name`.

2.2.0
-----
Expand Down
2 changes: 1 addition & 1 deletion src/ChainRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private function doMatch($pathinfo, Request $request = null)
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
{
if (is_object($name)) {
@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);
@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);
}

$debug = [];
Expand Down
29 changes: 25 additions & 4 deletions src/ContentAwareGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@ public function setContentRepository(ContentRepositoryInterface $contentReposito
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
{
if ($name instanceof SymfonyRoute) {
@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);

$route = $this->getBestLocaleRoute($name, $parameters);
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name && array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute) {
$route = $this->getBestLocaleRoute($parameters[RouteObjectInterface::ROUTE_OBJECT], $parameters);
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name) {
if (array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute) {
$route = $this->getBestLocaleRoute($parameters[RouteObjectInterface::ROUTE_OBJECT], $parameters);
} else {
$route = $this->getRouteByContent($name, $parameters);
}
} elseif (is_string($name) && $name) {
$route = $this->getRouteByName($name, $parameters);
} else {
Expand Down Expand Up @@ -166,8 +172,13 @@ protected function getBestLocaleRoute(SymfonyRoute $route, $parameters)
protected function getRouteByContent($name, &$parameters)
{
if ($name instanceof RouteReferrersReadInterface) {
@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);

$content = $name;
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name && array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof RouteReferrersReadInterface) {
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof RouteReferrersReadInterface
) {
$content = $parameters[RouteObjectInterface::ROUTE_OBJECT];
} elseif (array_key_exists('content_id', $parameters)
&& null !== $this->contentRepository
Expand Down Expand Up @@ -294,10 +305,20 @@ public function supports($name)
*/
public function getRouteDebugMessage($name, array $parameters = [])
{
if (!$name && array_key_exists('content_id', $parameters)) {
if ((!$name || RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name)
&& array_key_exists('content_id', $parameters)
) {
return 'Content id '.$parameters['content_id'];
}

if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof RouteReferrersReadInterface
) {
return 'Route aware content '.parent::getRouteDebugMessage($name, $parameters);
}

// legacy
if ($name instanceof RouteReferrersReadInterface) {
return 'Route aware content '.parent::getRouteDebugMessage($name, $parameters);
}
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function getGenerator()
public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
{
if (is_object($name)) {
@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);
@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);
}

if ($this->eventDispatcher) {
Expand Down
20 changes: 20 additions & 0 deletions src/ProviderBasedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,30 @@ public function supports($name)
*/
public function getRouteDebugMessage($name, array $parameters = [])
{
if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
) {
$routeObject = $parameters[RouteObjectInterface::ROUTE_OBJECT];
if ($routeObject instanceof RouteObjectInterface) {
return 'Route with key '.$routeObject->getRouteKey();
}

if ($routeObject instanceof SymfonyRoute) {
return 'Route with path '.$routeObject->getPath();
}

if (is_object($routeObject)) {
return get_class($routeObject);
}

return 'Null route';
}

if (is_scalar($name)) {
return $name;
}

// legacy
if (is_array($name)) {
return serialize($name);
}
Expand Down
17 changes: 10 additions & 7 deletions src/VersatileGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* This generator is able to handle more than string route names as symfony
* core supports them.
*
* @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
* This generator can provide additional information about the route that we wanted to generate.
*/
interface VersatileGeneratorInterface extends UrlGeneratorInterface
{
Expand All @@ -28,6 +25,13 @@ interface VersatileGeneratorInterface extends UrlGeneratorInterface
* resolved to a route, only whether the router can generate routes from
* objects of this class.
*
* @deprecated This method is deprecated since version 2.3 and will be
* removed in version 3.O.
*
* This method was used to not call generators that can not handle objects
* in $name. With Symfony 5, this becomes obsolete as the strict type
* declaration prevents passing anything else than a string as $name.
*
* @param mixed $name The route "name" which may also be an object or anything
*
* @return bool
Expand All @@ -38,9 +42,8 @@ public function supports($name);
* Convert a route identifier (name, content object etc) into a string
* usable for logging and other debug/error messages.
*
* @param mixed $name
* @param array $parameters which should contain a content field containing
* a RouteReferrersReadInterface object
* @param mixed $name In Symfony 5, the name can only be a string
* @param array $parameters Which might hold a route object or content id or similar to include in the debug message
*
* @return string
*/
Expand Down
22 changes: 14 additions & 8 deletions tests/Unit/Routing/ChainRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,12 @@ public function testGenerateNotFound()
* Route is an object but no versatile generator around to do the debug message.
*
* @group legacy
* @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.
* @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`.
*/
public function testGenerateObjectNotFound()
{
if (!class_exists(ObjectRouteLoader::class)) {
$this->markTestSkipped('Skip this test on >= sf5. This will throw a \TypeError.');
$this->markTestSkipped('Symfony 5 would throw a TypeError.');
}

$name = new \stdClass();
Expand All @@ -645,12 +645,12 @@ public function testGenerateObjectNotFound()
* A versatile router will generate the debug message.
*
* @group legacy
* @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.
* @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`.
*/
public function testGenerateObjectNotFoundVersatile()
{
if (!class_exists(ObjectRouteLoader::class)) {
$this->markTestSkipped('Skip this test on >= sf5. This will throw a \TypeError.');
$this->markTestSkipped('Symfony 5 would throw a TypeError.');
}

$name = new \stdClass();
Expand Down Expand Up @@ -681,12 +681,12 @@ public function testGenerateObjectNotFoundVersatile()

/**
* @group legacy
* @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.
* @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`.
*/
public function testGenerateObjectName()
{
if (!class_exists(ObjectRouteLoader::class)) {
$this->markTestSkipped('Skip this test on >= sf5. This will throw a \TypeError.');
$this->markTestSkipped('Symfony 5 would throw a TypeError.');
}

$name = new \stdClass();
Expand Down Expand Up @@ -718,6 +718,9 @@ public function testGenerateObjectName()
$this->assertEquals($name, $result);
}

/**
* This test currently triggers a deprecation notice because of ChainRouter BC.
*/
public function testGenerateWithObjectNameInParametersNotFoundVersatile()
{
$name = RouteObjectInterface::OBJECT_BASED_ROUTE_NAME;
Expand Down Expand Up @@ -757,13 +760,13 @@ public function testGenerateWithObjectNameInParameters()
->expects($this->once())
->method('generate')
->with($name, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH)
->willReturn($name)
->willReturn('/foo/bar')
;

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

$result = $this->router->generate($name, $parameters);
$this->assertEquals($name, $result);
$this->assertEquals('/foo/bar', $result);
}

public function testWarmup()
Expand Down Expand Up @@ -817,6 +820,9 @@ public function testRouteCollection()
$this->assertEquals(['high', 'low'], $names);
}

/**
* @group legacy
*/
public function testSupport()
{
$router = $this->createMock(VersatileRouter::class);
Expand Down
Loading

0 comments on commit c969140

Please sign in to comment.