8000 this flag is ignored in symfony 3, defaults to false in 2.8 and trigg… · symfony-cmf/routing-bundle@83f5261 · GitHub
[go: up one dir, main page]

Skip to content

Commit 83f5261

Browse files
committed
this flag is ignored in symfony 3, defaults to false in 2.8 and triggers deprecation since symfony 3.3
1 parent 08a2163 commit 83f5261

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/Resources/config/routing-dynamic.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<argument>%cmf_routing.uri_filter_regexp%</argument>
6262
<argument type="service" id="event_dispatcher" on-invalid="ignore"/>
6363
<argument type="service" id="cmf_routing.route_provider"/>
64-
<call method="setRequest"><argument type="service" id="request" on-invalid="null" strict="false"/></call>
64+
<call method="setRequestStack"><argument type="service" id="request_stack"/></call>
6565
</service>
6666

6767
<service id="cmf_routing.nested_matcher" class="Symfony\Cmf\Component\Routing\NestedMatcher\NestedMatcher">

src/Routing/DynamicRouter.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Cmf\Component\Routing\DynamicRouter as BaseDynamicRouter;
1515
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
1616
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\RequestStack;
1718
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
1819

1920
/**
@@ -44,9 +45,9 @@ class DynamicRouter extends BaseDynamicRouter
4445
const CONTENT_TEMPLATE = 'template';
4546

4647
/**
47-
* @var Request
48+
* @var RequestStack
4849
*/
49-
protected $request;
50+
private $requestStack;
5051

5152
/**
5253
* Put content and template name into the request attributes instead of the
@@ -110,24 +111,24 @@ protected function cleanDefaults($defaults, Request $request = null)
110111
}
111112

112113
/**
113-
* @param Request $request
114+
* Set the request stack so that we can find the current request.
115+
*
116+
* @param RequestStack $requestStack
114117
*/
115-
public function setRequest(Request $request = null)
118+
public function setRequestStack(RequestStack $requestStack)
116119
{
117-
$this->request = $request;
120+
$this->requestStack = $requestStack;
118121
}
119122

120123
/**
124+
* Get the current request from the request stack.
125+
*
121126
* @return Request
122127
*
123128
* @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException
124129
*/
125130
public function getRequest()
126131
{
127-
if (null === $this->request) {
128-
throw new ResourceNotFoundException('Request object not available from container');
129-
}
130-
131-
return $this->request;
132+
return $this->requestStack->getCurrentRequest();
132133
}
133134
}

tests/Unit/Routing/DynamicRouterTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
1919
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2020
use Symfony\Component\HttpFoundation\Request;
21+
use Symfony\Component\HttpFoundation\RequestStack;
22+
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
2123
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2224
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
2325
use Symfony\Component\Routing\RequestContext;
@@ -31,6 +33,10 @@ class DynamicRouterTest extends CmfUnitTestCase
3133
protected $context;
3234
/** @var Request */
3335
protected $request;
36+
/**
37+
* @var RequestStack
38+
*/
39+
private $requestStack;
3440
/** @var EventDispatcherInterface */
3541
protected $eventDispatcher;
3642
protected $container;
@@ -47,10 +53,12 @@ public function setUp()
4753
$this->generator = $this->createMock(UrlGeneratorInterface::class);
4854

4955
$this->request = Request::create('/foo');
56+
$this->requestStack = new RequestStack();
57+
$this->requestStack->push($this->request);
5058
$this->context = $this->createMock(RequestContext::class);
5159
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
5260
$this->router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $this->eventDispatcher);
53-
$this->router->setRequest($this->request);
61+
$this->router->setRequestStack($this->request);
5462
}
5563

5664
private function assertRequestAttributes($request)
@@ -91,19 +99,19 @@ public function testMatchRequest()
9199
}
92100

93101
/**
94-
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
95-
*
96102
* @group legacy
97103
*/
98104
public function testMatchNoRequest()
99105
{
100-
$this->router->setRequest(null);
106+
$this->router->setRequestStack(new RequestStack());
101107

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

113+
$this->expectException(ResourceNotFoundException::class);
114+
107115
$this->router->match('/foo');
108116
}
109117

0 commit comments

Comments
 (0)
0