Skip to content

Commit 92ee467

Browse files
committed
Merge pull request symfony#44 from symfony-cmf/safe-generator
add an interface for the generator to know if it has the supports method
2 parents 817b65e + 95e6f7c commit 92ee467

File tree

6 files changed

+48
-24
lines changed

6 files changed

+48
-24
lines changed

ChainRouter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function match($url)
135135
{
136136
$methodNotAllowed = null;
137137

138-
/** @var $router ChainedRouterInterface */
138+
/** @var $router RouterInterface */
139139
foreach ($this->all() as $router) {
140140
try {
141141
return $router->match($url);
@@ -196,13 +196,13 @@ public function matchRequest(Request $request)
196196
*/
197197
public function generate($name, $parameters = array(), $absolute = false)
198198
{
199-
/** @var $router ChainedRouterInterface */
199+
/** @var $router RouterInterface */
200200
foreach ($this->all() as $router) {
201201

202202
// if $name and $router does not implement ChainedRouterInterface and $name is not a string, continue
203203
// if $name and $router does not implement ChainedRouterInterface and $name is string but does not match a default Symfony2 route name, continue
204204
if ($name && !$router instanceof ChainedRouterInterface) {
205-
if (!is_string($name) || !preg_match('/^[a-z0-9A-Z_.]+$/', $name)) {
205+
if (!is_string($name) || !preg_match(VersatileGeneratorInterface::CORE_NAME_PATTERN, $name)) {
206206
continue;
207207
}
208208
}

ChainedRouterInterface.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,9 @@
55
use Symfony\Component\Routing\RouterInterface;
66

77
/**
8-
* Use this interface on custom routers that can handle non-string route
9-
* "names".
8+
* Interface to combine the VersatileGeneratorInterface with the RouterInterface
109
*/
11-
interface ChainedRouterInterface extends RouterInterface
10+
interface ChainedRouterInterface extends RouterInterface, VersatileGeneratorInterface
1211
{
13-
/**
14-
* Whether the router supports the thing in $name to generate a route.
15-
*
16-
* This check does not need to look if the specific instance can be
17-
* resolved to a route, only whether the router can generate routes from
18-
* objects of this class.
19-
20-
* @param mixed $name The route name or route object
21-
*
22-
* @return bool
23-
*/
24-
public function supports($name);
12+
// nothing new to add
2513
}

DynamicRouter.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* A flexible router accepting matcher and generator through injection and
1919
* using the RouteEnhancer concept to generate additional data on the routes.
2020
*
21-
* @author Crell
21+
* @author Larry Garfield
2222
* @author David Buchmann
2323
*/
2424
class DynamicRouter implements RouterInterface, RequestMatcherInterface, ChainedRouterInterface
@@ -130,14 +130,18 @@ public function generate($name, $parameters = array(), $absolute = false)
130130
}
131131

132132
/**
133-
* Support any string as route name
133+
* Delegate to our generator
134134
*
135135
* {@inheritDoc}
136136
*/
137137
public function supports($name)
138138
{
139-
// TODO: check $this->generator instanceof VersatileGeneratorInterface
140-
return $this->generator->supports($name);
139+
if ($this->generator instanceof VersatileGeneratorInterface) {
140+
141+
return $this->generator->supports($name);
142+
}
143+
144+
return (!is_string($name) || !preg_match(VersatileGeneratorInterface::CORE_NAME_PATTERN, $name));
141145
}
142146

143147
/**

ProviderBasedGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @author Larry Garfield
1818
*/
19-
class ProviderBasedGenerator extends UrlGenerator
19+
class ProviderBasedGenerator extends UrlGenerator implements VersatileGeneratorInterface
2020
{
2121
/**
2222
* The route provider for this generator.

Tests/Routing/DynamicRouterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function setUp()
2929
$this->routeDocument = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array('getDefaults'));
3030

3131
$this->matcher = $this->buildMock('Symfony\\Component\\Routing\\Matcher\\UrlMatcherInterface');
32-
$this->generator = $this->buildMock('Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface', array('supports', 'generate', 'setContext', 'getContext'));
32+
$this->generator = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\VersatileGeneratorInterface', array('supports', 'generate', 'setContext', 'getContext'));
3333
$this->enhancer = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\Enhancer\\RouteEnhancerInterface', array('enhance'));
3434

3535
$this->context = $this->buildMock('Symfony\\Component\\Routing\\RequestContext');

VersatileGeneratorInterface.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Component\Routing;
4+
5+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
6+
7+
/**
8+
* This generator is able to handle more than string route names as symfony
9+
* core supports them.
10+
*/
11+
interface VersatileGeneratorInterface extends UrlGeneratorInterface
12+
{
13+
/**
14+
* If $name preg_match this pattern, the name is valid for symfony core
15+
* compatible generators.
16+
*/
17+
const CORE_NAME_PATTERN = '/^[a-z0-9A-Z_.]+$/';
18+
19+
/**
20+
* Whether this generator supports the supplied $name.
21+
*
22+
* This check does not need to look if the specific instance can be
23+
* resolved to a route, only whether the router can generate routes from
24+
* objects of this class.
25+
*
26+
* @param mixed $name The route "name" which may also be an object or anything
27+
*
28+
* @return bool
29+
*/
30+
public function supports($name);
31+
}
32+

0 commit comments

Comments
 (0)