feature #19326 [Serializer][FrameworkBundle] Add a YAML encoder (dunglas)
This PR was squashed before being merged into the 3.2-dev branch (closes #19326).
Discussion
----------
[Serializer][FrameworkBundle] Add a YAML encoder
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | n/a
| License | MIT
| Doc PR | todo
Add YAML support to the Serializer.
Commits
-------
9366a7d
[Serializer][FrameworkBundle] Add a YAML encoder
This commit is contained in:
commit
d6d6a47fcf
@ -26,11 +26,13 @@ use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessor;
|
||||
use Symfony\Component\Serializer\Encoder\YamlEncoder;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
|
||||
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
|
||||
use Symfony\Component\Workflow;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
/**
|
||||
* FrameworkExtension.
|
||||
@ -1037,6 +1039,12 @@ class FrameworkExtension extends Extension
|
||||
$definition->addTag('serializer.normalizer', array('priority' => -900));
|
||||
}
|
||||
|
||||
if (class_exists(YamlEncoder::class) && defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT')) {
|
||||
$definition = $container->register('serializer.encoder.yaml', YamlEncoder::class);
|
||||
$definition->setPublic(false);
|
||||
$definition->addTag('serializer.encoder');
|
||||
}
|
||||
|
||||
$loader->load('serializer.xml');
|
||||
$chainLoader = $container->getDefinition('serializer.mapping.chain_loader');
|
||||
|
||||
|
73
src/Symfony/Component/Serializer/Encoder/YamlEncoder.php
Normal file
73
src/Symfony/Component/Serializer/Encoder/YamlEncoder.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Serializer\Encoder;
|
||||
|
||||
use Symfony\Component\Yaml\Dumper;
|
||||
use Symfony\Component\Yaml\Parser;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
/**
|
||||
* Encodes YAML data.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class YamlEncoder implements EncoderInterface, DecoderInterface
|
||||
{
|
||||
const FORMAT = 'yaml';
|
||||
|
||||
private $dumper;
|
||||
private $parser;
|
||||
private $defaultContext = array('yaml_inline' => 0, 'yaml_indent' => 0, 'yaml_flags' => 0);
|
||||
|
||||
public function __construct(Dumper $dumper = null, Parser $parser = null, array $defaultContext = array())
|
||||
{
|
||||
$this->dumper = $dumper ?: new Dumper();
|
||||
$this->parser = $parser ?: new Parser();
|
||||
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode($data, $format, array $context = array())
|
||||
{
|
||||
$context = array_merge($this->defaultContext, $context);
|
||||
|
||||
return $this->dumper->dump($data, $context['yaml_inline'], $context['yaml_indent'], $context['yaml_flags']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsEncoding($format)
|
||||
{
|
||||
return self::FORMAT === $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function decode($data, $format, array $context = array())
|
||||
{
|
||||
$context = array_merge($this->defaultContext, $context);
|
||||
|
||||
return Yaml::parse($data, $context['yaml_flags']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDecoding($format)
|
||||
{
|
||||
return self::FORMAT === $format;
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Serializer\Tests\Encoder;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\YamlEncoder;
|
||||
use Symfony\Component\Yaml\Dumper;
|
||||
use Symfony\Component\Yaml\Parser;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class YamlEncoderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testEncode()
|
||||
{
|
||||
$encoder = new YamlEncoder();
|
||||
|
||||
$this->assertEquals('foo', $encoder->encode('foo', 'yaml'));
|
||||
$this->assertEquals('{ foo: 1 }', $encoder->encode(array('foo' => 1), 'yaml'));
|
||||
}
|
||||
|
||||
public function testSupportsEncoding()
|
||||
{
|
||||
$encoder = new YamlEncoder();
|
||||
|
||||
$this->assertTrue($encoder->supportsEncoding('yaml'));
|
||||
$this->assertFalse($encoder->supportsEncoding('json'));
|
||||
}
|
||||
|
||||
public function testDecode()
|
||||
{
|
||||
$encoder = new YamlEncoder();
|
||||
|
||||
$this->assertEquals('foo', $encoder->decode('foo', 'yaml'));
|
||||
$this->assertEquals(array('foo' => 1), $encoder->decode('{ foo: 1 }', 'yaml'));
|
||||
}
|
||||
|
||||
public function testSupportsDecoding()
|
||||
{
|
||||
$encoder = new YamlEncoder();
|
||||
|
||||
$this->assertTrue($encoder->supportsDecoding('yaml'));
|
||||
$this->assertFalse($encoder->supportsDecoding('json'));
|
||||
}
|
||||
|
||||
public function testContext()
|
||||
{
|
||||
$encoder = new YamlEncoder(new Dumper(), new Parser(), array('yaml_inline' => 1, 'yaml_indent' => 4, 'yaml_flags' => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT));
|
||||
|
||||
$obj = new \stdClass();
|
||||
$obj->bar = 2;
|
||||
|
||||
$this->assertEquals(" foo: !php/object:O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}\n", $encoder->encode(array('foo' => $obj), 'yaml'));
|
||||
$this->assertEquals(' { foo: null }', $encoder->encode(array('foo' => $obj), 'yaml', array('yaml_inline' => 0, 'yaml_indent' => 2, 'yaml_flags' => 0)));
|
||||
$this->assertEquals(array('foo' => $obj), $encoder->decode('foo: !php/object:O:8:"stdClass":1:{s:3:"bar";i:2;}', 'yaml'));
|
||||
$this->assertEquals(array('foo' => null), $encoder->decode('foo: !php/object:O:8:"stdClass":1:{s:3:"bar";i:2;}', 'yaml', array('yaml_flags' => 0)));
|
||||
}
|
||||
}
|
@ -19,7 +19,7 @@
|
||||
"php": ">=5.5.9"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/yaml": "~2.8|~3.0",
|
||||
"symfony/yaml": "~3.1",
|
||||
"symfony/config": "~2.8|~3.0",
|
||||
"symfony/property-access": "~2.8|~3.0",
|
||||
"symfony/http-foundation": "~2.8|~3.0",
|
||||
@ -30,7 +30,8 @@
|
||||
"phpdocumentor/reflection-docblock": "~3.0"
|
||||
},
|
||||
"conflict": {
|
||||
"symfony/property-access": ">=3.0,<3.0.4|>=2.8,<2.8.4"
|
||||
"symfony/property-access": ">=3.0,<3.0.4|>=2.8,<2.8.4",
|
||||
"symfony/yaml": "<3.1"
|
||||
},
|
||||
"suggest": {
|
||||
"psr/cache-implementation": "For using the metadata cache.",
|
||||
|
Reference in New Issue
Block a user