You are here

public function SkinYamlDirectoryDiscovery::findAll in Skinr 8.2

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

src/Component/Discovery/SkinYamlDirectoryDiscovery.php, line 67
Contains \Drupal\skinr\Component\Discovery\SkinYamlDirectoryDiscovery.

Class

SkinYamlDirectoryDiscovery
Discovers multiple YAML files in a set of directories.

Namespace

Drupal\skinr\Component\Discovery

Code

public function findAll() {
  $all = array();
  $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 + [
      'path' => $file,
    ];
    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 SkinDiscoveryException("The {$file} contains invalid YAML");
      }
      $all[$provider][$this
        ->getIdentifier($file, $data)] = $data + [
        'path' => $file,
      ];
      $file_cache
        ->set($file, $data);
    }
  }
  return $all;
}