Skip to content

Commit 3c65ef3

Browse files
committed
Remove deprecated code in generate method
1 parent fdd2aaf commit 3c65ef3

File tree

8 files changed

+17
-233
lines changed

8 files changed

+17
-233
lines changed

src/ChainRouter.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ public function __construct(LoggerInterface $logger = null)
6767
$this->logger = $logger;
6868
}
6969

70-
/**
71-
* @return RequestContext
72-
*/
7370
public function getContext(): RequestContext
7471
{
7572
if (!$this->context) {
@@ -215,21 +212,11 @@ private function doMatch($pathinfo, Request $request = null)
215212
*
216213
* @param string $name
217214
*
218-
* The CMF routing system used to allow to pass route objects as $name to generate the route.
219-
* Since Symfony 5.0, the UrlGeneratorInterface declares $name as string. We widen the contract
220-
* for BC but deprecate passing non-strings.
221-
* Instead, Pass the RouteObjectInterface::OBJECT_BASED_ROUTE_NAME as route name and the object
222-
* in the parameters with key RouteObjectInterface::ROUTE_OBJECT.
223-
*
224215
* Loops through all registered routers and returns a router if one is found.
225216
* It will always return the first route generated.
226217
*/
227218
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string
228219
{
229-
if (is_object($name)) {
230-
@trigger_error('Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.', E_USER_DEPRECATED);
231-
}
232-
233220
$debug = [];
234221

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

src/ContentAwareGenerator.php

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,24 @@ public function setContentRepository(ContentRepositoryInterface $contentReposito
5656
/**
5757
* {@inheritdoc}
5858
*
59-
* The CMF routing system used to allow to pass route objects as $name to generate the route.
60-
* Since Symfony 5.0, the UrlGeneratorInterface declares $name as string. We widen the contract
61-
* for BC but deprecate passing non-strings.
62-
* Instead, Pass the RouteObjectInterface::OBJECT_BASED_ROUTE_NAME as route name and the object
63-
* in the parameters with key RouteObjectInterface::ROUTE_OBJECT or the ID of a
64-
* RouteReferrersReadInterface in 'content_id.
65-
*
66-
* @param mixed $name ignored
67-
* @param array $parameters must either contain the field 'route' with a
68-
* RouteObjectInterface or the field 'content_id'
69-
* with the id of a document implementing
70-
* RouteReferrersReadInterface
59+
* @param string $name ignored
60+
* @param array $parameters must either contain the field 'route' with a
61+
* RouteObjectInterface or the field 'content_id'
62+
* with the id of a document implementing RouteReferrersReadInterface
7163
*
7264
* @throws RouteNotFoundException If there is no such route in the database
7365
*/
7466
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string
7567
{
76-
if ($name instanceof SymfonyRoute) {
77-
@trigger_error('Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT` resp the content id with content_id.', E_USER_DEPRECATED);
78-
79-
$route = $this->getBestLocaleRoute($name, $parameters);
80-
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name) {
68+
if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name) {
8169
if (array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
8270
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute
8371
) {
8472
$route = $this->getBestLocaleRoute($parameters[RouteObjectInterface::ROUTE_OBJECT], $parameters);
8573
} else {
8674
$route = $this->getRouteByContent($name, $parameters);
8775
}
88-
} elseif (is_string($name) && $name) {
76+
} elseif (!empty($name)) {
8977
$route = $this->getRouteByName($name, $parameters);
9078
} else {
9179
$route = $this->getRouteByContent($name, $parameters);
@@ -167,21 +155,16 @@ protected function getBestLocaleRoute(SymfonyRoute $route, $parameters)
167155
* If no route with matching locale is found, falls back to just return the
168156
* first route.
169157
*
170-
* @param mixed $name
171158
* @param array $parameters which should contain a content field containing
172159
* a RouteReferrersReadInterface object
173160
*
174161
* @return SymfonyRoute the route instance
175162
*
176163
* @throws RouteNotFoundException if no route can be determined
177164
*/
178-
protected function getRouteByContent($name, &$parameters)
165+
protected function getRouteByContent(string $name, &$parameters)
179166
{
180-
if ($name instanceof RouteReferrersReadInterface) {
181-
@trigger_error('Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.', E_USER_DEPRECATED);
182-
183-
$content = $name;
184-
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
167+
if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
185168
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
186169
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof RouteReferrersReadInterface
187170
) {
@@ -197,9 +180,7 @@ protected function getRouteByContent($name, &$parameters)
197180
throw new RouteNotFoundException('Content repository did not return a RouteReferrersReadInterface instance for id '.$parameters['content_id']);
198181
}
199182
} else {
200-
$hint = is_object($name) ? get_class($name) : gettype($name);
201-
202-
throw new RouteNotFoundException("The route name argument '$hint' is not a RouteReferrersReadInterface instance and there is no 'content_id' parameter");
183+
throw new RouteNotFoundException(sprintf("The route name argument '%s' is not a RouteReferrersReadInterface instance and there is no 'content_id' parameter", gettype($name)));
203184
}
204185

205186
$routes = $content->getRoutes();

src/DynamicRouter.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ public function getGenerator()
165165
*/
166166
public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
167167
{
168-
if (is_object($name)) {
169-
@trigger_error('Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT', E_USER_DEPRECATED);
170-
}
171-
172168
if ($this->eventDispatcher) {
173169
$event = new RouterGenerateEvent($name, $parameters, $referenceType);
174170
$this->eventDispatcher->dispatch($event, Events::PRE_DYNAMIC_GENERATE);

src/ProviderBasedGenerator.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,10 @@ public function __construct(RouteProviderInterface $provider, LoggerInterface $l
4343

4444
/**
4545
* {@inheritdoc}
46-
*
47-
* The CMF routing system used to allow to pass route objects as $name to generate the route.
48-
* Since Symfony 5.0, the UrlGeneratorInterface declares $name as string. We widen the contract
49-
* for BC but deprecate passing non-strings.
50-
* Instead, Pass the RouteObjectInterface::OBJECT_BASED_ROUTE_NAME as route name and the object
51-
* in the parameters with key RouteObjectInterface::ROUTE_OBJECT.
52-
*
53-
* @param mixed $name
5446
*/
5547
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH): string
5648
{
57-
if (is_object($name)) {
58-
@trigger_error('Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`', E_USER_DEPRECATED);
59-
}
60-
if ($name instanceof SymfonyRoute) {
61-
$route = $name;
62-
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
49+
if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
6350
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
6451
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute
6552
) {

tests/Unit/Candidates/CandidatesTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function testIsCandidate()
2929

3030
/**
3131
* Nothing should be called on the query builder.
32+
*
33+
* @doesNotPerformAssertions
3234
*/
3335
public function testRestrictQuery()
3436
{

tests/Unit/Routing/ChainRouterTest.php

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
2424
use Symfony\Component\Routing\Exception\RouteNotFoundException;
2525
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
26-
use Symfony\Component\Routing\Loader\ObjectRouteLoader;
2726
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
2827
use Symfony\Component\Routing\RequestContext;
2928
use Symfony\Component\Routing\Route;
@@ -608,111 +607,6 @@ public function testGenerateNotFound()
608607
$this->router->generate($name, $parameters);
609608
}
610609

611-
/**
612-
* Route is an object but no versatile generator around to do the debug message.
613-
*
614-
* @group legacy
615-
* @expectedDeprecation Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.
616-
*/
617-
public function testGenerateObjectNotFound()
618-
{
619-
if (!class_exists(ObjectRouteLoader::class)) {
620-
$this->markTestSkipped('Symfony 5 would throw a TypeError.');
621-
}
622-
623-
$name = new \stdClass();
624-
$parameters = ['test' => 'value'];
625-
626-
$defaultRouter = $this->createMock(RouterInterface::class);
627-
628-
$defaultRouter
629-
->expects($this->never())
630-
->method('generate')
631-
;
632-
633-
$this->router->add($defaultRouter, 200);
634-
635-
$this->expectException(RouteNotFoundException::class);
636-
$this->router->generate($name, $parameters);
637-
}
638-
639-
/**
640-
* A versatile router will generate the debug message.
641-
*
642-
* @group legacy
643-
* @expectedDeprecation Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.
644-
*/
645-
public function testGenerateObjectNotFoundVersatile()
646-
{
647-
if (!class_exists(ObjectRouteLoader::class)) {
648-
$this->markTestSkipped('Symfony 5 would throw a TypeError.');
649-
}
650-
651-
$name = new \stdClass();
652-
$parameters = ['test' => 'value'];
653-
654-
$chainedRouter = $this->createMock(VersatileRouter::class);
655-
$chainedRouter
656-
->expects($this->once())
657-
->method('supports')
658-
->will($this->returnValue(true))
659-
;
660-
$chainedRouter->expects($this->once())
661-
->method('generate')
662-
->with($name, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH)
663-
->will($this->throwException(new RouteNotFoundException()))
664-
;
665-
$chainedRouter->expects($this->once())
666-
->method('getRouteDebugMessage')
667-
->with($name, $parameters)
668-
->will($this->returnValue('message'))
669-
;
670-
671-
$this->router->add($chainedRouter, 10);
672-
673-
$this->expectException(RouteNotFoundException::class);
674-
$this->router->generate($name, $parameters);
675-
}
676-
677-
/**
678-
* @group legacy
679-
* @expectedDeprecation Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.
680-
*/
681-
public function testGenerateObjectName()
682-
{
683-
if (!class_exists(ObjectRouteLoader::class)) {
684-
$this->markTestSkipped('Symfony 5 would throw a TypeError.');
685-
}
686-
687-
$name = new \stdClass();
688-
$parameters = ['test' => 'value'];
689-
690-
$defaultRouter = $this->createMock(RouterInterface::class);
691-
$chainedRouter = $this->createMock(VersatileRouter::class);
692-
693-
$defaultRouter
694-
->expects($this->never())
695-
->method('generate')
696-
;
697-
$chainedRouter
698-
->expects($this->once())
699-
->method('supports')
700-
->will($this->returnValue(true))
701-
;
702-
$chainedRouter
703-
->expects($this->once())
704-
->method('generate')
705-
->with($name, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH)
706-
->will($this->returnValue($name))
707-
;
708-
709-
$this->router->add($defaultRouter, 200);
710-
$this->router->add($chainedRouter, 100);
711-
712-
$result = $this->router->generate($name, $parameters);
713-
$this->assertEquals($name, $result);
714-
}
715-
716610
/**
717611
* This test currently triggers a deprecation notice because of ChainRouter BC.
718612
*/

tests/Unit/Routing/ContentAwareGeneratorTest.php

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Symfony\Cmf\Component\Routing\Tests\Unit\Routing\RouteMock;
2222
use Symfony\Component\Routing\CompiledRoute;
2323
use Symfony\Component\Routing\Exception\RouteNotFoundException;
24-
use Symfony\Component\Routing\Loader\ObjectRouteLoader;
2524
use Symfony\Component\Routing\RequestContext;
2625
use Symfony\Component\Routing\Route;
2726

@@ -38,7 +37,7 @@ class ContentAwareGeneratorTest extends TestCase
3837
private $routeDocument;
3938

4039
/**
41-
* @var CompiledRoute|MockObject
40+
* @var CompiledRoute
4241
*/
4342
private $routeCompiled;
4443

@@ -65,38 +64,13 @@ public function setUp(): void
6564
->setMethods(['compile', 'getContent'])
6665
->getMock();
6766

68-
$this->routeCompiled = $this->createMock(CompiledRoute::class);
67+
$this->routeCompiled = new CompiledRoute('', '', [], []);
6968
$this->provider = $this->createMock(RouteProviderInterface::class);
7069
$this->context = $this->createMock(RequestContext::class);
7170

7271
$this->generator = new TestableContentAwareGenerator($this->provider);
7372
}
7473

75-
/**
76-
* @group legacy
77-
* @expectedDeprecation Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`.
78-
*/
79-
public function testGenerateFromContent(): void
80-
{
81-
if (!class_exists(ObjectRouteLoader::class)) {
82-
$this->markTestSkipped('Symfony 5 would throw a TypeError.');
83-
}
84-
85-
$this->provider->expects($this->never())
86-
->method('getRouteByName')
87-
;
88-
$this->contentDocument->expects($this->once())
89-
->method('getRoutes')
90-
->willReturn([$this->routeDocument])
91-
;
92-
$this->routeDocument->expects($this->once())
93-
->method('compile')
94-
->willReturn($this->routeCompiled)
95-
;
96-
97-
$this->assertEquals('result_url', $this->generator->generate($this->contentDocument));
98-
}
99-
10074
public function testGenerateFromContentInParameters(): void
10175
{
10276
$this->provider->expects($this->never())
@@ -233,10 +207,6 @@ public function testGenerateRouteMultilangDefaultLocale()
233207
->with('_locale')
234208
->willReturn('en')
235209
;
236-
$this->routeCompiled->expects($this->any())
237-
->method('getVariables')
238-
->willReturn([])
239-
;
240210

241211
$generated = $this->generator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, ['_locale' => 'en', RouteObjectInterface::ROUTE_OBJECT => $route]);
242212
$this->assertEquals('result_url', $generated);
@@ -403,21 +373,6 @@ public function testGenerateInvalidContent(): void
403373
$this->generator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [RouteObjectInterface::ROUTE_OBJECT => $this]);
404374
}
405375

406-
/**
407-
* Generate with an object that is neither a route nor route aware.
408-
*
409-
* @group legacy
410-
*/
411-
public function testGenerateInvalidContentLegacy(): void
412-
{
413-
if (!class_exists(ObjectRouteLoader::class)) {
414-
$this->markTestSkipped('Symfony 5 would throw a TypeError.');
415-
}
416-
417-
$this->expectException(RouteNotFoundException::class);
418-
$this->generator->generate($this);
419-
}
420-
421376
/**
422377
* Generate with a content_id but there is no content repository.
423378
*/

0 commit comments

Comments
 (0)