diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index bc6afef..25fa828 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -255,6 +255,11 @@ public function build(ContainerBuilder $container) { $container->register('serializer.normalizer.typed_data', 'Drupal\Core\Serialization\TypedDataNormalizer')->addTag('normalizer'); $container->register('serializer.encoder.json', 'Drupal\Core\Serialization\JsonEncoder')->addTag('encoder'); $container->register('serializer.encoder.xml', 'Drupal\Core\Serialization\XmlEncoder')->addTag('encoder'); + $container->register('serializer.encoder.yaml', 'Drupal\Core\Serialization\YamlEncoder')->addTag('encoder'); + + // Register any additional content types on the request service. + $container->register('serializer_format_subscriber', 'Drupal\Core\Serialization\SerializerFormatSubscriber') + ->addTag('event_subscriber'); $container->register('flood', 'Drupal\Core\Flood\DatabaseBackend') ->addArgument(new Reference('database')); diff --git a/core/lib/Drupal/Core/Serialization/SerializerFormatSubscriber.php b/core/lib/Drupal/Core/Serialization/SerializerFormatSubscriber.php new file mode 100644 index 0000000..28f8b01 --- /dev/null +++ b/core/lib/Drupal/Core/Serialization/SerializerFormatSubscriber.php @@ -0,0 +1,37 @@ +getRequest()->setFormat('yaml', array('application/x-yaml', 'text/yaml')); + } + + /** + * Implements \Symfony\Component\EventDispatcher\EventSubscriberInterface::getSubscribedEvents(). + */ + static function getSubscribedEvents() { + $events[KernelEvents::REQUEST][] = array('onKernelRequest', 30); + return $events; + } +} diff --git a/core/lib/Drupal/Core/Serialization/YamlEncoder.php b/core/lib/Drupal/Core/Serialization/YamlEncoder.php new file mode 100644 index 0000000..a4f533f --- /dev/null +++ b/core/lib/Drupal/Core/Serialization/YamlEncoder.php @@ -0,0 +1,100 @@ +getDumper()->dump($data, PHP_INT_MAX); + } + + /** + * Implements \Symfony\Component\Serializer\Encoder\JsonEncoder::supportsEncoding(). + */ + public function supportsEncoding($format) { + return in_array($format, static::$format); + } + + /** + * Implements \Symfony\Component\Serializer\Encoder\EncoderInterface::decode(). + */ + public function decode($data, $format, array $context = array()){ + return $this->getParser()->parse($data); + } + + /** + * Implements \Symfony\Component\Serializer\Encoder\JsonEncoder::supportsDecoding(). + */ + public function supportsDecoding($format) { + return in_array($format, static::$format); + } + + /** + * Gets the YAML dumper instance. + * + * @return \Symfony\Component\Yaml\Dumper + */ + protected function getDumper() { + if (!isset($this->dumper)) { + $this->dumper = new Dumper(); + // Set Yaml\Dumper's default indentation for nested nodes/collections to + // 2 spaces for consistency with Drupal coding standards. + $this->dumper->setIndentation(2); + } + + return $this->dumper; + } + + /** + * Gets the YAML parser instance. + * + * @return \Symfony\Component\Yaml\Parser + */ + protected function getParser() { + if (!isset($this->parser)) { + $this->parser = new Parser(); + } + + return $this->parser; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php b/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php index 89fa5ce..089d52a 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php @@ -12,6 +12,7 @@ use Drupal\Core\Serialization\JsonEncoder; use Drupal\Core\Serialization\ComplexDataNormalizer; use Drupal\Core\Serialization\TypedDataNormalizer; +use Symfony\Component\Yaml\Dumper; /** * Tests entity normalization and serialization of supported core formats. @@ -153,5 +154,14 @@ public function testSerialize() { $this->assertIdentical($actual, $expected); $actual = $serializer->serialize($normalized, 'xml'); $this->assertIdentical($actual, $expected); + + // Test YAML serialization. + $dumper = new Dumper(); + $dumper->setIndentation(2); + $expected = $dumper->dump($normalized, PHP_INT_MAX); + $actual = $serializer->serialize($this->entity, 'yaml'); + $this->assertIdentical($actual, $expected); + $actual = $serializer->serialize($normalized, 'yaml'); + $this->assertIdentical($actual, $expected); } }