class YamlDirectoryDiscovery in Drupal 9
Same name in this branch
- 9 core/lib/Drupal/Component/Discovery/YamlDirectoryDiscovery.php \Drupal\Component\Discovery\YamlDirectoryDiscovery
- 9 core/lib/Drupal/Core/Plugin/Discovery/YamlDirectoryDiscovery.php \Drupal\Core\Plugin\Discovery\YamlDirectoryDiscovery
Same name and namespace in other branches
- 8 core/lib/Drupal/Component/Discovery/YamlDirectoryDiscovery.php \Drupal\Component\Discovery\YamlDirectoryDiscovery
Discovers multiple YAML files in a set of directories.
Hierarchy
- class \Drupal\Component\Discovery\YamlDirectoryDiscovery implements DiscoverableInterface
Expanded class hierarchy of YamlDirectoryDiscovery
3 files declare their use of YamlDirectoryDiscovery
- YamlDirectoryDiscovery.php in core/
lib/ Drupal/ Core/ Plugin/ Discovery/ YamlDirectoryDiscovery.php - YamlDirectoryDiscoveryTest.php in core/
tests/ Drupal/ Tests/ Component/ Discovery/ YamlDirectoryDiscoveryTest.php - YamlDirectoryDiscoveryTest.php in core/
tests/ Drupal/ Tests/ Core/ Plugin/ Discovery/ YamlDirectoryDiscoveryTest.php
File
- core/
lib/ Drupal/ Component/ Discovery/ YamlDirectoryDiscovery.php, line 13
Namespace
Drupal\Component\DiscoveryView source
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');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
YamlDirectoryDiscovery:: |
protected | property | An array of directories to scan, keyed by the provider. | |
YamlDirectoryDiscovery:: |
protected | property | The suffix for the file cache key. | |
YamlDirectoryDiscovery:: |
protected | property | The key contained in the discovered data that identifies it. | |
YamlDirectoryDiscovery:: |
constant | Defines the key in the discovered data where the file path is stored. | ||
YamlDirectoryDiscovery:: |
public | function |
Returns an array of discoverable items. Overrides DiscoverableInterface:: |
|
YamlDirectoryDiscovery:: |
protected | function | Returns an array of providers keyed by file path. | |
YamlDirectoryDiscovery:: |
protected | function | Gets an iterator to loop over the files in the provided directory. | |
YamlDirectoryDiscovery:: |
protected | function | Gets the identifier from the data. | |
YamlDirectoryDiscovery:: |
public | function | Constructs a YamlDirectoryDiscovery object. |