Skip to content

Commit 6eccc53

Browse files
committed
Merge pull request #628 from FriendsOfSymfony/inject_view_handler_params
inject the ViewHandler params to make them dynamically settable
2 parents 00ee0b2 + d409ab2 commit 6eccc53

File tree

4 files changed

+123
-44
lines changed

4 files changed

+123
-44
lines changed

Resources/config/view.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
<argument>%fos_rest.serialize_null%</argument>
2424
<argument>%fos_rest.force_redirects%</argument>
2525
<argument>%fos_rest.default_engine%</argument>
26+
<call method="setExclusionStrategyGroups">
27+
<argument>%fos_rest.serializer.exclusion_strategy.groups%</argument>
28+
</call>
29+
<call method="setExclusionStrategyVersion">
30+
<argument>%fos_rest.serializer.exclusion_strategy.version%</argument>
31+
</call>
32+
<call method="setSerializeNull">
33+
<argument>%fos_rest.serializer.serialize_null%</argument>
34+
</call>
2635
<call method="setContainer">
2736
<argument type="service" id="service_container" />
2837
</call>

Tests/View/ViewHandlerTest.php

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static function createResponseWithLocationDataProvider()
133133
public function testCreateResponseWithLocationAndData()
134134
{
135135
$testValue = array('naviter' => 'oudie');
136-
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
136+
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get'));
137137
$this->setupMockedSerializer($container, $testValue);
138138

139139
$viewHandler = new ViewHandler(array('json' => false));
@@ -203,9 +203,6 @@ public function testShouldReturnErrorResponseWhenDataContainsFormAndFormIsNotVal
203203

204204
$container->set('fos_rest.serializer', $serializer);
205205
$container->set('fos_rest.view.exception_wrapper_handler', new ExceptionWrapperHandler());
206-
$container->setParameter('fos_rest.serializer.exclusion_strategy.groups', 'foo');
207-
$container->setParameter('fos_rest.serializer.exclusion_strategy.version', '1.0');
208-
$container->setParameter('fos_rest.serializer.serialize_null', false);
209206

210207
//test
211208
$viewHandler = new ViewHandler(null, $expectedFailedValidationCode = Codes::HTTP_I_AM_A_TEAPOT);
@@ -238,7 +235,7 @@ public function testCreateResponseWithoutLocation($format, $expected, $createVie
238235
{
239236
$viewHandler = new ViewHandler(array('html' => true, 'json' => false));
240237

241-
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
238+
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get'));
242239
if ('html' === $format) {
243240
$templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\PhpEngine')
244241
->setMethods(array('render'))
@@ -319,17 +316,6 @@ function ($method) use ($serializer) {
319316
}
320317
)
321318
);
322-
323-
$map = array(
324-
array('fos_rest.serializer.exclusion_strategy.groups', 'foo'),
325-
array('fos_rest.serializer.exclusion_strategy.version', '1.0'),
326-
array('fos_rest.serializer.serialize_null', false)
327-
);
328-
329-
$container
330-
->expects($this->any())
331-
->method('getParameter')
332-
->will($this->returnValueMap($map));
333319
}
334320

335321
public static function createResponseWithoutLocationDataProvider()
@@ -348,7 +334,7 @@ public static function createResponseWithoutLocationDataProvider()
348334
public function testSerializeNull($expected, $serializeNull)
349335
{
350336
$viewHandler = new ViewHandler(array('json' => false), 404, 200, $serializeNull);
351-
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
337+
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get'));
352338

353339
$viewHandler->setContainer($container);
354340

@@ -396,20 +382,11 @@ public static function createSerializeNullDataProvider()
396382
public function testSerializeNullDataValues($expected, $serializeNull)
397383
{
398384
$viewHandler = new ViewHandler(array('json' => false), 404, 200);
399-
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
400-
401-
$viewHandler->setContainer($container);
385+
$viewHandler->setSerializeNullStrategy($serializeNull);
402386

403-
$map = array(
404-
array('fos_rest.serializer.exclusion_strategy.groups', 'foo'),
405-
array('fos_rest.serializer.exclusion_strategy.version', '1.0'),
406-
array('fos_rest.serializer.serialize_null', $serializeNull)
407-
);
387+
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get'));
408388

409-
$container
410-
->expects($this->any())
411-
->method('getParameter')
412-
->will($this->returnValueMap($map));
389+
$viewHandler->setContainer($container);
413390

414391
$view = new View();
415392
$context = $viewHandler->getSerializationContext($view);
@@ -549,4 +526,19 @@ public function prepareTemplateParametersDataProvider()
549526
'form is wrapped as form key' => array($form, array('form' => $formView, 'data' => $formView))
550527
);
551528
}
529+
530+
public function testConfigurableViewHandlerInterface()
531+
{
532+
//test
533+
$viewHandler = new ViewHandler();
534+
$viewHandler->setExclusionStrategyGroups('bar');
535+
$viewHandler->setExclusionStrategyVersion('1.1');
536+
$viewHandler->setSerializeNullStrategy(true);
537+
538+
$view = new View();
539+
$context = $viewHandler->getSerializationContext($view);
540+
$this->assertEquals(array('bar'), $context->attributes->get('groups')->getOrThrow(new \Exception('Serialization groups not set as expected')));
541+
$this->assertEquals('1.1', $context->attributes->get('version')->getOrThrow(new \Exception('Serialization version not set as expected')));
542+
$this->assertTrue($context->shouldSerializeNull());
543+
}
552544
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\View;
13+
14+
/**
15+
* Specialized ViewInterface that allows dynamic configuration of JMS serializer context aspects
16+
*
17+
* @author Lukas K. Smith <[email protected]>
18+
*/
19+
interface ConfigurableViewHandlerInterface extends ViewHandlerInterface
20+
{
21+
/**
22+
* Set the default serialization groups
23+
*
24+
* @param array $groups
25+
*/
26+
public function setExclusionStrategyGroups($groups);
27+
28+
/**
29+
* Set the default serialization version
30+
*
31+
* @param string $version
32+
*/
33+
public function setExclusionStrategyVersion($version);
34+
35+
/**
36+
* If nulls should be serialized
37+
*
38+
* @param Boolean $isEnabled
39+
*/
40+
public function setSerializeNullStrategy($isEnabled);
41+
}

View/ViewHandler.php

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
2424

2525
use FOS\RestBundle\Util\Codes;
26-
use FOS\RestBundle\Util\ExceptionWrapper;
2726

2827
/**
2928
* View may be used in controllers to build up a response in a format agnostic way
@@ -33,7 +32,7 @@
3332
* @author Jordi Boggiano <[email protected]>
3433
* @author Lukas K. Smith <[email protected]>
3534
*/
36-
class ViewHandler extends ContainerAware implements ViewHandlerInterface
35+
class ViewHandler extends ContainerAware implements ConfigurableViewHandlerInterface
3736
{
3837
/**
3938
* @var array key format, value a callable that returns a Response instance
@@ -70,6 +69,21 @@ class ViewHandler extends ContainerAware implements ViewHandlerInterface
7069
*/
7170
protected $defaultEngine;
7271

72+
/**
73+
* @var array
74+
*/
75+
protected $exclusionStrategyGroups = array();
76+
77+
/**
78+
* @var string
79+
*/
80+
protected $exclusionStrategyVersion;
81+
82+
/**
83+
* @var Boolean
84+
*/
85+
protected $serializeNullStrategy;
86+
7387
/**
7488
* Constructor
7589
*
@@ -96,6 +110,36 @@ public function __construct(
96110
$this->defaultEngine = $defaultEngine;
97111
}
98112

113+
/**
114+
* Set the default serialization groups
115+
*
116+
* @param array $groups
117+
*/
118+
public function setExclusionStrategyGroups($groups)
119+
{
120+
$this->exclusionStrategyGroups = (array) $groups;
121+
}
122+
123+
/**
124+
* Set the default serialization version
125+
*
126+
* @param string $version
127+
*/
128+
public function setExclusionStrategyVersion($version)
129+
{
130+
$this->exclusionStrategyVersion = $version;
131+
}
132+
133+
/**
134+
* If nulls should be serialized
135+
*
136+
* @param Boolean $isEnabled
137+
*/
138+
public function setSerializeNullStrategy($isEnabled)
139+
{
140+
$this->serializeNullStrategy = $isEnabled;
141+
}
142+
99143
/**
100144
* Verifies whether the given format is supported by this view
101145
*
@@ -200,23 +244,16 @@ public function getSerializationContext(View $view)
200244
{
201245
$context = $view->getSerializationContext();
202246

203-
if ($context->attributes->get('groups')->isEmpty()) {
204-
$groups = $this->container->getParameter('fos_rest.serializer.exclusion_strategy.groups');
205-
if ($groups) {
206-
$context->setGroups($groups);
207-
}
247+
if ($context->attributes->get('groups')->isEmpty() && $this->exclusionStrategyGroups) {
248+
$context->setGroups($this->exclusionStrategyGroups);
208249
}
209250

210-
if ($context->attributes->get('version')->isEmpty()) {
211-
$version = $this->container->getParameter('fos_rest.serializer.exclusion_strategy.version');
212-
if ($version) {
213-
$context->setVersion($version);
214-
}
251+
if ($context->attributes->get('version')->isEmpty() && $this->exclusionStrategyVersion) {
252+
$context->setVersion($this->exclusionStrategyVersion);
215253
}
216254

217-
if (null === $context->shouldSerializeNull()) {
218-
$serializeNull = $this->container->getParameter('fos_rest.serializer.serialize_null');
219-
$context->setSerializeNull($serializeNull);
255+
if (null === $context->shouldSerializeNull() && null !== $this->serializeNullStrategy) {
256+
$context->setSerializeNull($this->serializeNullStrategy);
220257
}
221258

222259
return $context;

0 commit comments

Comments
 (0)