diff --git a/core/lib/Drupal/Component/Serialization/Yaml.php b/core/lib/Drupal/Component/Serialization/Yaml.php index 5e104ba..f7bc09a 100644 --- a/core/lib/Drupal/Component/Serialization/Yaml.php +++ b/core/lib/Drupal/Component/Serialization/Yaml.php @@ -7,42 +7,34 @@ 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. + * 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); - } + $serializer = static::getSerializer(); + return $serializer::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); } /** @@ -52,4 +44,21 @@ public static function getFileExtension() { return 'yml'; } + /** + * Determines the optimal implementation to use for encoding and parsing Yaml. + */ + protected static function getSerializer() { + + if (!isset(static::$serializer)) { + // If the PECL YAML extension installed, use that. + if (extension_loaded('yaml')) { + static::$serializer = '\Drupal\Component\Serialization\YamlPecl'; + } + else { + // Otherwise, fallback to the Symfony implementation. + static::$serializer = '\Drupal\Component\Serialization\YamlSymfony'; + } + } + return static::$serializer; + } } diff --git a/core/lib/Drupal/Component/Serialization/YamlPecl.php b/core/lib/Drupal/Component/Serialization/YamlPecl.php new file mode 100644 index 0000000..0fd833c --- /dev/null +++ b/core/lib/Drupal/Component/Serialization/YamlPecl.php @@ -0,0 +1,99 @@ + '\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 91% copy from core/lib/Drupal/Component/Serialization/Yaml.php copy to core/lib/Drupal/Component/Serialization/YamlSymfony.php index 5e104ba..4ca5e25 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; @@ -14,7 +14,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/Asset/LibraryDiscoveryParser.php b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php index 4746b1e..50dfc40 100644 --- a/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php @@ -11,8 +11,8 @@ use Drupal\Core\Asset\Exception\InvalidLibraryFileException; use Drupal\Core\Asset\Exception\LibraryDefinitionMissingLicenseException; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Serialization\Yaml; use Drupal\Component\Serialization\Exception\InvalidDataTypeException; -use Drupal\Component\Serialization\Yaml; use Drupal\Component\Utility\NestedArray; /** diff --git a/core/lib/Drupal/Core/Config/ConfigManager.php b/core/lib/Drupal/Core/Config/ConfigManager.php index 1462d78..4d9d11d 100644 --- a/core/lib/Drupal/Core/Config/ConfigManager.php +++ b/core/lib/Drupal/Core/Config/ConfigManager.php @@ -8,7 +8,7 @@ namespace Drupal\Core\Config; use Drupal\Component\Diff\Diff; -use Drupal\Component\Serialization\Yaml; +use Drupal\Core\Serialization\Yaml; use Drupal\Core\Config\Entity\ConfigDependencyManager; use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Entity\EntityManagerInterface; diff --git a/core/lib/Drupal/Core/Config/FileStorage.php b/core/lib/Drupal/Core/Config/FileStorage.php index de3ac80..5d969c1 100644 --- a/core/lib/Drupal/Core/Config/FileStorage.php +++ b/core/lib/Drupal/Core/Config/FileStorage.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Config; -use Drupal\Component\Serialization\Yaml; +use Drupal\Core\Serialization\Yaml; use Drupal\Component\Serialization\Exception\InvalidDataTypeException; use Drupal\Component\Utility\SafeMarkup; diff --git a/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php b/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php index d4ae53e..2c2eaef 100644 --- a/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php +++ b/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php @@ -7,7 +7,7 @@ namespace Drupal\Core\DependencyInjection; -use Drupal\Component\Serialization\Yaml; +use Drupal\Core\Serialization\Yaml; use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Definition; diff --git a/core/lib/Drupal/Core/Discovery/YamlDiscovery.php b/core/lib/Drupal/Core/Discovery/YamlDiscovery.php new file mode 100644 index 0000000..bac6219 --- /dev/null +++ b/core/lib/Drupal/Core/Discovery/YamlDiscovery.php @@ -0,0 +1,30 @@ +findFiles() as $provider => $file) { + $all[$provider] = Yaml::decode(file_get_contents($file)); + } + + return $all; + } + +} diff --git a/core/lib/Drupal/Core/Extension/InfoParser.php b/core/lib/Drupal/Core/Extension/InfoParser.php index cb178fb..eb2d603 100644 --- a/core/lib/Drupal/Core/Extension/InfoParser.php +++ b/core/lib/Drupal/Core/Extension/InfoParser.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Extension; -use Drupal\Component\Serialization\Yaml; +use Drupal\Core\Serialization\Yaml; use Drupal\Component\Serialization\Exception\InvalidDataTypeException; use Drupal\Component\Utility\SafeMarkup; diff --git a/core/lib/Drupal/Core/Extension/ModuleInstaller.php b/core/lib/Drupal/Core/Extension/ModuleInstaller.php index caddbad..1596d17 100644 --- a/core/lib/Drupal/Core/Extension/ModuleInstaller.php +++ b/core/lib/Drupal/Core/Extension/ModuleInstaller.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Extension; -use Drupal\Component\Serialization\Yaml; +use Drupal\Core\Serialization\Yaml; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\PreExistingConfigException; diff --git a/core/lib/Drupal/Core/Plugin/Discovery/YamlDiscovery.php b/core/lib/Drupal/Core/Plugin/Discovery/YamlDiscovery.php index 0b7d10f6..9803171 100644 --- a/core/lib/Drupal/Core/Plugin/Discovery/YamlDiscovery.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/YamlDiscovery.php @@ -8,7 +8,7 @@ namespace Drupal\Core\Plugin\Discovery; use Drupal\Component\Plugin\Discovery\DiscoveryInterface; -use Drupal\Component\Discovery\YamlDiscovery as ComponentYamlDiscovery; +use Drupal\Core\Discovery\YamlDiscovery as CoreYamlDiscovery; use Drupal\Component\Plugin\Discovery\DiscoveryTrait; /** @@ -35,7 +35,7 @@ class YamlDiscovery implements DiscoveryInterface { * An array of directories to scan. */ function __construct($name, array $directories) { - $this->discovery = new ComponentYamlDiscovery($name, $directories); + $this->discovery = new CoreYamlDiscovery($name, $directories); } /** diff --git a/core/lib/Drupal/Core/Serialization/Yaml.php b/core/lib/Drupal/Core/Serialization/Yaml.php new file mode 100644 index 0000000..6b70775 --- /dev/null +++ b/core/lib/Drupal/Core/Serialization/Yaml.php @@ -0,0 +1,33 @@ +infoParser->parse('core/modules/system/tests/fixtures/common_test.info.txt'); $this->assertEqual($info_values['simple_string'], 'A simple string', 'Simple string value was parsed correctly.', 'System'); $this->assertEqual($info_values['version'], \Drupal::VERSION, 'Constant value was parsed correctly.', 'System'); - $this->assertEqual($info_values['double_colon'], 'dummyClassName::', 'Value containing double-colon was parsed correctly.', 'System'); + $this->assertEqual($info_values['double_colon'], 'dummyClassName::foo', 'Value containing double-colon was parsed correctly.', 'System'); } } 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/system/tests/modules/theme_test/theme_test.services.yml b/core/modules/system/tests/modules/theme_test/theme_test.services.yml index add8b4c..9049f36 100644 --- a/core/modules/system/tests/modules/theme_test/theme_test.services.yml +++ b/core/modules/system/tests/modules/theme_test/theme_test.services.yml @@ -1,7 +1,7 @@ services: theme_test.subscriber: class: Drupal\theme_test\EventSubscriber\ThemeTestSubscriber - arguments: [@current_route_match] + arguments: ["@current_route_match"] tags: - { name: event_subscriber } diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_group_rows.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_group_rows.yml index d2c967b..c48fe7d 100644 --- a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_group_rows.yml +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_group_rows.yml @@ -7,7 +7,6 @@ dependencies: - user config: - field.storage.node.field_views_testing_group_rows - module: id: test_group_rows label: test_group_rows module: views diff --git a/core/profiles/standard/config/install/editor.editor.full_html.yml b/core/profiles/standard/config/install/editor.editor.full_html.yml index 80da28c..34800d1 100644 --- a/core/profiles/standard/config/install/editor.editor.full_html.yml +++ b/core/profiles/standard/config/install/editor.editor.full_html.yml @@ -12,7 +12,7 @@ settings: - Strike - Superscript - Subscript - - - + - '-' - RemoveFormat - name: Linking diff --git a/core/tests/Drupal/Tests/Component/Serialization/YamlBaseTest.php b/core/tests/Drupal/Tests/Component/Serialization/YamlBaseTest.php new file mode 100644 index 0000000..36d0637 --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Serialization/YamlBaseTest.php @@ -0,0 +1,101 @@ + 'bar', + 'id' => 'schnitzel', + 'ponies' => ['nope', 'thanks'], + 'how' => [ + 'about' => 'if', + 'i' => 'ask', + 'nicely' + ], + 'the' => [ + 'answer' => [ + 'still' => 'would', + 'be' => 'Y', + ], + ], + 'how_many_times' => 123, + 'should_i_ask' => FALSE, + 1, + FALSE, + [1, FALSE], + [10], + [0 => '123456'], + ], + [NULL] + ]; + } + + public function providerDecodeTests() { + $data = [ + // Null files. + ['', NULL], + ["\n", NULL], + ["---\n...\n", NULL], + + // Node Anchors. + [ + " +jquery.ui: + version: &jquery_ui 1.10.2 + +jquery.ui.accordion: + version: *jquery_ui +", + [ + 'jquery.ui' => [ + 'version' => '1.10.2' + ], + 'jquery.ui.accordion' => [ + 'version' => '1.10.2' + ], + ], + ], + ]; + + // 1.2 Bool values. + foreach ($this->providerBoolTest() as $test) { + $data[] = ['bool: ' . $test[0], ['bool' => $test[1]]]; + } + $data = array_merge($data, $this->providerBoolTest()); + + return $data; + } + + public function providerBoolTest() { + return [ + ['true', TRUE], + ['TRUE', TRUE], + ['True', TRUE], + ['y', 'y'], + ['Y', 'Y'], + ['false', FALSE], + ['FALSE', FALSE], + ['False', FALSE], + ['n', 'n'], + ['N', 'N'], + ]; + } + +} diff --git a/core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php b/core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php new file mode 100644 index 0000000..33030f9 --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php @@ -0,0 +1,86 @@ +assertEquals($data, YamlPecl::decode(YamlPecl::encode($data))); + } + + /** + * Tests decoding YAML node anchors. + * + * @covers ::decode + * @dataProvider providerDecodeTests + */ + public function testDecode($string, $data) { + $this->assertEquals($data, YamlPecl::decode($string)); + } + + /** + * Tests our encode settings. + * + * @covers ::encode + */ + public function testEncode() { + $this->assertEquals('--- +foo: + bar: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis +... +', YamlPecl::encode(['foo' => ['bar' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis']])); + } + + /** + * Test yaml boolean callback. + * + * @covers ::applyBooleanCallbacks + * @dataProvider providerBoolTest + * @param string $string + * String value for the yaml boolean. + * @param string|bool $expected + * The expected return + */ + public function testApplyBooleanCallbacks($string, $expected) { + $this->assertEquals($expected, YamlPecl::applyBooleanCallbacks($string, 'bool', NULL)); + } + + /** + * @covers ::getFileExtension + */ + public function testGetFileExtension() { + $this->assertEquals('yml', YamlPecl::getFileExtension()); + } + + /** + * Invalid yaml throws exception. + * + * @covers ::errorHandler + * @expectedException \Drupal\Component\Serialization\Exception\InvalidDataTypeException + */ + public function testError() { + YamlPecl::decode('foo: [ads'); + } +} diff --git a/core/tests/Drupal/Tests/Component/Serialization/YamlSymfonyTest.php b/core/tests/Drupal/Tests/Component/Serialization/YamlSymfonyTest.php new file mode 100644 index 0000000..0836be4 --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Serialization/YamlSymfonyTest.php @@ -0,0 +1,70 @@ +assertEquals($data, YamlSymfony::decode(YamlSymfony::encode($data))); + } + + /** + * Tests decoding YAML node anchors. + * + * @covers ::decode + * @dataProvider providerDecodeTests + */ + public function testDecode($string, $data) { + $this->assertEquals($data, YamlSymfony::decode($string)); + } + + /** + * Tests our encode settings. + * + * @covers ::encode + */ + public function testEncode() { + $this->assertEquals('foo: + bar: \'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis\' +', YamlSymfony::encode(['foo' => ['bar' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis']])); + } + + /** + * @covers ::getFileExtension + */ + public function testGetFileExtension() { + $this->assertEquals('yml', YamlSymfony::getFileExtension()); + } + + /** + * Invalid yaml throws exception. + * + * @covers ::decode + * @expectedException \Drupal\Component\Serialization\Exception\InvalidDataTypeException + */ + public function testError() { + YamlSymfony::decode('foo: [ads'); + } +} diff --git a/core/tests/Drupal/Tests/Component/Serialization/YamlTest.php b/core/tests/Drupal/Tests/Component/Serialization/YamlTest.php index 94269e6..19d21af 100644 --- a/core/tests/Drupal/Tests/Component/Serialization/YamlTest.php +++ b/core/tests/Drupal/Tests/Component/Serialization/YamlTest.php @@ -7,7 +7,11 @@ namespace Drupal\Tests\Component\Serialization; +use Drupal\Component\Serialization\Exception\InvalidDataTypeException; +use Drupal\Component\Serialization\SerializationInterface; use Drupal\Component\Serialization\Yaml; +use Drupal\Component\Serialization\YamlPecl; +use Drupal\Component\Serialization\YamlSymfony; use Drupal\Tests\UnitTestCase; /** @@ -20,44 +24,117 @@ class YamlTest extends UnitTestCase { * @covers ::decode */ public function testDecode() { - // Test that files without line break endings are properly interpreted. - $yaml = 'foo: bar'; - $expected = array( - 'foo' => 'bar', - ); - $this->assertSame($expected, Yaml::decode($yaml)); - $yaml .= "\n"; - $this->assertSame($expected, Yaml::decode($yaml)); - $yaml .= "\n"; - $this->assertSame($expected, Yaml::decode($yaml)); - - $yaml = "{}\n"; - $expected = array(); - $this->assertSame($expected, Yaml::decode($yaml)); - - $yaml = ''; - $this->assertNULL(Yaml::decode($yaml)); - $yaml .= "\n"; - $this->assertNULL(Yaml::decode($yaml)); - $yaml .= "\n"; - $this->assertNULL(Yaml::decode($yaml)); + $stub = $this->getMockBuilder('\stdClass') + ->setMethods(['encode', 'decode', 'getFileExtension']) + ->getMock(); + $stub + ->expects($this->once()) + ->method('decode'); + YamlParserProxy::setMock($stub); + YamlStub::decode('test'); } /** * @covers ::encode */ public function testEncode() { - $decoded = array( - 'foo' => 'bar', - ); - $this->assertSame('foo: bar' . "\n", Yaml::encode($decoded)); + $stub = $this->getMockBuilder('\stdClass') + ->setMethods(['encode', 'decode', 'getFileExtension']) + ->getMock(); + $stub + ->expects($this->once()) + ->method('encode'); + YamlParserProxy::setMock($stub); + YamlStub::encode([]); } /** * @covers ::getFileExtension */ public function testGetFileExtension() { - $this->assertEquals('yml', Yaml::getFileExtension()); + $stub = $this->getMockBuilder('\stdClass') + ->setMethods(['encode', 'decode', 'getFileExtension']) + ->getMock(); + $stub + ->expects($this->never()) + ->method('getFileExtension'); + YamlParserProxy::setMock($stub); + $this->assertEquals('yml', YamlStub::getFileExtension()); } + /** + * Tests all YAML files are decoded in the same way with both Symfony and PECL. + * + * This tests is a little bit slow but it tests that we don't have any bugs + * in our YAML that might not be decoded correctly in on of our + * implementations. + * + * @TODO this should exist as an integration test no as part of our unit tests. + * + * @requires extension yaml + * @dataProvider providerYamlFilesInCore + */ + public function testYamlFiles($file) { + $data = file_get_contents($file); + try { + $this->assertEquals(YamlSymfony::decode($data), YamlPecl::decode($data), $file); + } + catch (InvalidDataTypeException $e) { + // Provide file context to the failure so the exception message is useful. + $this->fail("Exception thrown parsing $file:\n" . $e->getMessage()); + } + } + + /** + * Data provider that lists all YAML files core. + * @return array + */ + public function providerYamlFilesInCore() { + $files = []; + $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) { + if (strpos($dir->getRealPath(), 'invalid_file') !== FALSE) { + // There are some intentionally invalid files provided for testing + // library API behaviours, ignore them. + continue; + } + $files[] = array($dir->getRealPath()); + } + } + return $files; + } +} + +class YamlStub extends Yaml { + + public static function getSerializer() { + return '\Drupal\Tests\Component\Serialization\YamlParserProxy'; + } +} + +class YamlParserProxy implements SerializationInterface { + + /** + * @var \Drupal\Component\Serialization\SerializationInterface + */ + static $mock; + + public static function setMock($mock) { + static::$mock = $mock; + } + + public static function encode($data) { + return static::$mock->encode($data); + } + + public static function decode($raw) { + return static::$mock->decode($raw); + } + + public static function getFileExtension() { + return static::$mock->getFileExtension(); + } }