Skip to content

Commit

Permalink
Merge pull request #209 from cystbear/addSerializerVersioning
Browse files Browse the repository at this point in the history
Add serializer versioning
  • Loading branch information
lsmith77 committed Mar 24, 2012
2 parents ff3ab10 + 49ab31d commit 557833b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -54,6 +54,7 @@ public function getConfigTreeBuilder()
->scalarNode('view_handler')->defaultValue('fos_rest.view_handler.default')->end()
->end()
->end()
->scalarNode('objects_version')->defaultNull()->end()
->end()
->end();

Expand Down
6 changes: 4 additions & 2 deletions DependencyInjection/FOSRestExtension.php
Expand Up @@ -52,6 +52,8 @@ public function load(array $configs, ContainerBuilder $container)
foreach ($config['service'] as $key => $service) {
$container->setAlias($this->getAlias().'.'.$key, $config['service'][$key]);
}
$container->setParameter($this->getAlias().'.objects_version', $config['objects_version']);

$container->setParameter($this->getAlias().'.formats', $formats);
$container->setParameter($this->getAlias().'.default_engine', $config['view']['default_engine']);

Expand Down Expand Up @@ -83,7 +85,7 @@ public function load(array $configs, ContainerBuilder $container)
foreach ($config['exception']['messages'] as $exception => $message) {
$this->testExceptionExists($exception);
}

$container->setParameter($this->getAlias().'.exception.codes', $config['exception']['codes']);
$container->setParameter($this->getAlias().'.exception.messages', $config['exception']['messages']);

Expand Down Expand Up @@ -116,7 +118,7 @@ public function load(array $configs, ContainerBuilder $container)

/**
* Check if an exception is loadable.
*
*
* @param string $exception class to test
* @throws InvalidArgumentException if the class was not found.
*/
Expand Down
10 changes: 8 additions & 2 deletions Tests/View/ViewHandlerTest.php
Expand Up @@ -133,7 +133,7 @@ public function testCreateResponseWithoutLocation($format, $expected, $createVie
{
$viewHandler = new ViewHandler(array('html' => true, 'json' => false));

$container = $this->getMock('\Symfony\Component\DependencyInjection\Container', array('get'));
$container = $this->getMock('\Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
if ('html' === $format) {
$templating = $this->getMockBuilder('\Symfony\Bundle\FrameworkBundle\Templating\PhpEngine')
->setMethods(array('render'))
Expand All @@ -150,7 +150,7 @@ public function testCreateResponseWithoutLocation($format, $expected, $createVie
->with('fos_rest.templating')
->will($this->returnValue($templating));
} else {
$serializer = $this->getMock('\stdClass', array('serialize'));
$serializer = $this->getMock('\stdClass', array('serialize', 'setVersion'));
$serializer
->expects($this->once())
->method('serialize')
Expand All @@ -161,6 +161,12 @@ public function testCreateResponseWithoutLocation($format, $expected, $createVie
->method('get')
->with('fos_rest.serializer')
->will($this->returnValue($serializer));

$container
->expects($this->once())
->method('getParameter')
->with('fos_rest.objects_version')
->will($this->returnValue('1.0'));
}

$viewHandler->setContainer($container);
Expand Down
28 changes: 28 additions & 0 deletions View/View.php
Expand Up @@ -66,6 +66,11 @@ class View
*/
private $route;

/**
* @var string
*/
private $objectsVersion;

/**
* Convenience method to allow for a fluent interface.
*
Expand Down Expand Up @@ -146,6 +151,19 @@ public function setStatusCode($code)
return $this;
}

/**
* set the serializer objects version
*
* @param $objectsVersion
* @return View
*/
public function setObjectsVersion($objectsVersion)
{
$this->objectsVersion = $objectsVersion;

return $this;
}

/**
* Sets template to use for the encoding
*
Expand Down Expand Up @@ -318,4 +336,14 @@ public function getRoute()
{
return $this->route;
}

/**
* get the objects version
*
* @return string objects version
*/
public function getObjectsVersion()
{
return $this->objectsVersion;
}
}
24 changes: 20 additions & 4 deletions View/ViewHandler.php
Expand Up @@ -133,7 +133,7 @@ private function getStatusCode(View $view)
* If the given format uses the templating system for rendering
*
* @param string $format
*
*
* @return Boolean
*/
public function isFormatTemplating($format)
Expand Down Expand Up @@ -161,6 +161,18 @@ protected function getSerializer()
return $this->container->get('fos_rest.serializer');
}

/**
* Get the serializer objects version
*
* @param View $view
*
* @return string|null "Objects versioning" version
*/
protected function getObjectsVersion(View $view)
{
return $view->getObjectsVersion() ?: $this->container->getParameter('fos_rest.objects_version');
}

/**
* Get the templating service
*
Expand Down Expand Up @@ -307,9 +319,13 @@ public function createResponse(View $view, Request $request, $format)
return $this->createRedirectResponse($view, $location, $format);
}

$content = $this->isFormatTemplating($format)
? $this->renderTemplate($view, $format)
: $this->getSerializer()->serialize($view->getData(), $format);
if ($this->isFormatTemplating($format)) {
$content = $this->renderTemplate($view, $format);
} else {
$serializer = $this->getSerializer();
$serializer->setVersion($this->getObjectsVersion($view));
$content = $serializer->serialize($view->getData(), $format);
}

return new Response($content, $this->getStatusCode($view), $view->getHeaders());
}
Expand Down

0 comments on commit 557833b

Please sign in to comment.