Skip to content

Commit 17463cc

Browse files
committed
Merge pull request #120 from symfony-cmf/deprecate-dynamicrouter-match
deprecate DynamicRouter::match and cleanup interface usage
2 parents 062b241 + d9d4194 commit 17463cc

File tree

7 files changed

+49
-31
lines changed

7 files changed

+49
-31
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
* **2014-09-29**: ChainRouter does not require a RouterInterface, as a
5+
RequestMatcher and UrlGenerator is fine too. Fixed chain router interface to
6+
not force a RouterInterface.
7+
* **2014-09-29**: Deprecated DynamicRouter::match in favor of matchRequest.
8+
49
1.3.0-RC1
510
---------
611

ChainRouter.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Cmf\Component\Routing;
1313

1414
use Symfony\Component\Routing\RouterInterface;
15+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1516
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
1617
use Symfony\Component\Routing\RequestContext;
1718
use Symfony\Component\Routing\RequestContextAwareInterface;
@@ -24,17 +25,15 @@
2425
use Psr\Log\LoggerInterface;
2526

2627
/**
27-
* ChainRouter
28-
*
29-
* Allows access to a lot of different routers.
28+
* The ChainRouter allows to combine several routers to try in a defined order.
3029
*
3130
* @author Henrik Bjornskov <[email protected]>
3231
* @author Magnus Nordlander <[email protected]>
3332
*/
3433
class ChainRouter implements ChainRouterInterface, WarmableInterface
3534
{
3635
/**
37-
* @var \Symfony\Component\Routing\RequestContext
36+
* @var RequestContext
3837
*/
3938
private $context;
4039

@@ -45,17 +44,17 @@ class ChainRouter implements ChainRouterInterface, WarmableInterface
4544
private $routers = array();
4645

4746
/**
48-
* @var \Symfony\Component\Routing\RouterInterface[] Array of routers, sorted by priority
47+
* @var RouterInterface[] Array of routers, sorted by priority
4948
*/
5049
private $sortedRouters;
5150

5251
/**
53-
* @var \Symfony\Component\Routing\RouteCollection
52+
* @var RouteCollection
5453
*/
5554
private $routeCollection;
5655

5756
/**
58-
* @var null|\Psr\Log\LoggerInterface
57+
* @var null|LoggerInterface
5958
*/
6059
protected $logger;
6160

@@ -78,8 +77,13 @@ public function getContext()
7877
/**
7978
* {@inheritdoc}
8079
*/
81-
public function add(RouterInterface $router, $priority = 0)
80+
public function add($router, $priority = 0)
8281
{
82+
if (!$router instanceof RouterInterface
83+
&& !($router instanceof RequestMatcherInterface && $router instanceof UrlGeneratorInterface)
84+
) {
85+
throw new \InvalidArgumentException(sprintf('%s is not a valid router.', get_class($router)));
86+
}
8387
if (empty($this->routers[$priority])) {
8488
$this->routers[$priority] = array();
8589
}
@@ -159,6 +163,10 @@ public function matchRequest(Request $request)
159163
*
160164
* @param string $url
161165
* @param Request $request
166+
*
167+
* @return array An array of parameters
168+
*
169+
* @throws ResourceNotFoundException If no router matched.
162170
*/
163171
private function doMatch($url, Request $request = null)
164172
{
@@ -208,15 +216,14 @@ public function generate($name, $parameters = array(), $absolute = false)
208216
$debug = array();
209217

210218
foreach ($this->all() as $router) {
211-
// if $router does not implement ChainedRouterInterface and $name is not a string, continue
212-
if ($name && !$router instanceof ChainedRouterInterface) {
213-
if (! is_string($name)) {
214-
continue;
215-
}
219+
// if $router does not announce it is capable of handling
220+
// non-string routes and $name is not a string, continue
221+
if ($name && !is_string($name) && !$router instanceof VersatileGeneratorInterface) {
222+
continue;
216223
}
217224

218-
// If $router implements ChainedRouterInterface but doesn't support this route name, continue
219-
if ($router instanceof ChainedRouterInterface && !$router->supports($name)) {
225+
// If $router is versatile and doesn't support this route name, continue
226+
if ($router instanceof VersatileGeneratorInterface && !$router->supports($name)) {
220227
continue;
221228
}
222229

ChainRouterInterface.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,25 @@
1515
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
1616

1717
/**
18-
* ChainRouterInterface
18+
* Interface for a router that proxies routing to other routers.
1919
*
20-
* Allows access to a lot of different routers.
21-
*
22-
* @author Henrik Bjornskov <[email protected]>
23-
* @author Magnus Nordlander <[email protected]>
20+
* @author Daniel Wehner <[email protected]>
2421
*/
2522
interface ChainRouterInterface extends RouterInterface, RequestMatcherInterface
2623
{
2724
/**
28-
* Add a Router to the index
25+
* Add a Router to the index.
2926
*
30-
* @param RouterInterface $router The router instance
27+
* @param RouterInterface $router The router instance. Instead of RouterInterface, may also
28+
* be RequestMatcherInterface and UrlGeneratorInterface.
3129
* @param integer $priority The priority
3230
*/
33-
public function add(RouterInterface $router, $priority = 0);
31+
public function add($router, $priority = 0);
3432

3533
/**
3634
* Sorts the routers and flattens them.
3735
*
38-
* @return RouterInterface[]
36+
* @return RouterInterface[] or RequestMatcherInterface and UrlGeneratorInterface.
3937
*/
4038
public function all();
4139
}

ChainedRouterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Routing\RouterInterface;
1515

1616
/**
17-
* Interface to combine the VersatileGeneratorInterface with the RouterInterface
17+
* Interface to combine the VersatileGeneratorInterface with the RouterInterface.
1818
*/
1919
interface ChainedRouterInterface extends RouterInterface, VersatileGeneratorInterface
2020
{

DynamicRouter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public function supports($name)
198198
* @throws MethodNotAllowedException If the resource was found but the
199199
* request method is not allowed
200200
*
201+
* @deprecated Use matchRequest exclusively to avoid problems. This method will be removed in version 2.0
201202
* @api
202203
*/
203204
public function match($pathinfo)

Tests/Routing/ChainRouterTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111

1212
namespace Symfony\Cmf\Component\Routing\Tests\Routing;
1313

14+
use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface;
15+
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1416
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
1517
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
1618
use Symfony\Component\Routing\Exception\RouteNotFoundException;
19+
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
1720
use Symfony\Component\Routing\RequestContext;
1821
use Symfony\Component\Routing\RouteCollection;
1922
use Symfony\Component\HttpFoundation\Request;
@@ -569,7 +572,7 @@ public function testGenerateObjectNotFoundVersatile()
569572
$name = new \stdClass();
570573
$parameters = array('test' => 'value');
571574

572-
$chainedRouter = $this->getMock('Symfony\Cmf\Component\Routing\ChainedRouterInterface');
575+
$chainedRouter = $this->getMock('Symfony\Cmf\Component\Routing\Tests\Routing\VersatileRouter');
573576
$chainedRouter
574577
->expects($this->once())
575578
->method('supports')
@@ -597,7 +600,7 @@ public function testGenerateObjectName()
597600
$parameters = array('test' => 'value');
598601

599602
$defaultRouter = $this->getMock('Symfony\Component\Routing\RouterInterface');
600-
$chainedRouter = $this->getMock('Symfony\Cmf\Component\Routing\ChainedRouterInterface');
603+
$chainedRouter = $this->getMock('Symfony\Cmf\Component\Routing\Tests\Routing\VersatileRouter');
601604

602605
$defaultRouter
603606
->expects($this->never())
@@ -683,7 +686,7 @@ public function testRouteCollection()
683686
public function testSupport()
684687
{
685688

686-
$router = $this->getMock('Symfony\Cmf\Component\Routing\ChainedRouterInterface');
689+
$router = $this->getMock('Symfony\Cmf\Component\Routing\Tests\Routing\VersatileRouter');
687690
$router
688691
->expects($this->once())
689692
->method('supports')
@@ -714,10 +717,14 @@ protected function createRouterMocks()
714717
}
715718
}
716719

717-
abstract class WarmableRouterMock implements \Symfony\Component\Routing\RouterInterface, \Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface
720+
abstract class WarmableRouterMock implements RouterInterface, WarmableInterface
718721
{
719722
}
720723

721-
abstract class RequestMatcher implements \Symfony\Component\Routing\RouterInterface, \Symfony\Component\Routing\Matcher\RequestMatcherInterface
724+
abstract class RequestMatcher implements RouterInterface, RequestMatcherInterface
725+
{
726+
}
727+
728+
abstract class VersatileRouter implements VersatileGeneratorInterface, RequestMatcherInterface
722729
{
723730
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"psr/log": "~1.0"
2020
},
2121
"require-dev": {
22-
"symfony/dependency-injection": "~2.0",
22+
"symfony/dependency-injection": "~2.0@stable",
2323
"symfony/config": "~2.2",
2424
"symfony/event-dispatcher": "~2.1"
2525
},

0 commit comments

Comments
 (0)