comparison core/lib/Drupal/Component/Serialization/Yaml.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Component\Serialization;
4
5 /**
6 * Provides a YAML serialization implementation.
7 *
8 * Proxy implementation that will choose the best library based on availability.
9 */
10 class Yaml implements SerializationInterface {
11
12 /**
13 * The YAML implementation to use.
14 *
15 * @var \Drupal\Component\Serialization\SerializationInterface
16 */
17 protected static $serializer;
18
19 /**
20 * {@inheritdoc}
21 */
22 public static function encode($data) {
23 // Instead of using \Drupal\Component\Serialization\Yaml::getSerializer(),
24 // always using Symfony for writing the data, to reduce the risk of having
25 // differences if different environments (like production and development)
26 // do not match in terms of what YAML implementation is available.
27 return YamlSymfony::encode($data);
28 }
29
30 /**
31 * {@inheritdoc}
32 */
33 public static function decode($raw) {
34 $serializer = static::getSerializer();
35 return $serializer::decode($raw);
36 }
37
38 /**
39 * {@inheritdoc}
40 */
41 public static function getFileExtension() {
42 return 'yml';
43 }
44
45 /**
46 * Determines which implementation to use for parsing YAML.
47 */
48 protected static function getSerializer() {
49
50 if (!isset(static::$serializer)) {
51 // Use the PECL YAML extension if it is available. It has better
52 // performance for file reads and is YAML compliant.
53 if (extension_loaded('yaml')) {
54 static::$serializer = YamlPecl::class;
55 }
56 else {
57 // Otherwise, fallback to the Symfony implementation.
58 static::$serializer = YamlSymfony::class;
59 }
60 }
61 return static::$serializer;
62 }
63
64 }