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;
}
}

View file

@ -0,0 +1,101 @@
<?php
namespace Drupal\Component\Serialization;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
/**
* Provides default serialization for YAML using the PECL extension.
*/
class YamlPecl implements SerializationInterface {
/**
* {@inheritdoc}
*/
public static function encode($data) {
static $init;
if (!isset($init)) {
ini_set('yaml.output_indent', 2);
// Do not break lines at 80 characters.
ini_set('yaml.output_width', -1);
$init = TRUE;
}
return yaml_emit($data, YAML_UTF8_ENCODING, YAML_LN_BREAK);
}
/**
* {@inheritdoc}
*/
public static function decode($raw) {
// yaml_parse() will error with an empty value.
if (!trim($raw)) {
return NULL;
}
// @todo Use ErrorExceptions when https://drupal.org/node/1247666 is in.
// yaml_parse() will throw errors instead of raising an exception. Until
// such time as Drupal supports native PHP ErrorExceptions as the error
// handler, we need to temporarily set the error handler as ::errorHandler()
// and then restore it after decoding has occurred. This allows us to turn
// parsing errors into a throwable exception.
// @see Drupal\Component\Serialization\Exception\InvalidDataTypeException
// @see http://php.net/manual/en/class.errorexception.php
set_error_handler([__CLASS__, 'errorHandler']);
$ndocs = 0;
$data = yaml_parse($raw, 0, $ndocs, [
YAML_BOOL_TAG => '\Drupal\Component\Serialization\YamlPecl::applyBooleanCallbacks',
]);
restore_error_handler();
return $data;
}
/**
* Handles errors for \Drupal\Component\Serialization\YamlPecl::decode().
*
* @param int $severity
* The severity level of the error.
* @param string $message
* The error message to display.
*
* @see \Drupal\Component\Serialization\YamlPecl::decode()
*/
public static function errorHandler($severity, $message) {
restore_error_handler();
throw new InvalidDataTypeException($message, $severity);
}
/**
* {@inheritdoc}
*/
public static function getFileExtension() {
return 'yml';
}
/**
* Applies callbacks after parsing to ignore 1.1 style booleans.
*
* @param mixed $value
* Value from YAML file.
* @param string $tag
* Tag that triggered the callback.
* @param int $flags
* Scalar entity style flags.
*
* @return string|bool
* FALSE, false, TRUE and true are returned as booleans, everything else is
* returned as a string.
*/
public static function applyBooleanCallbacks($value, $tag, $flags) {
// YAML 1.1 spec dictates that 'Y', 'N', 'y' and 'n' are booleans. But, we
// want the 1.2 behavior, so we only consider 'false', 'FALSE', 'true' and
// 'TRUE' as booleans.
if (!in_array(strtolower($value), ['false', 'true'], TRUE)) {
return $value;
}
$map = [
'false' => FALSE,
'true' => TRUE,
];
return $map[strtolower($value)];
}
}

View file

@ -0,0 +1,50 @@
<?php
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.
*/
class YamlSymfony implements SerializationInterface {
/**
* {@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);
}
}
/**
* {@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);
}
}
/**
* {@inheritdoc}
*/
public static function getFileExtension() {
return 'yml';
}
}