diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index dc9b10a33..5cd11ce4c 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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(); diff --git a/DependencyInjection/FOSRestExtension.php b/DependencyInjection/FOSRestExtension.php index a73f82b45..31e55bbdd 100644 --- a/DependencyInjection/FOSRestExtension.php +++ b/DependencyInjection/FOSRestExtension.php @@ -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']); @@ -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']); @@ -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. */ diff --git a/Tests/View/ViewHandlerTest.php b/Tests/View/ViewHandlerTest.php index e3039d781..8dc496c22 100644 --- a/Tests/View/ViewHandlerTest.php +++ b/Tests/View/ViewHandlerTest.php @@ -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')) @@ -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') @@ -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); diff --git a/View/View.php b/View/View.php index 207a5f249..da8e71534 100644 --- a/View/View.php +++ b/View/View.php @@ -66,6 +66,11 @@ class View */ private $route; + /** + * @var string + */ + private $objectsVersion; + /** * Convenience method to allow for a fluent interface. * @@ -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 * @@ -318,4 +336,14 @@ public function getRoute() { return $this->route; } + + /** + * get the objects version + * + * @return string objects version + */ + public function getObjectsVersion() + { + return $this->objectsVersion; + } } diff --git a/View/ViewHandler.php b/View/ViewHandler.php index 9c7237dd7..bf8b7c789 100644 --- a/View/ViewHandler.php +++ b/View/ViewHandler.php @@ -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) @@ -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 * @@ -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()); }