Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 78255fe

Browse filesBrowse files
committed
Merge pull request symfony#32 from krizon/route-key-interface
Add getRouteKey method to RouteObjectInterface
2 parents ad024d9 + f426ac2 commit 78255fe
Copy full SHA for 78255fe

7 files changed

+141
-6
lines changed

‎ChainRouter.php

Copy file name to clipboardExpand all lines: ChainRouter.php
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,15 @@ public function generate($name, $parameters = array(), $absolute = false)
199199
/** @var $router ChainedRouterInterface */
200200
foreach ($this->all() as $router) {
201201

202-
if ($name && !is_string($name) && !$router instanceof ChainedRouterInterface ) {
203-
continue;
202+
// if $name and $router does not implement ChainedRouterInterface and $name is not a string, continue
203+
// if $name and $router does not implement ChainedRouterInterface and $name is string but does not match a default Symfony2 route name, continue
204+
if ($name && !$router instanceof ChainedRouterInterface) {
205+
if (!is_string($name) || !preg_match('/^[a-z0-9A-Z_.]+$/', $name)) {
206+
continue;
207+
}
204208
}
205209

210+
// If $router implements ChainedRouterInterface but doesn't support this route name, continue
206211
if ($router instanceof ChainedRouterInterface && !$router->supports($name)) {
207212
continue;
208213
}

‎DynamicRouter.php

Copy file name to clipboardExpand all lines: DynamicRouter.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ public function match($url)
223223
$defaults[RouteObjectInterface::CONTENT_OBJECT] = $content;
224224
}
225225

226+
if ($route instanceof RouteObjectInterface && is_string($route->getRouteKey())) {
227+
$defaults['_route'] = $route->getRouteKey();
228+
}
229+
226230
return $defaults;
227231
}
228232

‎README.md

Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ This library extends the Symfony2 Routing component. Even though it has Symfony
44
in its name, it does not need the full Symfony2 framework and can be used in
55
standalone projects.
66

7-
http://symfony.com/doc/master/cmf/reference/routing.html
7+
http://symfony.com/doc/master/cmf/components/routing.html

‎RouteObjectInterface.php

Copy file name to clipboardExpand all lines: RouteObjectInterface.php
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
* addition to extending Symfony\Component\Routing\Route.
88
*
99
* If they do, the DynamicRouter will request the route content and put it into
10-
* the RouteObjectInterface::CONTENT_OBJECT field.
10+
* the RouteObjectInterface::CONTENT_OBJECT field. The DynamicRouter will also
11+
* request getRouteKey and this will be used instead of the symfony core compatible
12+
* route name and can contain any characters.
1113
*
1214
* Some fields in defaults have a special meaning in the getDefaults(). In addition
1315
* to the constants defined in this class, _locale and _controller are also used.
@@ -53,4 +55,16 @@ interface RouteObjectInterface
5355
* @return object the document or entity this route entry points to
5456
*/
5557
public function getRouteContent();
58+
59+
/**
60+
* Get the route key.
61+
*
62+
* This key will be used as route name instead of the symfony core compatible
63+
* route name and can contain any characters.
64+
*
65+
* Return null if you want to use the default key.
66+
*
67+
* @return string the route name
68+
*/
69+
public function getRouteKey();
5670
}

‎Tests/Mapper/RouteObject.php

Copy file name to clipboardExpand all lines: Tests/Mapper/RouteObject.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
*/
1212
abstract class RouteObject extends Route implements RouteObjectInterface
1313
{
14-
14+
public function getRouteKey()
15+
{
16+
return null;
17+
}
1518
}

‎Tests/Routing/ChainRouterTest.php

Copy file name to clipboardExpand all lines: Tests/Routing/ChainRouterTest.php
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,68 @@ public function testGenerateNotFound()
428428
$this->assertEquals($url, $result);
429429
}
430430

431+
public function testGenerateObjectName()
432+
{
433+
$name = new \stdClass();
434+
$parameters = array('test' => 'value');
435+
436+
$defaultRouter = $this->getMock('Symfony\\Component\\Routing\\RouterInterface');
437+
$chainedRouter = $this->getMock('Symfony\\Cmf\\Component\\Routing\\ChainedRouterInterface');
438+
439+
$defaultRouter
440+
->expects($this->never())
441+
->method('generate')
442+
;
443+
$chainedRouter
444+
->expects($this->once())
445+
->method('supports')
446+
->will($this->returnValue(true))
447+
;
448+
$chainedRouter
449+
->expects($this->once())
450+
->method('generate')
451+
->with($name, $parameters, false)
452+
->will($this->returnValue($name))
453+
;
454+
455+
$this->router->add($defaultRouter, 200);
456+
$this->router->add($chainedRouter, 100);
457+
458+
$result = $this->router->generate($name, $parameters);
459+
$this->assertEquals($name, $result);
460+
}
461+
462+
public function testGenerateNonDefaultStringName()
463+
{
464+
$name = '/test/this';
465+
$parameters = array('test' => 'value');
466+
467+
$defaultRouter = $this->getMock('Symfony\\Component\\Routing\\RouterInterface');
468+
$chainedRouter = $this->getMock('Symfony\\Cmf\\Component\\Routing\\ChainedRouterInterface');
469+
470+
$defaultRouter
471+
->expects($this->never())
472+
->method('generate')
473+
;
474+
$chainedRouter
475+
->expects($this->once())
476+
->method('supports')
477+
->will($this->returnValue(true))
478+
;
479+
$chainedRouter
480+
->expects($this->once())
481+
->method('generate')
482+
->with($name, $parameters, false)
483+
->will($this->returnValue($name))
484+
;
485+
486+
$this->router->add($defaultRouter, 200);
487+
$this->router->add($chainedRouter, 100);
488+
489+
$result = $this->router->generate($name, $parameters);
490+
$this->assertEquals($name, $result);
491+
}
492+
431493
public function testWarmup()
432494
{
433495
$dir = 'test_dir';

‎Tests/Routing/DynamicRouterTest.php

Copy file name to clipboardExpand all lines: Tests/Routing/DynamicRouterTest.php
+48-1Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DynamicRouterTest extends CmfUnitTestCase
2424
public function setUp()
2525
{
2626
$this->contentDocument = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\RouteAwareInterface');
27-
$this->routeDocument = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array('getDefaults', 'getRouteContent'));
27+
$this->routeDocument = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array('getDefaults', 'getRouteContent', 'getRouteKey'));
2828
$this->loader = $this->buildMock("Symfony\\Component\\Config\\Loader\\LoaderInterface");
2929
$this->repository = $this->buildMock("Symfony\\Cmf\\Component\\Routing\\RouteRepositoryInterface", array('findManyByUrl', 'getRouteByName'));
3030

@@ -298,6 +298,49 @@ public function testMatch()
298298
$this->assertEquals($expected, $results);
299299
}
300300

301+
public function testMatchRouteKey()
302+
{
303+
$url_alias = "/company/more";
304+
305+
$this->routeDocument->expects($this->once())
306+
->method('getRouteContent')
307+
->will($this->returnValue($this->contentDocument));
308+
$this->routeDocument->expects($this->atLeastOnce())
309+
->method('getRouteKey')
310+
->will($this->returnValue($url_alias));
311+
312+
$routeCollection = new RouteCollection();
313+
$routeCollection->add('_company_more', $this->routeDocument);
314+
$this->repository->expects($this->once())
315+
->method('findManyByUrl')
316+
->with($url_alias)
317+
->will($this->returnValue($routeCollection));
318+
319+
$this->mapper->expects($this->once())
320+
->method('getController')
321+
->will($this->returnValue('NameSpace\\Controller::action'));
322+
323+
$matcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcher')->disableOriginalConstructor()->getMock();
324+
$matcher->expects($this->once())
325+
->method('match')
326+
->with($url_alias)
327+
->will($this->returnValue(array('_route' => '_company_more')));
328+
329+
$router = new TestRouter($this->repository, $matcher);
330+
$router->setContext($this->context);
331+
$router->addControllerMapper($this->mapper);
332+
333+
$results = $router->match($url_alias);
334+
335+
$expected = array(
336+
RouteObjectInterface::CONTROLLER_NAME => 'NameSpace\\Controller::action',
337+
'_route' => $url_alias,
338+
RouteObjectInterface::CONTENT_OBJECT => $this->contentDocument,
339+
);
340+
341+
$this->assertEquals($expected, $results);
342+
}
343+
301344
public function testNoReferenceMatch()
302345
{
303346
$url_alias = "/company/more_no_reference";
@@ -418,6 +461,10 @@ public function getDefaults()
418461
}
419462
return $defaults;
420463
}
464+
public function getRouteKey()
465+
{
466+
return null;
467+
}
421468
}
422469

423470
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.