Skip to content

Commit

Permalink
Add serializer
Browse files Browse the repository at this point in the history
Update docs/reference/installation.rst

Co-Authored-By: Grégoire Paris <[email protected]>

Update src/Bridge/Symfony/DependencyInjection/Configuration.php

Co-Authored-By: Grégoire Paris <[email protected]>

Update src/Bridge/Symfony/DependencyInjection/Configuration.php

Co-Authored-By: Grégoire Paris <[email protected]>

Update src/Bridge/Symfony/DependencyInjection/SonataFormExtension.php

Co-Authored-By: Grégoire Paris <[email protected]>

Update src/Bridge/Symfony/DependencyInjection/SonataFormExtension.php

Co-Authored-By: Grégoire Paris <[email protected]>

Update src/Serializer/BaseSerializerHandler.php

Co-Authored-By: Javier Spagnoletti <[email protected]>

Update src/Bridge/Symfony/DependencyInjection/SonataFormExtension.php

Co-Authored-By: Javier Spagnoletti <[email protected]>
  • Loading branch information
2 people authored and greg0ire committed Apr 10, 2020
1 parent 730cfbd commit f5408fd
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"require": {
"php": "^7.2",
"jms/metadata": "^2.1",
"jms/serializer": "^2.0 || ^3.0",
"sonata-project/doctrine-extensions": "^1.1",
"symfony/event-dispatcher": "^3.4 || ^4.3 || ^5.0",
"symfony/form": "^3.4.24 || ^4.3 || ^5.0",
"symfony/options-resolver": "^3.4 || ^4.3 || ^5.0",
Expand Down
13 changes: 13 additions & 0 deletions docs/reference/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ corresponding configuration node accordingly:
sonata_form:
form_type: horizontal
If you are using ``jms/serializer`` subscribing methods you will need to configure the
return formats:

.. configuration-block::

.. code-block:: yaml
# config/packages/sonata_form.yaml
sonata_form:
serializer:
formats: ['json', 'xml', 'yml']
.. note::

If you are not using Symfony Flex, this configuration should be added
Expand Down
22 changes: 22 additions & 0 deletions src/Bridge/Symfony/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function getConfigTreeBuilder(): TreeBuilder
}

$this->addFlashMessageSection($rootNode);
$this->addSerializerFormats($rootNode);

return $treeBuilder;
}
Expand All @@ -62,4 +63,25 @@ private function addFlashMessageSection(ArrayNodeDefinition $node): void
->end()
;
}

/**
* Adds configuration for serializer formats.
*/
private function addSerializerFormats(ArrayNodeDefinition $node): void
{
$node
->children()
->arrayNode('serializer')
->addDefaultsIfNotSet()
->children()
->arrayNode('formats')
->prototype('scalar')->end()
->defaultValue(['json', 'xml', 'yml'])
->info('Default serializer formats, will be used while getting subscribing methods.')
->end()
->end()
->end()
->end()
;
}
}
14 changes: 14 additions & 0 deletions src/Bridge/Symfony/DependencyInjection/SonataFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Sonata\Form\Bridge\Symfony\DependencyInjection;

use JMS\Serializer\Handler\SubscribingHandlerInterface;
use Sonata\Form\Serializer\BaseSerializerHandler;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -51,5 +53,17 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('validator.xml');

$container->setParameter('sonata.form.form_type', $config['form_type']);

$this->configureSerializerFormats($config);
}

/**
* @param mixed[] $config
*/
private function configureSerializerFormats(array $config): void
{
if (interface_exists(SubscribingHandlerInterface::class)) {
BaseSerializerHandler::setFormats($config['serializer']['formats']);
}
}
}
96 changes: 96 additions & 0 deletions src/Serializer/BaseSerializerHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\Form\Serializer;

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

/**
* @author Sylvain Deloux <[email protected]>
*/
abstract class BaseSerializerHandler implements SerializerHandlerInterface
{
/**
* @var ManagerInterface
*/
protected $manager;

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

public function __construct(ManagerInterface $manager)
{
$this->manager = $manager;
}

final public static function setFormats(array $formats): void
{
static::$formats = $formats;
}

final public static function addFormat(string $format): void
{
static::$formats[] = $format;
}

public static function getSubscribingMethods(): array
{
$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.
*/
public function serializeObjectToId(VisitorInterface $visitor, object $data, array $type, Context $context): ?int
{
$className = $this->manager->getClass();

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

return null;
}

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

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\Form\Serializer;

use JMS\Serializer\Handler\SubscribingHandlerInterface;

/**
* @author Sylvain Deloux <[email protected]>
*/
interface SerializerHandlerInterface extends SubscribingHandlerInterface
{
public static function getType(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public function testProcessedConfigurationLooksAsExpected(): void
['form_type' => 'standard'], // by this during the merge
], [
'form_type' => 'standard',
'serializer' => [
'formats' => ['json', 'xml', 'yml'],
],
]);
}

Expand All @@ -52,6 +55,9 @@ public function testDefault(): void
[],
], [
'form_type' => 'standard',
'serializer' => [
'formats' => ['json', 'xml', 'yml'],
],
]);
}
}

0 comments on commit f5408fd

Please sign in to comment.