Version 1
[yaffs-website] / web / core / lib / Drupal / Component / Discovery / YamlDirectoryDiscovery.php
1 <?php
2
3 namespace Drupal\Component\Discovery;
4
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;
9
10 /**
11  * Discovers multiple YAML files in a set of directories.
12  */
13 class YamlDirectoryDiscovery implements DiscoverableInterface {
14
15   /**
16    * Defines the key in the discovered data where the file path is stored.
17    */
18   const FILE_KEY = '_discovered_file_path';
19
20   /**
21    * An array of directories to scan, keyed by the provider.
22    *
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.
25    *
26    * @var array
27    */
28   protected $directories = [];
29
30   /**
31    * The suffix for the file cache key.
32    *
33    * @var string
34    */
35   protected $fileCacheKeySuffix;
36
37   /**
38    * The key contained in the discovered data that identifies it.
39    *
40    * @var string
41    */
42   protected $idKey;
43
44   /**
45    * Constructs a YamlDirectoryDiscovery object.
46    *
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
53    *   discovery.
54    * @param string $key
55    *   (optional) The key contained in the discovered data that identifies it.
56    *   Defaults to 'id'.
57    */
58   public function __construct(array $directories, $file_cache_key_suffix, $key = 'id') {
59     $this->directories = $directories;
60     $this->fileCacheKeySuffix = $file_cache_key_suffix;
61     $this->idKey = $key;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function findAll() {
68     $all = [];
69
70     $files = $this->findFiles();
71
72     $file_cache = FileCacheFactory::get('yaml_discovery:' . $this->fileCacheKeySuffix);
73
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;
77       unset($files[$file]);
78     }
79
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.
82     if ($files) {
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.
86         try {
87           $data = Yaml::decode(file_get_contents($file)) ?: [];
88         }
89         catch (InvalidDataTypeException $e) {
90           throw new DiscoveryException("The $file contains invalid YAML", 0, $e);
91         }
92         $data[static::FILE_KEY] = $file;
93         $all[$provider][$this->getIdentifier($file, $data)] = $data;
94         $file_cache->set($file, $data);
95       }
96     }
97
98     return $all;
99   }
100
101   /**
102    * Gets the identifier from the data.
103    *
104    * @param string $file
105    *   The filename.
106    * @param array $data
107    *   The data from the YAML file.
108    *
109    * @return string
110    *   The identifier from the data.
111    */
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}'");
115     }
116     return $data[$this->idKey];
117   }
118
119   /**
120    * Returns an array of providers keyed by file path.
121    *
122    * @return array
123    *   An array of providers keyed by file path.
124    */
125   protected function findFiles() {
126     $file_list = [];
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;
134           }
135         }
136       }
137     }
138     return $file_list;
139   }
140
141   /**
142    * Gets an iterator to loop over the files in the provided directory.
143    *
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
146    * recursive.
147    *
148    * @param string $directory
149    *   The directory to scan.
150    *
151    * @return \Traversable
152    *   An \Traversable object or array where the values are \SplFileInfo
153    *   objects.
154    */
155   protected function getDirectoryIterator($directory) {
156     return new RegexDirectoryIterator($directory, '/\.yml$/i');
157   }
158
159 }