diff --git a/src/Symfony/Component/Routing/Router.php b/src/Symfony/Component/Routing/Router.php index e9671922ba..6c9b20b998 100644 --- a/src/Symfony/Component/Routing/Router.php +++ b/src/Symfony/Component/Routing/Router.php @@ -17,8 +17,10 @@ use Psr\Log\LoggerInterface; use Symfony\Component\Routing\Generator\ConfigurableRequirementsInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\Dumper\GeneratorDumperInterface; +use Symfony\Component\Routing\Matcher\RequestMatcherInterface; use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface; +use Symfony\Component\HttpFoundation\Request; /** * The Router class is an example of the integration of all pieces of the @@ -26,7 +28,7 @@ use Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface; * * @author Fabien Potencier */ -class Router implements RouterInterface +class Router implements RouterInterface, RequestMatcherInterface { /** * @var UrlMatcherInterface|null @@ -217,6 +219,20 @@ class Router implements RouterInterface return $this->getMatcher()->match($pathinfo); } + /** + * {@inheritdoc} + */ + public function matchRequest(Request $request) + { + $matcher = $this->getMatcher(); + if (!$matcher instanceof RequestMatcherInterface) { + // fallback to the default UrlMatcherInterface + return $matcher->match($request->getPathInfo()); + } + + return $matcher->matchRequest($request); + } + /** * Gets the UrlMatcher instance associated with this Router. * diff --git a/src/Symfony/Component/Routing/Tests/RouterTest.php b/src/Symfony/Component/Routing/Tests/RouterTest.php index a3c336e5b7..42a344c74a 100644 --- a/src/Symfony/Component/Routing/Tests/RouterTest.php +++ b/src/Symfony/Component/Routing/Tests/RouterTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Routing\Tests; use Symfony\Component\Routing\Router; +use Symfony\Component\HttpFoundation\Request; class RouterTest extends \PHPUnit_Framework_TestCase { @@ -135,4 +136,28 @@ class RouterTest extends \PHPUnit_Framework_TestCase array('generator_cache_class') ); } + + public function testMatchRequestWithUrlMatcherInterface() + { + $matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface'); + $matcher->expects($this->once())->method('match'); + + $p = new \ReflectionProperty($this->router, 'matcher'); + $p->setAccessible(true); + $p->setValue($this->router, $matcher); + + $this->router->matchRequest(Request::create('/')); + } + + public function testMatchRequestWithRequestMatcherInterface() + { + $matcher = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface'); + $matcher->expects($this->once())->method('matchRequest'); + + $p = new \ReflectionProperty($this->router, 'matcher'); + $p->setAccessible(true); + $p->setValue($this->router, $matcher); + + $this->router->matchRequest(Request::create('/')); + } }