You are here

class SkinYamlDirectoryDiscovery in Skinr 8.2

Same name in this branch
  1. 8.2 src/Plugin/Discovery/SkinYamlDirectoryDiscovery.php \Drupal\skinr\Plugin\Discovery\SkinYamlDirectoryDiscovery
  2. 8.2 src/Component/Discovery/SkinYamlDirectoryDiscovery.php \Drupal\skinr\Component\Discovery\SkinYamlDirectoryDiscovery

Discovers multiple YAML files in a set of directories.

Once https://www.drupal.org/node/2671034 goes into core we can eliminate this.

Hierarchy

Expanded class hierarchy of SkinYamlDirectoryDiscovery

1 file declares its use of SkinYamlDirectoryDiscovery
SkinYamlDirectoryDiscovery.php in src/Plugin/Discovery/SkinYamlDirectoryDiscovery.php
Contains \Drupal\skinr\Plugin\Discovery\SkinYamlDirectoryDiscovery.

File

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

Namespace

Drupal\skinr\Component\Discovery
View source
class SkinYamlDirectoryDiscovery implements DiscoverableInterface {

  /**
   * An array of directories to scan, keyed by the provider.
   *
   * The value can either be a string or an array of strings. The string values
   * should be the path of a directory to scan.
   *
   * @var array
   */
  protected $directories = [];

  /**
   * The suffix the file cache key.
   *
   * @var string
   */
  protected $fileCacheKeySuffix;

  /**
   * The key which contains the value to key the data by.
   *
   * @var string
   */
  protected $key;

  /**
   * Constructs a YamlDirectoryDiscovery object.
   *
   * @param array $directories
   *   An array of directories to scan, keyed by the provider.
   * @param string $file_cache_key_suffix
   *   The file cache key suffix. This should be unique for each type of
   *   discovery.
   * @param string $key
   *   (optional) The key contained in the discovered data that identifies it.
   *   Defaults to 'id'.
   */
  public function __construct(array $directories, $file_cache_key_suffix, $key = 'id') {
    $this->directories = $directories;
    $this->fileCacheKeySuffix = $file_cache_key_suffix;
    $this->key = $key;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Gets the identifier from the data.
   *
   * @param array $data
   *   The data from the YAML file.
   *
   * @return string
   *   The identifier from the data.
   */
  protected function getIdentifier($file, array $data) {
    if (!isset($data[$this->key])) {
      throw new SkinDiscoveryException("The {$file} contains no data in the identifier key '{$this->key}'");
    }
    return $data[$this->key];
  }

  /**
   * Returns an array of providers keyed by file path.
   *
   * @return array
   *   An array of providers keyed by file path.
   */
  protected function findFiles() {
    $file_list = [];
    $pattern = '/\\.yml$/i';
    foreach ($this->directories as $provider => $directories) {
      $directories = (array) $directories;
      foreach ($directories as $directory) {
        if (is_dir($directory)) {
          $files = file_scan_directory($directory, $pattern);
          foreach ($files as $fileInfo) {
            $file_list[$fileInfo->uri] = $provider;
          }
        }
      }
    }
    return $file_list;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SkinYamlDirectoryDiscovery::$directories protected property An array of directories to scan, keyed by the provider.
SkinYamlDirectoryDiscovery::$fileCacheKeySuffix protected property The suffix the file cache key.
SkinYamlDirectoryDiscovery::$key protected property The key which contains the value to key the data by.
SkinYamlDirectoryDiscovery::findAll public function Returns an array of discoverable items. Overrides DiscoverableInterface::findAll
SkinYamlDirectoryDiscovery::findFiles protected function Returns an array of providers keyed by file path.
SkinYamlDirectoryDiscovery::getIdentifier protected function Gets the identifier from the data.
SkinYamlDirectoryDiscovery::__construct public function Constructs a YamlDirectoryDiscovery object.