You are here

public function YamlDirectoryDiscovery::findAll in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Discovery/YamlDirectoryDiscovery.php \Drupal\Component\Discovery\YamlDirectoryDiscovery::findAll()

Returns an array of discoverable items.

Return value

array An array of discovered data keyed by provider.

Throws

\Drupal\Component\Discovery\DiscoveryException Exception thrown if there is a problem during discovery.

Overrides DiscoverableInterface::findAll

File

core/lib/Drupal/Component/Discovery/YamlDirectoryDiscovery.php, line 67

Class

YamlDirectoryDiscovery
Discovers multiple YAML files in a set of directories.

Namespace

Drupal\Component\Discovery

Code

public function findAll() {
  $all = [];
  $files = $this
    ->findFiles();
  $file_cache = FileCacheFactory::get('yaml_discovery:' . $this->fileCacheKeySuffix);

  // Try to load from the file cache first.
  foreach ($file_cache
    ->getMultiple(array_keys($files)) as $file => $data) {
    $all[$files[$file]][$this
      ->getIdentifier($file, $data)] = $data;
    unset($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 ($files) {
    foreach ($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.
      try {
        $data = Yaml::decode(file_get_contents($file)) ?: [];
      } catch (InvalidDataTypeException $e) {
        throw new DiscoveryException("The {$file} contains invalid YAML", 0, $e);
      }
      $data[static::FILE_KEY] = $file;
      $all[$provider][$this
        ->getIdentifier($file, $data)] = $data;
      $file_cache
        ->set($file, $data);
    }
  }
  return $all;
}