diff --git a/core/includes/common.inc b/core/includes/common.inc index 11f8a9d..bab5257 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -9,30 +9,21 @@ */ use Drupal\Component\Serialization\Json; -use Drupal\Component\Serialization\Yaml; -use Drupal\Component\Serialization\Exception\InvalidDataTypeException; use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Html; -use Drupal\Component\Utility\Number; use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\SortArray; use Drupal\Component\Utility\String; -use Drupal\Component\Utility\Tags; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Cache\Cache; -use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Site\Settings; use Drupal\Core\Url; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; -use Drupal\Core\PhpStorage\PhpStorageFactory; use Drupal\Component\Utility\NestedArray; -use Drupal\Core\Datetime\DrupalDateTime; -use Drupal\Core\Routing\GeneratorNotInitializedException; use Drupal\Core\Template\Attribute; use Drupal\Core\Render\Element; -use Drupal\Core\Session\AnonymousUserSession; /** * @defgroup php_wrappers PHP wrapper functions diff --git a/core/lib/Drupal/Component/Serialization/Yaml.php b/core/lib/Drupal/Component/Serialization/Yaml.php index 7d0f0c5..058300e 100644 --- a/core/lib/Drupal/Component/Serialization/Yaml.php +++ b/core/lib/Drupal/Component/Serialization/Yaml.php @@ -8,6 +8,9 @@ namespace Drupal\Component\Serialization; use Drupal\Component\Serialization\Exception\InvalidDataTypeException; +use Drupal\Component\Serialization\YamlPecl; +use Drupal\Component\Serialization\YamlSymfony; +use Drupal\Core\Site\Settings; use Symfony\Component\Yaml\Yaml as Symfony; /** @@ -16,27 +19,55 @@ class Yaml implements SerializationInterface { /** + * The YAML implementation to use. + * + * @var \Drupal\Component\Serialization\SerializationInterface + */ + protected static $instance; + + /** + * Determines the optimal implementation to use for encoding and parsing Yaml. + * + * The selection is made based on the enabled PHP extensions, with the most + * performant available option chosen. + */ + public function __construct() { + if (isset(static::$instance)) { + return; + } + $settings = Settings::getInstance(); + // If there is a settings.php override, use that. + if ($settings && ($class = $settings->get('yaml_parser_class'))) { + static::$instance = new $class(); + } + // If the PECL YAML extension installed, use that. + elseif (extension_loaded('yaml')) { + static::$instance = new YamlPecl(); + } + else { + // Otherwise, fallback to the Symfony implementation. + static::$instance = new YamlSymfony(); + } + } + + /** * {@inheritdoc} */ public static function encode($data) { - try { - return Symfony::dump($data, PHP_INT_MAX, 2, TRUE); - } - catch (\Exception $e) { - throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e); + if (!isset(static::$instance)) { + new static(); } + return static::$instance->encode($data); } /** * {@inheritdoc} */ public static function decode($raw) { - try { - return Symfony::parse($raw, TRUE); - } - catch (\Exception $e) { - throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e); + if (!isset(static::$instance)) { + new static(); } + return static::$instance->decode($raw); } /** diff --git a/core/lib/Drupal/Component/Serialization/YamlPecl.php b/core/lib/Drupal/Component/Serialization/YamlPecl.php new file mode 100644 index 0000000..03a8a31 --- /dev/null +++ b/core/lib/Drupal/Component/Serialization/YamlPecl.php @@ -0,0 +1,91 @@ + '\Drupal\Component\Serialization\YamlPecl::applyBooleanCallbacks', + ]); + restore_error_handler(); + return $data; + } + + /** + * Temporary error handler for YamlPecl::decode(). + */ + public static function errorHandler($severity, $message, $file, $line) { + 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, we want + // the 1.2 behaviour so we only case 'false', 'FALSE', 'true' OR 'TRUE' as + // booleans. + if (!in_array(strtolower($value), ['false', 'true'], TRUE)) { + return $value; + } + $map = [ + 'false' => FALSE, + 'true' => TRUE, + ]; + return $map[strtolower($value)]; + } + +} diff --git a/core/lib/Drupal/Component/Serialization/Yaml.php b/core/lib/Drupal/Component/Serialization/YamlSymfony.php similarity index 88% copy from core/lib/Drupal/Component/Serialization/Yaml.php copy to core/lib/Drupal/Component/Serialization/YamlSymfony.php index 7d0f0c5..2d71b7c 100644 --- a/core/lib/Drupal/Component/Serialization/Yaml.php +++ b/core/lib/Drupal/Component/Serialization/YamlSymfony.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\Component\Serialization\Yaml. + * Contains \Drupal\Component\Serialization\YamlSymfony. */ namespace Drupal\Component\Serialization; @@ -13,7 +13,7 @@ /** * Default serialization for YAML using the Symfony component. */ -class Yaml implements SerializationInterface { +class YamlSymfony implements SerializationInterface { /** * {@inheritdoc} diff --git a/core/lib/Drupal/Core/Serialization/Yaml.php b/core/lib/Drupal/Core/Serialization/Yaml.php new file mode 100644 index 0000000..5aa3fbb --- /dev/null +++ b/core/lib/Drupal/Core/Serialization/Yaml.php @@ -0,0 +1,79 @@ +get('yaml_parser_class'))) { + static::$instance = new $class(); + } + // If the PECL YAML extension installed, use that. + elseif (extension_loaded('yaml')) { + static::$instance = new YamlPecl(); + } + else { + // Otherwise, fallback to the Symfony implementation. + static::$instance = new YamlSymfony(); + } + } + + /** + * {@inheritdoc} + */ + public static function encode($data) { + if (!isset(static::$instance)) { + new static(); + } + return static::$instance->encode($data); + } + + /** + * {@inheritdoc} + */ + public static function decode($raw) { + if (!isset(static::$instance)) { + new static(); + } + return static::$instance->decode($raw); + } + + /** + * {@inheritdoc} + */ + public static function getFileExtension() { + return 'yml'; + } + +} diff --git a/core/modules/system/tests/fixtures/common_test.info.txt b/core/modules/system/tests/fixtures/common_test.info.txt index 7e57dfe..ff535b1 100644 --- a/core/modules/system/tests/fixtures/common_test.info.txt +++ b/core/modules/system/tests/fixtures/common_test.info.txt @@ -4,4 +4,4 @@ type: module description: 'testing info file parsing' simple_string: 'A simple string' version: "VERSION" -double_colon: dummyClassName:: +double_colon: dummyClassName::foo diff --git a/core/modules/views/config/schema/views.data_types.schema.yml b/core/modules/views/config/schema/views.data_types.schema.yml index 64ff9c4..7cf0cad 100644 --- a/core/modules/views/config/schema/views.data_types.schema.yml +++ b/core/modules/views/config/schema/views.data_types.schema.yml @@ -365,7 +365,6 @@ views_argument: label: 'Override title' title: type: label - label: 'Title' label: 'Overridden title' default_argument_type: type: string diff --git a/core/tests/Drupal/Tests/Component/Serialization/YamlTest.php b/core/tests/Drupal/Tests/Component/Serialization/YamlTest.php new file mode 100644 index 0000000..a6dc382 --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Serialization/YamlTest.php @@ -0,0 +1,152 @@ +markTestSkipped('The PECL Yaml extension is not available.'); + } + } + + /** + * Tests encoding with Symfony and decoding with PECL and vice versa. + * + * @dataProvider providerYamlData + * @covers \Drupal\Component\Serialization\Yaml::encode + * @covers \Drupal\Component\Serialization\Yaml::decode + * @covers \Drupal\Component\Serialization\YamlPecl::encode + * @covers \Drupal\Component\Serialization\YamlPecl::decode + * @covers \Drupal\Component\Serialization\YamlSymfony::encode + * @covers \Drupal\Component\Serialization\YamlSymfony::decode + */ + public function testEncodeDecode($data, $parser_class, $dumper_class) { + $dumper = new $dumper_class; + $parser = new $parser_class; + + $dumped = $dumper->encode($data); + + $parsed = $parser->decode($dumped); + + $this->assertEquals($data, $parsed); + } + + /** + * Tests decoding YAML node anchors with both Symfony and PECL. + * + * @dataProvider providerYamlNodeAnchors + */ + public function testDecodeNodeAnchors($data) { + $symfony = new YamlSymfony(); + $pecl = new YamlPecl(); + $this->assertEquals($symfony->decode($data), $pecl->decode($data)); + } + + /** + * Data provider for YAML instance tests. + * + * @return array + */ + public function providerYamlData() { + $object = array( + 'foo' => 'bar', + 'id' => 'schnitzel', + 'ponies' => array('nope', 'thanks'), + 'how' => array( + 'about' => 'if', + 'i' => 'ask', + 'nicely' + ), + 'the' => array( + 'answer' => array( + 'still' => 'would', + 'be' => 'Y', + ), + ), + 'how_many_times' => 123, + 'should_i_ask' => FALSE, + 1, + FALSE, + array(1, FALSE), + array(10), + array(0 => '123456'), + ); + + // Test parsing with Symfony and dumping with PECL. + $data[] = array( + $object, + 'Drupal\Component\Serialization\YamlSymfony', + 'Drupal\Component\Serialization\YamlPecl' + ); + // Test parsing with PECL and dumping with Symfony. + $data[] = array( + $object, + 'Drupal\Component\Serialization\YamlPecl', + 'Drupal\Component\Serialization\YamlSymfony' + ); + return $data; + } + + /** + * Data provider for YAML instance tests. + * + * @return array + */ + public function providerYamlNodeAnchors() { + $yaml = <<assertEquals($symfony->decode($data), $pecl->decode($data)); + } + + /** + * Data provider for YAML files in core test. + * @return array + */ + public function providerYamlFilesInCore() { + $files = array(); + $dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/../../../../../', \RecursiveDirectoryIterator::FOLLOW_SYMLINKS)); + foreach ($dirs as $dir) { + $pathname = $dir->getPathname(); + // Exclude vendor. + if ($dir->getExtension() == 'yml' && strpos($pathname, '/../../../../../vendor') === FALSE) { + $files[] = array($dir->getRealPath()); + } + } + return $files; + } + +}