Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit78255fe

Browse files
committed
Merge pull requestsymfony#32 from krizon/route-key-interface
Add getRouteKey method to RouteObjectInterface
2 parentsad024d9 +f426ac2 commit78255fe

File tree

7 files changed

+141
-6
lines changed

7 files changed

+141
-6
lines changed

‎ChainRouter.php

Lines 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) && !$routerinstanceof 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 && !$routerinstanceof 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 ($routerinstanceof ChainedRouterInterface && !$router->supports($name)) {
207212
continue;
208213
}

‎DynamicRouter.php

Lines 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 ($routeinstanceof RouteObjectInterface &&is_string($route->getRouteKey())) {
227+
$defaults['_route'] =$route->getRouteKey();
228+
}
229+
226230
return$defaults;
227231
}
228232

‎README.md

Lines 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

Lines 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
publicfunctiongetRouteContent();
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+
publicfunctiongetRouteKey();
5670
}

‎Tests/Mapper/RouteObject.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
*/
1212
abstractclass RouteObjectextends Routeimplements RouteObjectInterface
1313
{
14-
14+
publicfunctiongetRouteKey()
15+
{
16+
returnnull;
17+
}
1518
}

‎Tests/Routing/ChainRouterTest.php

Lines 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+
publicfunctiontestGenerateObjectName()
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+
publicfunctiontestGenerateNonDefaultStringName()
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
publicfunctiontestWarmup()
432494
{
433495
$dir ='test_dir';

‎Tests/Routing/DynamicRouterTest.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DynamicRouterTest extends CmfUnitTestCase
2424
publicfunctionsetUp()
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+
publicfunctiontestMatchRouteKey()
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 =newRouteCollection();
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 =newTestRouter($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
publicfunctiontestNoReferenceMatch()
302345
{
303346
$url_alias ="/company/more_no_reference";
@@ -418,6 +461,10 @@ public function getDefaults()
418461
}
419462
return$defaults;
420463
}
464+
publicfunctiongetRouteKey()
465+
{
466+
returnnull;
467+
}
421468
}
422469

423470
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp