diff --git a/core/includes/common.inc b/core/includes/common.inc index 61b63be..8c57cef 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -9,9 +9,9 @@ use Drupal\Core\Language\Language; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\Yaml\Parser; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; +use Drupal\Component\Yaml\Yaml; use Drupal\Component\PhpStorage\PhpStorageFactory; use Drupal\Component\Utility\MapArray; use Drupal\Component\Utility\NestedArray; @@ -5383,8 +5383,8 @@ function drupal_parse_info_file($filename) { $info[$filename] = array(); } else { - $parser = new Parser(); - $info[$filename] = $parser->parse(file_get_contents($filename)); + $yaml = new Yaml(); + $info[$filename] = $yaml->parse(file_get_contents($filename)); if (isset($info[$filename]['version']) && $info[$filename]['version'] === 'VERSION') { $info[$filename]['version'] = VERSION; } diff --git a/core/lib/Drupal/Component/Yaml/Pecl.php b/core/lib/Drupal/Component/Yaml/Pecl.php new file mode 100644 index 0000000..635b277 --- /dev/null +++ b/core/lib/Drupal/Component/Yaml/Pecl.php @@ -0,0 +1,42 @@ +getParser()->parse($input); + } + + /** + * {@inheritdoc} + */ + public function dump($value) { + // The level where you switch to inline YAML is set to PHP_INT_MAX to ensure + // this does not occur. + return $this->getDumper()->dump($value, PHP_INT_MAX); + } + + /** + * Gets the YAML dumper instance. + * + * @return \Symfony\Component\Yaml\Dumper + */ + protected function getDumper() { + if (!isset($this->dumper)) { + $this->dumper = new Dumper(); + // Set Yaml\Dumper's default indentation for nested nodes/collections to + // 2 spaces for consistency with Drupal coding standards. + $this->dumper->setIndentation($this->indentation); + } + return $this->dumper; + } + + /** + * Gets the YAML parser instance. + * + * @return \Symfony\Component\Yaml\Parser + */ + protected function getParser() { + if (!isset($this->parser)) { + $this->parser = new Parser(); + } + return $this->parser; + } + +} diff --git a/core/lib/Drupal/Component/Yaml/Yaml.php b/core/lib/Drupal/Component/Yaml/Yaml.php new file mode 100644 index 0000000..0a16f01 --- /dev/null +++ b/core/lib/Drupal/Component/Yaml/Yaml.php @@ -0,0 +1,71 @@ +determinePlugin(); + $this->plugin = new $class(); + } + + /** + * {@inheritdoc} + */ + public function parse($input) { + return $this->plugin->parse($input); + } + + /** + * {@inheritdoc} + */ + public function dump($value) { + return $this->plugin->dump($value); + } + + /** + * 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. + * + * @return string + * The class name for the optimal Yaml implementation. + */ + protected function determinePlugin() { + static $plugin; + if (!empty($plugin)) { + return $plugin; + } + + // Fallback to the Symfony implementation. + $plugin = 'Drupal\Component\Yaml\Symfony'; + + // Is the PECL Yaml extension installed + if (function_exists('yaml_emit')) { + $plugin = 'Drupal\Component\Yaml\Pecl'; + } + return $plugin; + } +} diff --git a/core/lib/Drupal/Component/Yaml/YamlInterface.php b/core/lib/Drupal/Component/Yaml/YamlInterface.php new file mode 100644 index 0000000..6b0e575 --- /dev/null +++ b/core/lib/Drupal/Component/Yaml/YamlInterface.php @@ -0,0 +1,39 @@ +dumper)) { - $this->dumper = new Dumper(); - // Set Yaml\Dumper's default indentation for nested nodes/collections to - // 2 spaces for consistency with Drupal coding standards. - $this->dumper->setIndentation(2); - } - return $this->dumper; - } - - /** - * Gets the YAML parser instance. + * Gets the YAML instance. * - * @return Symfony\Component\Yaml\Parser + * @return \Drupal\Component\Yaml\YamlInterface */ - protected function getParser() { - if (!isset($this->parser)) { - $this->parser = new Parser(); + protected function getYaml() { + if (!isset($this->yaml)) { + $this->yaml = new Yaml(); } - return $this->parser; + return $this->yaml; } /** @@ -161,9 +138,7 @@ protected function getParser() { * @throws Symfony\Component\Yaml\Exception\DumpException */ public function encode($data) { - // The level where you switch to inline YAML is set to PHP_INT_MAX to ensure - // this does not occur. - return $this->getDumper()->dump($data, PHP_INT_MAX); + return $this->getYaml()->dump($data); } /** @@ -172,7 +147,7 @@ public function encode($data) { * @throws Symfony\Component\Yaml\Exception\ParseException */ public function decode($raw) { - $data = $this->getParser()->parse($raw); + $data = $this->getYaml()->parse($raw); // A simple string is valid YAML for any reason. if (!is_array($data)) { return FALSE; diff --git a/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php b/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php index f5df4ec..a2ef204 100644 --- a/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php +++ b/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php @@ -7,12 +7,12 @@ namespace Drupal\Core\DependencyInjection; +use Drupal\Component\Yaml\Yaml; use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\DefinitionDecorator; use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\Yaml\Parser; /** * YamlFileLoader loads YAML files service definitions. @@ -39,6 +39,7 @@ class YamlFileLoader { public function __construct(ContainerBuilder $container) { $this->container = $container; + $this->yaml = new Yaml(); } /** @@ -190,8 +191,7 @@ protected function parseDefinition($id, $service, $filename) { * The file content. */ protected function loadFile($filename) { - $parser = new Parser(); - return $this->validate($parser->parse(file_get_contents($filename)), $filename); + return $this->validate($this->yaml->parse(file_get_contents($filename)), $filename); } /** diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php index 656e918..9ba76d6 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandler.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php @@ -8,7 +8,7 @@ namespace Drupal\Core\Extension; use Drupal\Component\Graph\Graph; -use Symfony\Component\Yaml\Parser; +use Drupal\Component\Yaml\Yaml; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\KeyValueStore\KeyValueStoreInterface; @@ -844,6 +844,7 @@ public function uninstall($module_list = array(), $uninstall_dependents = TRUE) $schema_store = \Drupal::keyValue('system.schema'); $disabled_config = config('system.module.disabled'); + $yaml = new Yaml(); foreach ($module_list as $module) { // Uninstall the module. module_load_install($module); @@ -856,8 +857,7 @@ public function uninstall($module_list = array(), $uninstall_dependents = TRUE) // Remove any cache bins defined by the module. $service_yaml_file = drupal_get_path('module', $module) . "/$module.services.yml"; if (file_exists($service_yaml_file)) { - $parser = new Parser; - $definitions = $parser->parse(file_get_contents($service_yaml_file)); + $definitions = $yaml->parse(file_get_contents($service_yaml_file)); if (isset($definitions['services'])) { foreach ($definitions['services'] as $id => $definition) { if (isset($definition['tags'])) { diff --git a/core/lib/Drupal/Core/Routing/RouteBuilder.php b/core/lib/Drupal/Core/Routing/RouteBuilder.php index 232e47e..f69f742 100644 --- a/core/lib/Drupal/Core/Routing/RouteBuilder.php +++ b/core/lib/Drupal/Core/Routing/RouteBuilder.php @@ -9,12 +9,12 @@ use Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\Yaml\Parser; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Lock\LockBackendInterface; +use Drupal\Component\Yaml\Yaml; /** * Managing class for rebuilding the router table. @@ -81,7 +81,7 @@ public function rebuild() { return; } - $parser = new Parser(); + $yaml = new Yaml(); // We need to manually call each module so that we can know which module // a given item came from. @@ -89,7 +89,7 @@ public function rebuild() { $collection = new RouteCollection(); $routing_file = DRUPAL_ROOT . '/' . dirname($filename) . '/' . $module . '.routing.yml'; if (file_exists($routing_file)) { - $routes = $parser->parse(file_get_contents($routing_file)); + $routes = $yaml->parse(file_get_contents($routing_file)); if (!empty($routes)) { foreach ($routes as $name => $route_info) { $defaults = isset($route_info['defaults']) ? $route_info['defaults'] : array(); diff --git a/core/modules/config/lib/Drupal/config/Tests/Storage/FileStorageTest.php b/core/modules/config/lib/Drupal/config/Tests/Storage/FileStorageTest.php index d4d492a..e6a6f16 100644 --- a/core/modules/config/lib/Drupal/config/Tests/Storage/FileStorageTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/Storage/FileStorageTest.php @@ -8,12 +8,20 @@ namespace Drupal\config\Tests\Storage; use Drupal\Core\Config\FileStorage; -use Symfony\Component\Yaml\Yaml; +use Drupal\Component\Yaml\Yaml; /** * Tests FileStorage controller operations. */ class FileStorageTest extends ConfigStorageTestBase { + + /** + * A YAML instance. + * + * @var \Drupal\Component\Yaml\YamlInterface + */ + protected $yaml; + public static function getInfo() { return array( 'name' => 'FileStorage controller operations', @@ -24,6 +32,7 @@ public static function getInfo() { function setUp() { parent::setUp(); + $this->yaml = new Yaml(); $this->storage = new FileStorage($this->configDirectories[CONFIG_ACTIVE_DIRECTORY]); $this->invalidStorage = new FileStorage($this->configDirectories[CONFIG_ACTIVE_DIRECTORY] . '/nonexisting'); @@ -33,7 +42,7 @@ function setUp() { protected function read($name) { $data = file_get_contents($this->storage->getFilePath($name)); - return Yaml::parse($data); + return $this->yaml->parse($data); } protected function insert($name, $data) { diff --git a/core/modules/layout/layouts/static/one-col/one-col.yml b/core/modules/layout/layouts/static/one-col/one-col.yml index ea550ca..991e380 100644 --- a/core/modules/layout/layouts/static/one-col/one-col.yml +++ b/core/modules/layout/layouts/static/one-col/one-col.yml @@ -1,7 +1,7 @@ -title: One column -category: Columns: 1 +title: 'One column' +category: 'Columns: 1' template: one-col regions: content: - label: Middle column + label: 'Middle column' type: content \ No newline at end of file diff --git a/core/modules/layout/layouts/static/twocol/two-col.yml b/core/modules/layout/layouts/static/twocol/two-col.yml index e6c435e..4ecfcb8 100644 --- a/core/modules/layout/layouts/static/twocol/two-col.yml +++ b/core/modules/layout/layouts/static/twocol/two-col.yml @@ -1,12 +1,12 @@ -title: Two column -category: Columns: 2 +title: 'Two column' +category: 'Columns: 2' template: two-col stylesheets: - two-col.css regions: first: - label: Left side + label: 'Left side' type: content second: - label: Right side + label: 'Right side' type: aside diff --git a/core/modules/layout/lib/Drupal/layout/Plugin/Derivative/Layout.php b/core/modules/layout/lib/Drupal/layout/Plugin/Derivative/Layout.php index f313b5a..ec779a4 100644 --- a/core/modules/layout/lib/Drupal/layout/Plugin/Derivative/Layout.php +++ b/core/modules/layout/lib/Drupal/layout/Plugin/Derivative/Layout.php @@ -9,7 +9,7 @@ use DirectoryIterator; use Drupal\Component\Plugin\Derivative\DerivativeInterface; -use Drupal\Core\Config\FileStorage; +use Drupal\Component\Yaml\Yaml; /** * Layout plugin derivative definition. @@ -93,6 +93,7 @@ public function getDerivativeDefinitions(array $base_plugin_definition) { */ protected function iterateDirectories($dir, $provider) { $directories = new DirectoryIterator($dir); + $yaml = new Yaml(); foreach ($directories as $fileinfo) { if ($fileinfo->isDir() && !$fileinfo->isDot()) { // Keep discovering in subdirectories to arbitrary depth. @@ -102,9 +103,8 @@ protected function iterateDirectories($dir, $provider) { // Declarative layout definitions are defined with a .yml file in a // layout subdirectory. This provides all information about the layout // such as layout markup template and CSS and JavaScript files to use. - $directory = new FileStorage($fileinfo->getPath()); $key = $provider['provider'] . '__' . $fileinfo->getBasename('.yml'); - $this->derivatives[$key] = $directory->read($fileinfo->getBasename('.yml')); + $this->derivatives[$key] = $yaml->parse(file_get_contents($fileinfo->getRealPath())); $this->derivatives[$key]['theme'] = $key; $this->derivatives[$key]['path'] = $fileinfo->getPath(); $this->derivatives[$key]['provider'] = $provider; diff --git a/core/modules/layout/tests/layouts/static/one-col/one-col.yml b/core/modules/layout/tests/layouts/static/one-col/one-col.yml index 9048e14..317b3f0 100644 --- a/core/modules/layout/tests/layouts/static/one-col/one-col.yml +++ b/core/modules/layout/tests/layouts/static/one-col/one-col.yml @@ -1,7 +1,7 @@ -title: Single column -category: Columns: 1 +title: 'Single column' +category: 'Columns: 1' template: one-col regions: middle: - label: Middle column + label: 'Middle column' type: content diff --git a/core/modules/layout/tests/themes/layout_test_theme/layouts/static/two-col/two-col.yml b/core/modules/layout/tests/themes/layout_test_theme/layouts/static/two-col/two-col.yml index 01b9e86..f8b02bd 100644 --- a/core/modules/layout/tests/themes/layout_test_theme/layouts/static/two-col/two-col.yml +++ b/core/modules/layout/tests/themes/layout_test_theme/layouts/static/two-col/two-col.yml @@ -1,12 +1,12 @@ -title: Two column -category: Columns: 2 +title: 'Two column' +category: 'Columns: 2' template: two-col stylesheets: - two-col.css regions: left: - label: Left side + label: 'Left side' type: content right: - label: Right side + label: 'Right side' type: aside \ No newline at end of file diff --git a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_uid_argument.yml b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_uid_argument.yml index cdb3ca9..d162246 100644 --- a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_uid_argument.yml +++ b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_uid_argument.yml @@ -17,7 +17,7 @@ display: table: users field: uid title_enable: true - title: %1 + title: '%1' plugin_id: user_uid display_plugin: default display_title: Master