Skip to content

Commit

Permalink
Create a Serializer adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhemN committed Jan 15, 2016
1 parent b8476e0 commit b264edc
Show file tree
Hide file tree
Showing 21 changed files with 992 additions and 226 deletions.
82 changes: 82 additions & 0 deletions Context/Adapter/JMSContextAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Context\Adapter;

use FOS\RestBundle\Context\Context;
use JMS\Serializer\Context as JMSContext;
use JMS\Serializer\DeserializationContext as JMSDeserializationContext;
use JMS\Serializer\SerializationContext as JMSSerializationContext;

/**
* BC FOSRestBundle < 2.0.
*
* @author Ener-Getick <egetick@gmail.com>
*
* @internal
*
* @todo Remove this in 2.0
*/
final class JMSContextAdapter
{
public static function convertSerializationContext(Context $context)
{
$newContext = JMSSerializationContext::create();
self::fillContext($context, $newContext);

return $newContext;
}

public static function convertDeserializationContext(Context $context)
{
$newContext = JMSDeserializationContext::create();
if (null !== $context->getMaxDepth()) {
for ($i = 0; $i < $context->getMaxDepth(); ++$i) {
$newContext->increaseDepth();
}
}

self::fillContext($context, $newContext);

return $newContext;
}

/**
* Fill a jms context.
*
* @param Context $context
* @param JMSContext $newContext
*
* @return JMSContext
*/
private static function fillContext(Context $context, JMSContext $newContext)
{
foreach ($context->getAttributes() as $key => $value) {
$newContext->attributes->set($key, $value);
}

if (null !== $context->getVersion()) {
$newContext->setVersion($context->getVersion());
}
$groups = $context->getGroups();
if (!empty($groups)) {
$newContext->setGroups($context->getGroups());
}
if (null !== $context->getMaxDepth()) {
$newContext->enableMaxDepthChecks();
}
if (null !== $context->getSerializeNull()) {
$newContext->setSerializeNull($context->getSerializeNull());
}

return $newContext;
}
}
206 changes: 206 additions & 0 deletions Context/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Context;

/**
* Stores the serialization or deserialization context (groups, version, ...).
*
* @author Ener-Getick <egetick@gmail.com>
*/
final class Context
{
/**
* @var array
*/
private $attributes = array();
/**
* @var int|null
*/
private $version;
/**
* @var array
*/
private $groups = array();
/**
* @var int
*/
private $maxDepth;
/**
* @var bool
*/
private $serializeNull;

/**
* Sets an attribute.
*
* @param string $key
* @param mixed $value
*
* @return self
*/
public function setAttribute($key, $value)
{
$this->attributes[$key] = $value;

return $this;
}

/**
* Checks if contains a normalization attribute.
*
* @param string $key
*
* @return bool
*/
public function hasAttribute($key)
{
return isset($this->attributes[$key]);
}

/**
* Gets an attribute.
*
* @param string $key
*
* @return mixed
*/
public function getAttribute($key)
{
if (isset($this->attributes[$key])) {
return $this->attributes[$key];
}
}

/**
* Gets the attributes.
*
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* Sets the normalization version.
*
* @param int|null $version
*
* @return self
*/
public function setVersion($version)
{
$this->version = $version;

return $this;
}

/**
* Gets the normalization version.
*
* @return int|null
*/
public function getVersion()
{
return $this->version;
}

/**
* Adds a normalization group.
*
* @param string $group
*
* @return self
*/
public function addGroup($group)
{
if (!in_array($group, $this->groups)) {
$this->groups[] = $group;
}

return $this;
}

/**
* Adds normalization groups.
*
* @param string[] $groups
*
* @return self
*/
public function addGroups(array $groups)
{
foreach ($groups as $group) {
$this->addGroup($group);
}

return $this;
}

/**
* Gets the normalization groups.
*
* @return array
*/
public function getGroups()
{
return $this->groups;
}

/**
* Sets the normalization max depth.
*
* @param int|null $depth
*
* @return self
*/
public function setMaxDepth($maxDepth)
{
$this->maxDepth = $maxDepth;

return $this;
}

/**
* Gets the normalization max depth.
*
* @return int|null
*/
public function getMaxDepth()
{
return $this->maxDepth;
}

/**
* Sets serialize null.
*
* @param bool|null $serializeNull
*
* @return self
*/
public function setSerializeNull($serializeNull)
{
$this->serializeNull = $serializeNull;

return $this;
}

/**
* Gets serialize null.
*
* @return bool|null
*/
public function getSerializeNull()
{
return $this->serializeNull;
}
}
32 changes: 29 additions & 3 deletions DependencyInjection/Compiler/SerializerConfigurationPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace FOS\RestBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Checks if a serializer is either set or can be auto-configured.
Expand All @@ -27,6 +27,16 @@ class SerializerConfigurationPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
if ($container->has('fos_rest.serializer')) {
$class = $container->getParameterBag()->resolveValue(
$container->findDefinition('fos_rest.serializer')->getClass()
);
if (!is_subclass_of($class, 'FOS\RestBundle\Serializer\Serializer')) {
@trigger_error('Support of custom serializers is deprecated since 1.8 and will be dropped in 2.0. You should create a new class implementing FOS\RestBundle\Serializer\Serializer and define it as fos_rest.serializer', E_USER_DEPRECATED);

// @todo: Uncomment in 2.0
// throw new \InvalidArgumentException(sprintf('"fos_rest.serializer" must implement FOS\RestBundle\Serializer\Serializer (instance of "%s" given).', $class));
}

return;
}

Expand All @@ -35,13 +45,29 @@ public function process(ContainerBuilder $container)
}

if ($container->has('jms_serializer.serializer')) {
$container->setAlias('fos_rest.serializer', 'jms_serializer.serializer');
$container->setAlias('fos_rest.serializer', 'fos_rest.serializer.jms');
} else {
$container->removeDefinition('fos_rest.serializer.exception_wrapper_serialize_handler');
}

if ($container->has('serializer')) {
$container->setAlias('fos_rest.serializer', 'serializer');
$class = $container->getParameterBag()->resolveValue(
$container->findDefinition('serializer')->getClass()
);

if (is_subclass_of($class, 'Symfony\Component\Serializer\SerializerInterface')) {
$container->setAlias('fos_rest.serializer', 'fos_rest.serializer.symfony');
} elseif (is_subclass_of($class, 'JMS\Serializer\SerializerInterface')) {
$container->setAlias('fos_rest.serializer', 'fos_rest.serializer.jms');
} elseif (is_subclass_of($class, 'FOS\RestBundle\Serializer\Serializer')) {
$container->setAlias('fos_rest.serializer', 'serializer');
} else {
@trigger_error('Support of custom serializers is deprecated since 1.8 and will be dropped in 2.0. You should create a new class implementing FOS\RestBundle\Serializer\Serializer and define it as fos_rest.serializer', E_USER_DEPRECATED);
$container->setAlias('fos_rest.serializer', 'serializer');

// @todo: Uncomment in 2.0
// throw new \InvalidArgumentException(sprintf('"fos_rest.serializer" must implement FOS\RestBundle\Serializer\Serializer (instance of "%s" given).', $class));
}
} else {
$container->removeDefinition('fos_rest.serializer.exception_wrapper_normalizer');
}
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/FOSRestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('routing.xml');
$loader->load('util.xml');
$loader->load('request.xml');
$loader->load('serializer.xml');

$container->setParameter('fos_rest.cache_dir', $config['cache_dir']);
$container->setParameter('fos_rest.routing.loader.default_format', $config['routing_loader']['default_format']);
Expand Down
Loading

0 comments on commit b264edc

Please sign in to comment.