Skip to content

Commit 2bba795

Browse files
committed
Configuration to turn off copying of default variables
1 parent 72c99d2 commit 2bba795

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed

Controller/Annotations/View.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class View extends Template
3535
*/
3636
protected $serializerGroups;
3737

38+
/**
39+
* @var Boolean
40+
*/
41+
protected $populateDefaultVars = true;
42+
3843
/**
3944
* Returns the annotation alias name.
4045
*
@@ -97,4 +102,20 @@ public function getSerializerGroups()
97102
{
98103
return $this->serializerGroups;
99104
}
105+
106+
/**
107+
* @param Boolean $populateDefaultVars
108+
*/
109+
public function setPopulateDefaultVars($populateDefaultVars)
110+
{
111+
$this->populateDefaultVars = (Boolean) $populateDefaultVars;
112+
}
113+
114+
/**
115+
* @return Boolean
116+
*/
117+
public function isPopulateDefaultVars()
118+
{
119+
return $this->populateDefaultVars;
120+
}
100121
}

EventListener/ViewResponseListener.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,17 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
9696
$context->setGroups($configuration->getSerializerGroups());
9797
$view->setSerializationContext($context);
9898
}
99+
$fillDefaultTemplateVars = $configuration->isPopulateDefaultVars();
100+
} else {
101+
$fillDefaultTemplateVars = true;
99102
}
100103

101104
if (null === $view->getFormat()) {
102105
$view->setFormat($request->getRequestFormat());
103106
}
104107

105108
$vars = $request->attributes->get('_template_vars');
106-
if (!$vars) {
109+
if (!$vars && $fillDefaultTemplateVars) {
107110
$vars = $request->attributes->get('_template_default_vars');
108111
}
109112

Resources/doc/3-listener-support.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ public function getUsersAction()
144144
See the following example code for more details:
145145
https://github.com/liip/LiipHelloBundle/blob/master/Controller/ExtraController.php
146146

147+
The ViewResponse listener will automatically populate your view with request attributes if
148+
you do not provide any data when returning a view object. This behaviour comes from
149+
SensioFrameworkExtraBundle and will automatically add any variables listed in the
150+
`_template_default_vars` request attribute when no data is supplied. In some cases, this
151+
is not desirable and can be disabled by either supplying the data you want or disabling
152+
the automatic population of data with the @View annotation. :
153+
154+
```php
155+
/**
156+
* $user will no longer end up in the View's data.
157+
*
158+
* @View(populateDefaultVars=false)
159+
*/
160+
public function getUserDetails(User $user)
161+
{
162+
}
163+
```
164+
147165
### Body listener
148166

149167
The Request body decoding listener makes it possible to decode the contents of
@@ -195,7 +213,7 @@ fos_rest:
195213
```
196214
197215
Note: You will probably want to disable the automatic route generation (`@NoRoute`)
198-
for routes using the body converter, and instead define the routes manually to
216+
for routes using the body converter, and instead define the routes manually to
199217
avoid having the deserialized, typehinted objects (`$post in this example`) appear
200218
in the route as a parameter.
201219

Tests/EventListener/ViewResponseListenerTest.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,14 @@ public function testStatusCode($annotationCode, $viewCode, $expectedCode)
232232
$this->viewHandler->setContainer($this->container);
233233

234234
// This is why we avoid container dependencies!
235+
$that = $this;
235236
$this->container->expects($this->exactly(2))
236237
->method('get')
237238
->with($this->logicalOr('fos_rest.view_handler', 'fos_rest.templating'))
238-
->will($this->returnCallback(function ($service) {
239+
->will($this->returnCallback(function ($service) use ($that) {
239240
return $service === 'fos_rest.view_handler' ?
240-
$this->viewHandler :
241-
$this->templating;
241+
$that->viewHandler :
242+
$that->templating;
242243
}));
243244

244245
$this->templating->expects($this->any())
@@ -264,6 +265,58 @@ public function testStatusCode($annotationCode, $viewCode, $expectedCode)
264265
$this->assertSame($expectedCode, $response->getStatusCode());
265266
}
266267

268+
public function getDataForDefaultVarsCopy()
269+
{
270+
return array(
271+
array(true, false, false),
272+
array(true, true, true),
273+
array(false, null, true)
274+
);
275+
}
276+
277+
/**
278+
* @dataProvider getDataForDefaultVarsCopy
279+
*/
280+
public function testViewWithNoCopyDefaultVars($createAnnotation, $populateDefaultVars, $shouldCopy)
281+
{
282+
$request = new Request();
283+
$request->attributes->set('_template_default_vars', array('customer'));
284+
$request->attributes->set('customer', 'A person goes here');
285+
$view = View::create();
286+
287+
if ($createAnnotation) {
288+
$viewAnnotation = new ViewAnnotation(array());
289+
$viewAnnotation->setPopulateDefaultVars($populateDefaultVars);
290+
$request->attributes->set('_view', $viewAnnotation);
291+
}
292+
293+
$event = $this->getResponseEvent($request, $view);
294+
295+
$this->viewHandler = new ViewHandler(array('html' => true));
296+
$this->viewHandler->setContainer($this->container);
297+
298+
// This is why we avoid container dependencies!
299+
$that = $this;
300+
$this->container->expects($this->exactly(2))
301+
->method('get')
302+
->with($this->logicalOr('fos_rest.view_handler', 'fos_rest.templating'))
303+
->will($this->returnCallback(function ($service) use ($that) {
304+
return $service === 'fos_rest.view_handler' ?
305+
$that->viewHandler :
306+
$that->templating;
307+
}));
308+
309+
$this->listener->onKernelView($event);
310+
311+
$data = $view->getData();
312+
if ($shouldCopy) {
313+
$this->assertArrayHasKey('customer', $data);
314+
$this->assertEquals('A person goes here', $data['customer']);
315+
} else {
316+
$this->assertNull($data);
317+
}
318+
}
319+
267320
protected function setUp()
268321
{
269322
$this->viewHandler = $this->getMock('FOS\RestBundle\View\ViewHandlerInterface');

0 commit comments

Comments
 (0)