Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
CRAP
90.48% covered (success)
90.48%
19 / 21
YamlDiscovery
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
9.07
90.48% covered (success)
90.48%
19 / 21
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 findAll
0.00% covered (danger)
0.00%
0 / 1
5.12
83.33% covered (warning)
83.33%
10 / 12
 findFiles
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
6 / 6
<?php
/**
 * @file
 * Contains \Drupal\Component\Discovery\YamlDiscovery.
 */
namespace Drupal\Component\Discovery;
use Drupal\Component\Serialization\Yaml;
use Drupal\Component\FileCache\FileCacheFactory;
/**
 * Provides discovery for YAML files within a given set of directories.
 */
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 = array();
  /**
   * 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 = array();
    $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] = Yaml::decode(file_get_contents($file)) ?: [];
        $file_cache->set($file, $all[$provider]);
      }
    }
    return $all;
  }
  /**
   * Returns an array of file paths, keyed by provider.
   *
   * @return array
   */
  protected function findFiles() {
    $files = array();
    foreach ($this->directories as $provider => $directory) {
      $file = $directory . '/' . $provider . '.' . $this->name . '.yml';
      if (file_exists($file)) {
        $files[$provider] = $file;
      }
    }
    return $files;
  }
}