Update to Drupal 8.2.0. For more information, see https://www.drupal.org/project/drupal/releases/8.2.0

This commit is contained in:
Pantheon Automation 2016-10-06 15:16:20 -07:00 committed by Greg Anderson
parent 2f563ab520
commit f1c8716f57
1732 changed files with 52334 additions and 11780 deletions

View file

@ -2,42 +2,37 @@
namespace Drupal\Component\Serialization;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Dumper;
/**
* Default serialization for YAML using the Symfony component.
* Provides a YAML serialization implementation.
*
* Proxy implementation that will choose the best library based on availability.
*/
class Yaml implements SerializationInterface {
/**
* The YAML implementation to use.
*
* @var \Drupal\Component\Serialization\SerializationInterface
*/
protected static $serializer;
/**
* {@inheritdoc}
*/
public static function encode($data) {
try {
$yaml = new Dumper();
$yaml->setIndentation(2);
return $yaml->dump($data, PHP_INT_MAX, 0, TRUE, FALSE);
}
catch (\Exception $e) {
throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
}
// Instead of using \Drupal\Component\Serialization\Yaml::getSerializer(),
// always using Symfony for writing the data, to reduce the risk of having
// differences if different environments (like production and development)
// do not match in terms of what YAML implementation is available.
return YamlSymfony::encode($data);
}
/**
* {@inheritdoc}
*/
public static function decode($raw) {
try {
$yaml = new Parser();
// Make sure we have a single trailing newline. A very simple config like
// 'foo: bar' with no newline will fail to parse otherwise.
return $yaml->parse($raw, TRUE, FALSE);
}
catch (\Exception $e) {
throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
}
$serializer = static::getSerializer();
return $serializer::decode($raw);
}
/**
@ -47,4 +42,23 @@ class Yaml implements SerializationInterface {
return 'yml';
}
/**
* Determines which implementation to use for parsing YAML.
*/
protected static function getSerializer() {
if (!isset(static::$serializer)) {
// Use the PECL YAML extension if it is available. It has better
// performance for file reads and is YAML compliant.
if (extension_loaded('yaml')) {
static::$serializer = YamlPecl::class;
}
else {
// Otherwise, fallback to the Symfony implementation.
static::$serializer = YamlSymfony::class;
}
}
return static::$serializer;
}
}