[Routing] remove RequestContextAwareInterface from RequestMatcherInterface

This commit is contained in:
Tobias Schultze 2012-06-14 23:30:21 +02:00
parent 921be34ee7
commit c413e7ba39
4 changed files with 17 additions and 13 deletions

View File

@ -89,6 +89,7 @@
<tag name="kernel.event_subscriber" />
<tag name="monolog.logger" channel="request" />
<argument type="service" id="router" />
<argument type="service" id="router.request_context" on-invalid="ignore" />
<argument type="service" id="logger" on-invalid="ignore" />
</service>
</services>

View File

@ -21,25 +21,33 @@ use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RequestContextAwareInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Initializes request attributes based on a matching route.
* Initializes the context from the request and sets request attributes based on a matching route.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class RouterListener implements EventSubscriberInterface
{
private $matcher;
private $context;
private $logger;
public function __construct($matcher, LoggerInterface $logger = null)
public function __construct($matcher, RequestContext $context = null, LoggerInterface $logger = null)
{
if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
}
if (null === $context && !$matcher instanceof RequestContextAwareInterface) {
throw new \InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.');
}
$this->matcher = $matcher;
$this->context = $context ?: $matcher->getContext();
$this->logger = $logger;
}
@ -47,8 +55,9 @@ class RouterListener implements EventSubscriberInterface
{
$request = $event->getRequest();
// initialize the context that is also used by the generator (assuming matcher and generator share the same context instance)
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
$this->matcher->getContext()->fromRequest($request);
$this->context->fromRequest($request);
}
if ($request->attributes->has('_controller')) {
@ -56,9 +65,9 @@ class RouterListener implements EventSubscriberInterface
return;
}
// add attributes based on the path info (routing)
// add attributes based on the request (routing)
try {
// matching requests is more powerful than matching URLs only, so try that first
// matching a request is more powerful than matching a URL path + context, so try that first
if ($this->matcher instanceof RequestMatcherInterface) {
$parameters = $this->matcher->matchRequest($request);
} else {

View File

@ -92,18 +92,13 @@ class RouterListenerTest extends \PHPUnit_Framework_TestCase
$request = Request::create('http://localhost/');
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
$context = new RequestContext();
$requestMatcher = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface');
$requestMatcher->expects($this->any())
->method('getContext')
->will($this->returnValue($context));
$requestMatcher->expects($this->once())
->method('matchRequest')
->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
->will($this->returnValue(array()));
$listener = new RouterListener($requestMatcher);
$listener = new RouterListener($requestMatcher, new RequestContext());
$listener->onKernelRequest($event);
}
}

View File

@ -12,7 +12,6 @@
namespace Symfony\Component\Routing\Matcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContextAwareInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
@ -21,7 +20,7 @@ use Symfony\Component\Routing\Exception\MethodNotAllowedException;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface RequestMatcherInterface extends RequestContextAwareInterface
interface RequestMatcherInterface
{
/**
* Tries to match a request with a set of routes.