3 namespace Drupal\Component\Discovery;
5 use Drupal\Component\FileSystem\RegexDirectoryIterator;
6 use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
7 use Drupal\Component\Serialization\Yaml;
8 use Drupal\Component\FileCache\FileCacheFactory;
11 * Discovers multiple YAML files in a set of directories.
13 class YamlDirectoryDiscovery implements DiscoverableInterface {
16 * Defines the key in the discovered data where the file path is stored.
18 const FILE_KEY = '_discovered_file_path';
21 * An array of directories to scan, keyed by the provider.
23 * The value can either be a string or an array of strings. The string values
24 * should be the path of a directory to scan.
28 protected $directories = [];
31 * The suffix for the file cache key.
35 protected $fileCacheKeySuffix;
38 * The key contained in the discovered data that identifies it.
45 * Constructs a YamlDirectoryDiscovery object.
47 * @param array $directories
48 * An array of directories to scan, keyed by the provider. The value can
49 * either be a string or an array of strings. The string values should be
50 * the path of a directory to scan.
51 * @param string $file_cache_key_suffix
52 * The file cache key suffix. This should be unique for each type of
55 * (optional) The key contained in the discovered data that identifies it.
58 public function __construct(array $directories, $file_cache_key_suffix, $key = 'id') {
59 $this->directories = $directories;
60 $this->fileCacheKeySuffix = $file_cache_key_suffix;
67 public function findAll() {
70 $files = $this->findFiles();
72 $file_cache = FileCacheFactory::get('yaml_discovery:' . $this->fileCacheKeySuffix);
74 // Try to load from the file cache first.
75 foreach ($file_cache->getMultiple(array_keys($files)) as $file => $data) {
76 $all[$files[$file]][$this->getIdentifier($file, $data)] = $data;
80 // If there are files left that were not returned from the cache, load and
81 // parse them now. This list was flipped above and is keyed by filename.
83 foreach ($files as $file => $provider) {
84 // If a file is empty or its contents are commented out, return an empty
85 // array instead of NULL for type consistency.
87 $data = Yaml::decode(file_get_contents($file)) ?: [];
89 catch (InvalidDataTypeException $e) {
90 throw new DiscoveryException("The $file contains invalid YAML", 0, $e);
92 $data[static::FILE_KEY] = $file;
93 $all[$provider][$this->getIdentifier($file, $data)] = $data;
94 $file_cache->set($file, $data);
102 * Gets the identifier from the data.
104 * @param string $file
107 * The data from the YAML file.
110 * The identifier from the data.
112 protected function getIdentifier($file, array $data) {
113 if (!isset($data[$this->idKey])) {
114 throw new DiscoveryException("The $file contains no data in the identifier key '{$this->idKey}'");
116 return $data[$this->idKey];
120 * Returns an array of providers keyed by file path.
123 * An array of providers keyed by file path.
125 protected function findFiles() {
127 foreach ($this->directories as $provider => $directories) {
128 $directories = (array) $directories;
129 foreach ($directories as $directory) {
130 if (is_dir($directory)) {
131 /** @var \SplFileInfo $fileInfo */
132 foreach ($this->getDirectoryIterator($directory) as $fileInfo) {
133 $file_list[$fileInfo->getPathname()] = $provider;
142 * Gets an iterator to loop over the files in the provided directory.
144 * This method exists so that it is easy to replace this functionality in a
145 * class that extends this one. For example, it could be used to make the scan
148 * @param string $directory
149 * The directory to scan.
151 * @return \Traversable
152 * An \Traversable object or array where the values are \SplFileInfo
155 protected function getDirectoryIterator($directory) {
156 return new RegexDirectoryIterator($directory, '/\.yml$/i');