YamlDiscovery.php in Drupal 9
File
core/lib/Drupal/Component/Discovery/YamlDiscovery.php
View source
<?php
namespace Drupal\Component\Discovery;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\Yaml;
class YamlDiscovery implements DiscoverableInterface {
protected $name;
protected $directories = [];
public function __construct($name, array $directories) {
$this->name = $name;
$this->directories = $directories;
}
public function findAll() {
$all = [];
$files = $this
->findFiles();
$provider_by_files = array_flip($files);
$file_cache = FileCacheFactory::get('yaml_discovery:' . $this->name);
foreach ($file_cache
->getMultiple($files) as $file => $data) {
$all[$provider_by_files[$file]] = $data;
unset($provider_by_files[$file]);
}
if ($provider_by_files) {
foreach ($provider_by_files as $file => $provider) {
$all[$provider] = $this
->decode($file);
$file_cache
->set($file, $all[$provider]);
}
}
return $all;
}
protected function decode($file) {
try {
return Yaml::decode(file_get_contents($file)) ?: [];
} catch (InvalidDataTypeException $e) {
throw new InvalidDataTypeException($file . ': ' . $e
->getMessage(), $e
->getCode(), $e);
}
}
protected function findFiles() {
$files = [];
foreach ($this->directories as $provider => $directory) {
$file = $directory . '/' . $provider . '.' . $this->name . '.yml';
if (file_exists($file)) {
$files[$provider] = $file;
}
}
return $files;
}
}
Classes
Name |
Description |
YamlDiscovery |
Provides discovery for YAML files within a given set of directories. |