You are here

class Yaml in Drupal 9

Same name in this branch
  1. 9 core/lib/Drupal/Core/Serialization/Yaml.php \Drupal\Core\Serialization\Yaml
  2. 9 core/lib/Drupal/Component/Serialization/Yaml.php \Drupal\Component\Serialization\Yaml
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Component/Serialization/Yaml.php \Drupal\Component\Serialization\Yaml

Provides a YAML serialization implementation.

Proxy implementation that will choose the best library based on availability.

Hierarchy

Expanded class hierarchy of Yaml

12 files declare their use of Yaml
DistributionProfileExistingSettingsTest.php in core/tests/Drupal/FunctionalTests/Installer/DistributionProfileExistingSettingsTest.php
ExtensionListTest.php in core/tests/Drupal/Tests/Core/Extension/ExtensionListTest.php
FrontMatterTest.php in core/tests/Drupal/Tests/Component/FrontMatter/FrontMatterTest.php
GenerateTheme.php in core/lib/Drupal/Core/Command/GenerateTheme.php
InstallerExistingConfigTestBase.php in core/tests/Drupal/FunctionalTests/Installer/InstallerExistingConfigTestBase.php

... See full list

1 string reference to 'Yaml'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses Yaml
serialization.yaml in core/core.services.yml
Drupal\Component\Serialization\Yaml

File

core/lib/Drupal/Component/Serialization/Yaml.php, line 10

Namespace

Drupal\Component\Serialization
View source
class Yaml implements SerializationInterface {

  /**
   * The YAML implementation to use.
   *
   * @var \Drupal\Component\Serialization\SerializationInterface
   */
  protected static $serializer;

  /**
   * {@inheritdoc}
   */
  public static function encode($data) {

    // 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) {
    $serializer = static::getSerializer();
    return $serializer::decode($raw);
  }

  /**
   * {@inheritdoc}
   */
  public static function getFileExtension() {
    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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Yaml::$serializer protected static property The YAML implementation to use.
Yaml::decode public static function Decodes data from the serialization format. Overrides SerializationInterface::decode
Yaml::encode public static function Encodes data into the serialization format. Overrides SerializationInterface::encode
Yaml::getFileExtension public static function Gets the file extension for this serialization format. Overrides SerializationInterface::getFileExtension
Yaml::getSerializer protected static function Determines which implementation to use for parsing YAML. 2