Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6d6a47

Browse files
committed
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
2 parents fa18a4f + 9366a7d commit d6d6a47

File tree

4 files changed

+152
-2
lines changed

4 files changed

+152
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2727
use Symfony\Component\Config\FileLocator;
2828
use Symfony\Component\PropertyAccess\PropertyAccessor;
29+
use Symfony\Component\Serializer\Encoder\YamlEncoder;
2930
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
3031
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
3132
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
3233
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
3334
use Symfony\Component\Workflow;
35+
use Symfony\Component\Yaml\Yaml;
3436

3537
/**
3638
* FrameworkExtension.
@@ -1037,6 +1039,12 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
10371039
$definition->addTag('serializer.normalizer', array('priority' => -900));
10381040
}
10391041

1042+
if (class_exists(YamlEncoder::class) && defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT')) {
1043+
$definition = $container->register('serializer.encoder.yaml', YamlEncoder::class);
1044+
$definition->setPublic(false);
1045+
$definition->addTag('serializer.encoder');
1046+
}
1047+
10401048
$loader->load('serializer.xml');
10411049
$chainLoader = $container->getDefinition('serializer.mapping.chain_loader');
10421050

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Encoder;
13+
14+
use Symfony\Component\Yaml\Dumper;
15+
use Symfony\Component\Yaml\Parser;
16+
use Symfony\Component\Yaml\Yaml;
17+
18+
/**
19+
* Encodes YAML data.
20+
*
21+
* @author Kévin Dunglas <[email protected]>
22+
*/
23+
class YamlEncoder implements EncoderInterface, DecoderInterface
24+
{
25+
const FORMAT = 'yaml';
26+
27+
private $dumper;
28+
private $parser;
29+
private $defaultContext = array('yaml_inline' => 0, 'yaml_indent' => 0, 'yaml_flags' => 0);
30+
31+
public function __construct(Dumper $dumper = null, Parser $parser = null, array $defaultContext = array())
32+
{
33+
$this->dumper = $dumper ?: new Dumper();
34+
$this->parser = $parser ?: new Parser();
35+
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function encode($data, $format, array $context = array())
42+
{
43+
$context = array_merge($this->defaultContext, $context);
44+
45+
return $this->dumper->dump($data, $context['yaml_inline'], $context['yaml_indent'], $context['yaml_flags']);
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function supportsEncoding($format)
52+
{
53+
return self::FORMAT === $format;
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function decode($data, $format, array $context = array())
60+
{
61+
$context = array_merge($this->defaultContext, $context);
62+
63+
return Yaml::parse($data, $context['yaml_flags']);
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function supportsDecoding($format)
70+
{
71+
return self::FORMAT === $format;
72+
}
73+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Encoder;
13+
14+
use Symfony\Component\Serializer\Encoder\YamlEncoder;
15+
use Symfony\Component\Yaml\Dumper;
16+
use Symfony\Component\Yaml\Parser;
17+
use Symfony\Component\Yaml\Yaml;
18+
19+
/**
20+
* @author Kévin Dunglas <[email protected]>
21+
*/
22+
class YamlEncoderTest extends \PHPUnit_Framework_TestCase
23+
{
24+
public function testEncode()
25+
{
26+
$encoder = new YamlEncoder();
27+
28+
$this->assertEquals('foo', $encoder->encode('foo', 'yaml'));
29+
$this->assertEquals('{ foo: 1 }', $encoder->encode(array('foo' => 1), 'yaml'));
30+
}
31+
32+
public function testSupportsEncoding()
33+
{
34+
$encoder = new YamlEncoder();
35+
36+
$this->assertTrue($encoder->supportsEncoding('yaml'));
37+
$this->assertFalse($encoder->supportsEncoding('json'));
38+
}
39+
40+
public function testDecode()
41+
{
42+
$encoder = new YamlEncoder();
43+
44+
$this->assertEquals('foo', $encoder->decode('foo', 'yaml'));
45+
$this->assertEquals(array('foo' => 1), $encoder->decode('{ foo: 1 }', 'yaml'));
46+
}
47+
48+
public function testSupportsDecoding()
49+
{
50+
$encoder = new YamlEncoder();
51+
52+
$this->assertTrue($encoder->supportsDecoding('yaml'));
53+
$this->assertFalse($encoder->supportsDecoding('json'));
54+
}
55+
56+
public function testContext()
57+
{
58+
$encoder = new YamlEncoder(new Dumper(), new Parser(), array('yaml_inline' => 1, 'yaml_indent' => 4, 'yaml_flags' => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT));
59+
60+
$obj = new \stdClass();
61+
$obj->bar = 2;
62+
63+
$this->assertEquals(" foo: !php/object:O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}\n", $encoder->encode(array('foo' => $obj), 'yaml'));
64+
$this->assertEquals(' { foo: null }', $encoder->encode(array('foo' => $obj), 'yaml', array('yaml_inline' => 0, 'yaml_indent' => 2, 'yaml_flags' => 0)));
65+
$this->assertEquals(array('foo' => $obj), $encoder->decode('foo: !php/object:O:8:"stdClass":1:{s:3:"bar";i:2;}', 'yaml'));
66+
$this->assertEquals(array('foo' => null), $encoder->decode('foo: !php/object:O:8:"stdClass":1:{s:3:"bar";i:2;}', 'yaml', array('yaml_flags' => 0)));
67+
}
68+
}

src/Symfony/Component/Serializer/composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=5.5.9"
2020
},
2121
"require-dev": {
22-
"symfony/yaml": "~2.8|~3.0",
22+
"symfony/yaml": "~3.1",
2323
"symfony/config": "~2.8|~3.0",
2424
"symfony/property-access": "~2.8|~3.0",
2525
"symfony/http-foundation": "~2.8|~3.0",
@@ -30,7 +30,8 @@
3030
"phpdocumentor/reflection-docblock": "~3.0"
3131
},
3232
"conflict": {
33-
"symfony/property-access": ">=3.0,<3.0.4|>=2.8,<2.8.4"
33+
"symfony/property-access": ">=3.0,<3.0.4|>=2.8,<2.8.4",
34+
"symfony/yaml": "<3.1"
3435
},
3536
"suggest": {
3637
"psr/cache-implementation": "For using the metadata cache.",

0 commit comments

Comments
 (0)