feature #9494 made Router implement RequestMatcherInterface (fabpot)

This PR was merged into the master branch.

Discussion
----------

made Router implement RequestMatcherInterface

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #9492
| License       | MIT
| Doc PR        | n/a

Commits
-------

42346ea made Router implement RequestMatcherInterface
This commit is contained in:
Fabien Potencier 2013-11-12 22:52:06 +01:00
commit 1667197875
2 changed files with 42 additions and 1 deletions

View File

@ -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 <fabien@symfony.com>
*/
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.
*

View File

@ -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('/'));
}
}