YamlDiscovery.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Drupal\Component\Discovery;
  3. use Drupal\Component\FileCache\FileCacheFactory;
  4. use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
  5. use Drupal\Component\Serialization\Yaml;
  6. /**
  7. * Provides discovery for YAML files within a given set of directories.
  8. */
  9. class YamlDiscovery implements DiscoverableInterface {
  10. /**
  11. * The base filename to look for in each directory.
  12. *
  13. * @var string
  14. */
  15. protected $name;
  16. /**
  17. * An array of directories to scan, keyed by the provider.
  18. *
  19. * @var array
  20. */
  21. protected $directories = [];
  22. /**
  23. * Constructs a YamlDiscovery object.
  24. *
  25. * @param string $name
  26. * The base filename to look for in each directory. The format will be
  27. * $provider.$name.yml.
  28. * @param array $directories
  29. * An array of directories to scan, keyed by the provider.
  30. */
  31. public function __construct($name, array $directories) {
  32. $this->name = $name;
  33. $this->directories = $directories;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function findAll() {
  39. $all = [];
  40. $files = $this->findFiles();
  41. $provider_by_files = array_flip($files);
  42. $file_cache = FileCacheFactory::get('yaml_discovery:' . $this->name);
  43. // Try to load from the file cache first.
  44. foreach ($file_cache->getMultiple($files) as $file => $data) {
  45. $all[$provider_by_files[$file]] = $data;
  46. unset($provider_by_files[$file]);
  47. }
  48. // If there are files left that were not returned from the cache, load and
  49. // parse them now. This list was flipped above and is keyed by filename.
  50. if ($provider_by_files) {
  51. foreach ($provider_by_files as $file => $provider) {
  52. // If a file is empty or its contents are commented out, return an empty
  53. // array instead of NULL for type consistency.
  54. $all[$provider] = $this->decode($file);
  55. $file_cache->set($file, $all[$provider]);
  56. }
  57. }
  58. return $all;
  59. }
  60. /**
  61. * Decode a YAML file.
  62. *
  63. * @param string $file
  64. * Yaml file path.
  65. *
  66. * @return array
  67. */
  68. protected function decode($file) {
  69. try {
  70. return Yaml::decode(file_get_contents($file)) ?: [];
  71. }
  72. catch (InvalidDataTypeException $e) {
  73. throw new InvalidDataTypeException($file . ': ' . $e->getMessage(), $e->getCode(), $e);
  74. }
  75. }
  76. /**
  77. * Returns an array of file paths, keyed by provider.
  78. *
  79. * @return array
  80. */
  81. protected function findFiles() {
  82. $files = [];
  83. foreach ($this->directories as $provider => $directory) {
  84. $file = $directory . '/' . $provider . '.' . $this->name . '.yml';
  85. if (file_exists($file)) {
  86. $files[$provider] = $file;
  87. }
  88. }
  89. return $files;
  90. }
  91. }