-
Notifications
You must be signed in to change notification settings - Fork 2
/
YamlDirectoryDiscovery.php
159 lines (141 loc) · 4.55 KB
/
YamlDirectoryDiscovery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Drupal\Component\Discovery;
use Drupal\Component\FileSystem\RegexDirectoryIterator;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\Yaml;
use Drupal\Component\FileCache\FileCacheFactory;
/**
* Discovers multiple YAML files in a set of directories.
*/
class YamlDirectoryDiscovery implements DiscoverableInterface {
/**
* Defines the key in the discovered data where the file path is stored.
*/
const FILE_KEY = '_discovered_file_path';
/**
* 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 for the file cache key.
*
* @var string
*/
protected $fileCacheKeySuffix;
/**
* The key contained in the discovered data that identifies it.
*
* @var string
*/
protected $idKey;
/**
* Constructs a YamlDirectoryDiscovery object.
*
* @param array $directories
* 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.
* @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->idKey = $key;
}
/**
* {@inheritdoc}
*/
public function findAll() {
$all = [];
$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;
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 DiscoveryException("The $file contains invalid YAML", 0, $e);
}
$data[static::FILE_KEY] = $file;
$all[$provider][$this->getIdentifier($file, $data)] = $data;
$file_cache->set($file, $data);
}
}
return $all;
}
/**
* Gets the identifier from the data.
*
* @param string $file
* The filename.
* @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->idKey])) {
throw new DiscoveryException("The $file contains no data in the identifier key '{$this->idKey}'");
}
return $data[$this->idKey];
}
/**
* Returns an array of providers keyed by file path.
*
* @return array
* An array of providers keyed by file path.
*/
protected function findFiles() {
$file_list = [];
foreach ($this->directories as $provider => $directories) {
$directories = (array) $directories;
foreach ($directories as $directory) {
if (is_dir($directory)) {
/** @var \SplFileInfo $fileInfo */
foreach ($this->getDirectoryIterator($directory) as $fileInfo) {
$file_list[$fileInfo->getPathname()] = $provider;
}
}
}
}
return $file_list;
}
/**
* Gets an iterator to loop over the files in the provided directory.
*
* This method exists so that it is easy to replace this functionality in a
* class that extends this one. For example, it could be used to make the scan
* recursive.
*
* @param string $directory
* The directory to scan.
*
* @return \Traversable
* An \Traversable object or array where the values are \SplFileInfo
* objects.
*/
protected function getDirectoryIterator($directory) {
return new RegexDirectoryIterator($directory, '/\.yml$/i');
}
}