Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Commit

Permalink
Moved serializer components to Serializer namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 authored and OskarStark committed Dec 10, 2018
1 parent 7149644 commit 0dcfde8
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 105 deletions.
6 changes: 6 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ All twig classes have been deprecated and were moved to the `Sonata\Twig\` subna
All meta classes have been deprecated and were moved to the `SonataBlockBundle`:
- `Sonata\CoreBundle\Model\Metadata` => `Sonata\BlockBundle\Meta\Metadata`
- `Sonata\CoreBundle\Model\MetadataInterface` => `Sonata\BlockBundle\Meta\MetadataInterface`

### Serializer deprecations

All serializer classes have been deprecated and were moved to the `Sonata\Serializer\` subnamespace:
- `Sonata\CoreBundle\Serializer\BaseSerializerHandler` => `Sonata\Serializer\BaseSerializerHandler`
- `Sonata\CoreBundle\Serializer\SerializerHandlerInterface` => `Sonata\Serializer\SerializerHandlerInterface`

UPGRADE FROM 3.6 to 3.7
=======================
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@
"psr-4": {
"Sonata\\CoreBundle\\": "src/CoreBundle/",
"Sonata\\Form\\": "src/Form/",
"Sonata\\Serializer\\": "src/Serializer/",
"Sonata\\Twig\\": "src/Twig/"
}
},
"autoload-dev": {
"psr-4": {
"Sonata\\CoreBundle\\Tests\\": "tests/CoreBundle/",
"Sonata\\Form\\Tests\\": "tests/Form/",
"Sonata\\Serializer\\Tests\\": "tests/Serializer/",
"Sonata\\Twig\\Tests\\": "tests/Twig/"
}
}
Expand Down
109 changes: 7 additions & 102 deletions src/CoreBundle/Serializer/BaseSerializerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,110 +11,15 @@

namespace Sonata\CoreBundle\Serializer;

use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\VisitorInterface;
use Sonata\CoreBundle\Model\ManagerInterface;
@trigger_error(
'The '.__NAMESPACE__.'\BaseSerializerHandler class is deprecated since version 3.x and will be removed in 4.0.'
.' Use Sonata\Serializer\BaseSerializerHandler instead.',
E_USER_DEPRECATED
);

/**
* @author Sylvain Deloux <sylvain.deloux@ekino.com>
* @deprecated Since version 3.x, to be removed in 4.0.
*/
abstract class BaseSerializerHandler implements SerializerHandlerInterface
abstract class BaseSerializerHandler extends \Sonata\Serializer\BaseSerializerHandler implements SerializerHandlerInterface
{
/**
* @var ManagerInterface
*/
protected $manager;

/**
* @var string[]
*/
protected static $formats;

/**
* @param ManagerInterface $manager
*/
public function __construct(ManagerInterface $manager)
{
$this->manager = $manager;
}

/**
* @param string[] $formats
*/
final public static function setFormats(array $formats)
{
static::$formats = $formats;
}

/**
* @param string $format
*/
final public static function addFormat($format)
{
static::$formats[] = $format;
}

public static function getSubscribingMethods()
{
// NEXT_MAJOR : remove this block
if (null === static::$formats) {
static::$formats = ['json', 'xml', 'yml'];
@trigger_error(
'$formats has been set to default array("json", "xml", "yml"). Setting $formats to a
default array is deprecated since version 3.0 and will be removed in 4.0. Use SonataCoreBundle
configuration to add default serializer formats.',
E_USER_DEPRECATED
);
}

$type = static::getType();
$methods = [];

foreach (static::$formats as $format) {
$methods[] = [
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
'format' => $format,
'type' => $type,
'method' => 'serializeObjectToId',
];

$methods[] = [
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
'format' => $format,
'type' => $type,
'method' => 'deserializeObjectFromId',
];
}

return $methods;
}

/**
* Serialize data object to id.
*
* @param object $data
*
* @return int|null
*/
public function serializeObjectToId(VisitorInterface $visitor, $data, array $type, Context $context)
{
$className = $this->manager->getClass();

if ($data instanceof $className) {
return $visitor->visitInteger($data->getId(), $type, $context);
}
}

/**
* Deserialize object from its id.
*
* @param int $data
*
* @return null|object
*/
public function deserializeObjectFromId(VisitorInterface $visitor, $data, array $type)
{
return $this->manager->findOneBy(['id' => $data]);
}
}
10 changes: 7 additions & 3 deletions src/CoreBundle/Serializer/SerializerHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

namespace Sonata\CoreBundle\Serializer;

use JMS\Serializer\Handler\SubscribingHandlerInterface;
@trigger_error(
'The '.__NAMESPACE__.'\SerializerHandlerInterface class is deprecated since version 3.x and will be removed in 4.0.'
.' Use Sonata\Serializer\SerializerHandlerInterface instead.',
E_USER_DEPRECATED
);

/**
* @author Sylvain Deloux <sylvain.deloux@ekino.com>
* @deprecated Since version 3.x, to be removed in 4.0.
*/
interface SerializerHandlerInterface extends SubscribingHandlerInterface
interface SerializerHandlerInterface extends \Sonata\Serializer\SerializerHandlerInterface
{
/**
* @return string
Expand Down
120 changes: 120 additions & 0 deletions src/Serializer/BaseSerializerHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\Serializer;

use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\VisitorInterface;
use Sonata\CoreBundle\Model\ManagerInterface;

/**
* @author Sylvain Deloux <sylvain.deloux@ekino.com>
*/
abstract class BaseSerializerHandler implements SerializerHandlerInterface
{
/**
* @var ManagerInterface
*/
protected $manager;

/**
* @var string[]
*/
protected static $formats;

/**
* @param ManagerInterface $manager
*/
public function __construct(ManagerInterface $manager)
{
$this->manager = $manager;
}

/**
* @param string[] $formats
*/
final public static function setFormats(array $formats)
{
static::$formats = $formats;
}

/**
* @param string $format
*/
final public static function addFormat($format)
{
static::$formats[] = $format;
}

public static function getSubscribingMethods()
{
// NEXT_MAJOR : remove this block
if (null === static::$formats) {
static::$formats = ['json', 'xml', 'yml'];
@trigger_error(
'$formats has been set to default array("json", "xml", "yml"). Setting $formats to a
default array is deprecated since version 3.0 and will be removed in 4.0. Use SonataCoreBundle
configuration to add default serializer formats.',
E_USER_DEPRECATED
);
}

$type = static::getType();
$methods = [];

foreach (static::$formats as $format) {
$methods[] = [
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
'format' => $format,
'type' => $type,
'method' => 'serializeObjectToId',
];

$methods[] = [
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
'format' => $format,
'type' => $type,
'method' => 'deserializeObjectFromId',
];
}

return $methods;
}

/**
* Serialize data object to id.
*
* @param object $data
*
* @return int|null
*/
public function serializeObjectToId(VisitorInterface $visitor, $data, array $type, Context $context)
{
$className = $this->manager->getClass();

if ($data instanceof $className) {
return $visitor->visitInteger($data->getId(), $type, $context);
}
}

/**
* Deserialize object from its id.
*
* @param int $data
*
* @return null|object
*/
public function deserializeObjectFromId(VisitorInterface $visitor, $data, array $type)
{
return $this->manager->findOneBy(['id' => $data]);
}
}
25 changes: 25 additions & 0 deletions src/Serializer/SerializerHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\Serializer;

use JMS\Serializer\Handler\SubscribingHandlerInterface;

/**
* @author Sylvain Deloux <sylvain.deloux@ekino.com>
*/
interface SerializerHandlerInterface extends SubscribingHandlerInterface
{
/**
* @return string
*/
public static function getType();
}
Loading

0 comments on commit 0dcfde8

Please sign in to comment.