8000 use request stack rather than a single request by dbu · Pull Request #390 · symfony-cmf/routing-bundle · GitHub
[go: up one dir, main page]

Skip to content

use request stack rather than a single request #390

8000
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Changelog
=========

* **2017-02-17**: [BC BREAK] DynamicRouter::setRequest has been replaced with DynamicRouter::setRequestStack.

2.0.0-RC2
---------

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/routing-dynamic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<argument>%cmf_routing.uri_filter_regexp%</argument>
<argument type="service" id="event_dispatcher" on-invalid="ignore"/>
<argument type="service" id="cmf_routing.route_provider"/>
<call method="setRequest"><argument type="service" id="request" on-invalid="null" strict="false"/></call>
<call method="setRequestStack"><argument type="service" id="request_stack"/></call>
</service>

<service id="cmf_routing.nested_matcher" class="Symfony\Cmf\Component\Routing\NestedMatcher\NestedMatcher">
Expand Down
22 changes: 14 additions & 8 deletions src/Routing/DynamicRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Cmf\Component\Routing\DynamicRouter as BaseDynamicRouter;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

/**
Expand Down Expand Up @@ -44,9 +45,9 @@ class DynamicRouter extends BaseDynamicRouter
const CONTENT_TEMPLATE = 'template';

/**
* @var Request
* @var RequestStack
*/
protected $request;
private $requestStack;

/**
* Put content and template name into the request attributes instead of the
Expand Down Expand Up @@ -110,24 +111,29 @@ protected function cleanDefaults($defaults, Request $request = null)
}

/**
* @param Request $request
* Set the request stack so that we can find the current request.
*
* @param RequestStack $requestStack
*/
public function setRequest(Request $request = null)
public function setRequestStack(RequestStack $requestStack)
{
$this->request = $request;
$this->requestStack = $requestStack;
}

/**
* Get the current request from the request stack.
*
* @return Request
*
* @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException
*/
public function getRequest()
{
if (null === $this->request) {
throw new ResourceNotFoundException('Request object not available from container');
$currentRequest = $this->requestStack->getCurrentRequest();
if (!$currentRequest) {
throw new ResourceNotFoundException('There is no request in the request stack');
}

return $this->request;
return $currentRequest;
}
}
16 changes: 12 additions & 4 deletions tests/Unit/Routing/DynamicRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\RequestContext;
Expand All @@ -31,6 +33,10 @@ class DynamicRouterTest extends CmfUnitTestCase
protected $context;
/** @var Request */
protected $request;
/**
* @var RequestStack
*/
private $requestStack;
/** @var EventDispatcherInterface */
protected $eventDispatcher;
protected $container;
Expand All @@ -47,10 +53,12 @@ public function setUp()
$this->generator = $this->createMock(UrlGeneratorInterface::class);

$this->request = Request::create('/foo');
$this->requestStack = new RequestStack();
$this->requestStack->push($this->request);
$this->context = $this->createMock(RequestContext::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $this->eventDispatcher);
$this->router->setRequest($this->request);
$this->router->setRequestStack($this->requestStack);
}

private function assertRequestAttributes($request)
Expand Down Expand Up @@ -91,19 +99,19 @@ public function testMatchRequest()
}

/**
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
*
* @group legacy
*/
public function testMatchNoRequest()
{
$this->router->setRequest(null);
$this->router->setRequestStack(new RequestStack());

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->with(Events::PRE_DYNAMIC_MATCH, $this->equalTo(new RouterMatchEvent()))
;

$this->expectException(ResourceNotFoundException::class);

$this->router->match('/foo');
}

Expand Down
0