Same name in this branch
  1. 10 core/lib/Drupal/Component/Discovery/YamlDiscovery.php \Drupal\Component\Discovery\YamlDiscovery
  2. 10 core/lib/Drupal/Core/Discovery/YamlDiscovery.php \Drupal\Core\Discovery\YamlDiscovery
  3. 10 core/lib/Drupal/Core/Plugin/Discovery/YamlDiscovery.php \Drupal\Core\Plugin\Discovery\YamlDiscovery
Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Component/Discovery/YamlDiscovery.php \Drupal\Component\Discovery\YamlDiscovery
  2. 9 core/lib/Drupal/Component/Discovery/YamlDiscovery.php \Drupal\Component\Discovery\YamlDiscovery

Provides discovery for YAML files within a given set of directories.

Hierarchy

Expanded class hierarchy of YamlDiscovery

4 files declare their use of YamlDiscovery
StateFileExistsTest.php in core/modules/migrate_drupal/tests/src/Kernel/StateFileExistsTest.php
ValidateMigrationStateTestTrait.php in core/modules/migrate_drupal/tests/src/Traits/ValidateMigrationStateTestTrait.php
YamlDiscovery.php in core/lib/Drupal/Core/Discovery/YamlDiscovery.php
YamlDiscoveryTest.php in core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php

File

core/lib/Drupal/Component/Discovery/YamlDiscovery.php, line 12

Namespace

Drupal\Component\Discovery
View source
class YamlDiscovery implements DiscoverableInterface {

  /**
   * The base filename to look for in each directory.
   *
   * @var string
   */
  protected $name;

  /**
   * An array of directories to scan, keyed by the provider.
   *
   * @var array
   */
  protected $directories = [];

  /**
   * Constructs a YamlDiscovery object.
   *
   * @param string $name
   *   The base filename to look for in each directory. The format will be
   *   $provider.$name.yml.
   * @param array $directories
   *   An array of directories to scan, keyed by the provider.
   */
  public function __construct($name, array $directories) {
    $this->name = $name;
    $this->directories = $directories;
  }

  /**
   * {@inheritdoc}
   */
  public function findAll() {
    $all = [];
    $files = $this
      ->findFiles();
    $provider_by_files = array_flip($files);
    $file_cache = FileCacheFactory::get('yaml_discovery:' . $this->name);

    // Try to load from the file cache first.
    foreach ($file_cache
      ->getMultiple($files) as $file => $data) {
      $all[$provider_by_files[$file]] = $data;
      unset($provider_by_files[$file]);
    }

    // If there are files left that were not returned from the cache, load and
    // parse them now. This list was flipped above and is keyed by filename.
    if ($provider_by_files) {
      foreach ($provider_by_files as $file => $provider) {

        // If a file is empty or its contents are commented out, return an empty
        // array instead of NULL for type consistency.
        $all[$provider] = $this
          ->decode($file);
        $file_cache
          ->set($file, $all[$provider]);
      }
    }
    return $all;
  }

  /**
   * Decode a YAML file.
   *
   * @param string $file
   *   Yaml file path.
   *
   * @return array
   */
  protected function decode($file) {
    try {
      return Yaml::decode(file_get_contents($file)) ?: [];
    } catch (InvalidDataTypeException $e) {
      throw new InvalidDataTypeException($file . ': ' . $e
        ->getMessage(), $e
        ->getCode(), $e);
    }
  }

  /**
   * Returns an array of file paths, keyed by provider.
   *
   * @return array
   */
  protected function findFiles() {
    $files = [];
    foreach ($this->directories as $provider => $directory) {
      $file = $directory . '/' . $provider . '.' . $this->name . '.yml';
      if (file_exists($file)) {
        $files[$provider] = $file;
      }
    }
    return $files;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
YamlDiscovery::$directories protected property An array of directories to scan, keyed by the provider.
YamlDiscovery::$name protected property The base filename to look for in each directory.
YamlDiscovery::decode protected function Decode a YAML file. 1
YamlDiscovery::findAll public function Returns an array of discoverable items. Overrides DiscoverableInterface::findAll
YamlDiscovery::findFiles protected function Returns an array of file paths, keyed by provider.
YamlDiscovery::__construct public function Constructs a YamlDiscovery object.