ConfigFinder.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Grav\Common\Config;
  3. use Grav\Common\Filesystem\Folder;
  4. /**
  5. * The Configuration Finder class.
  6. *
  7. * @author RocketTheme
  8. * @license MIT
  9. */
  10. class ConfigFinder
  11. {
  12. /**
  13. * Get all locations for blueprint files (including plugins).
  14. *
  15. * @param array $blueprints
  16. * @param array $plugins
  17. * @return array
  18. */
  19. public function locateBlueprintFiles(array $blueprints, array $plugins)
  20. {
  21. $list = [];
  22. foreach (array_reverse($plugins) as $folder) {
  23. $list += $this->detectInFolder($folder, 'blueprints');
  24. }
  25. foreach (array_reverse($blueprints) as $folder) {
  26. $list += $this->detectRecursive($folder);
  27. }
  28. return $list;
  29. }
  30. /**
  31. * Get all locations for configuration files (including plugins).
  32. *
  33. * @param array $configs
  34. * @param array $plugins
  35. * @return array
  36. */
  37. public function locateConfigFiles(array $configs, array $plugins)
  38. {
  39. $list = [];
  40. foreach (array_reverse($plugins) as $folder) {
  41. $list += $this->detectInFolder($folder);
  42. }
  43. foreach (array_reverse($configs) as $folder) {
  44. $list += $this->detectRecursive($folder);
  45. }
  46. return $list;
  47. }
  48. public function locateLanguageFiles(array $languages, array $plugins)
  49. {
  50. $list = [];
  51. foreach (array_reverse($plugins) as $folder) {
  52. $list += $this->detectLanguagesInFolder($folder, 'languages');
  53. }
  54. foreach (array_reverse($languages) as $folder) {
  55. $list += $this->detectRecursive($folder);
  56. }
  57. return $list;
  58. }
  59. /**
  60. * Get all locations for a single configuration file.
  61. *
  62. * @param array $folders Locations to look up from.
  63. * @param string $name Filename to be located.
  64. * @return array
  65. */
  66. public function locateConfigFile(array $folders, $name)
  67. {
  68. $filename = "{$name}.yaml";
  69. $list = [];
  70. foreach ($folders as $folder) {
  71. $path = trim(Folder::getRelativePath($folder), '/');
  72. if (is_file("{$folder}/{$filename}")) {
  73. $modified = filemtime("{$folder}/{$filename}");
  74. } else {
  75. $modified = 0;
  76. }
  77. $list[$path] = [$name => ['file' => "{$path}/{$filename}", 'modified' => $modified]];
  78. }
  79. return $list;
  80. }
  81. /**
  82. * Detects all plugins with a configuration file and returns them with last modification time.
  83. *
  84. * @param string $folder Location to look up from.
  85. * @param string $lookup Filename to be located.
  86. * @return array
  87. * @internal
  88. */
  89. protected function detectInFolder($folder, $lookup = null)
  90. {
  91. $path = trim(Folder::getRelativePath($folder), '/');
  92. $list = [];
  93. if (is_dir($folder)) {
  94. $iterator = new \FilesystemIterator($folder);
  95. /** @var \DirectoryIterator $directory */
  96. foreach ($iterator as $directory) {
  97. if (!$directory->isDir()) {
  98. continue;
  99. }
  100. $name = $directory->getBasename();
  101. $find = ($lookup ?: $name) . '.yaml';
  102. $filename = "{$path}/{$name}/$find";
  103. if (file_exists($filename)) {
  104. $list["plugins/{$name}"] = ['file' => $filename, 'modified' => filemtime($filename)];
  105. }
  106. }
  107. }
  108. return [$path => $list];
  109. }
  110. protected function detectLanguagesInFolder($folder, $lookup = null)
  111. {
  112. $path = trim(Folder::getRelativePath($folder), '/');
  113. $list = [];
  114. if (is_dir($folder)) {
  115. $iterator = new \FilesystemIterator($folder);
  116. /** @var \DirectoryIterator $directory */
  117. foreach ($iterator as $directory) {
  118. if (!$directory->isDir()) {
  119. continue;
  120. }
  121. $name = $directory->getBasename();
  122. $find = ($lookup ?: $name) . '.yaml';
  123. $filename = "{$path}/{$name}/$find";
  124. if (file_exists($filename)) {
  125. $list[$name] = ['file' => $filename, 'modified' => filemtime($filename)];
  126. }
  127. }
  128. }
  129. return [$path => $list];
  130. }
  131. /**
  132. * Detects all plugins with a configuration file and returns them with last modification time.
  133. *
  134. * @param string $folder Location to look up from.
  135. * @return array
  136. * @internal
  137. */
  138. protected function detectRecursive($folder)
  139. {
  140. $path = trim(Folder::getRelativePath($folder), '/');
  141. if (is_dir($folder)) {
  142. // Find all system and user configuration files.
  143. $options = [
  144. 'compare' => 'Filename',
  145. 'pattern' => '|\.yaml$|',
  146. 'filters' => [
  147. 'key' => '|\.yaml$|',
  148. 'value' => function (\RecursiveDirectoryIterator $file) use ($path) {
  149. return ['file' => "{$path}/{$file->getSubPathname()}", 'modified' => $file->getMTime()];
  150. }
  151. ],
  152. 'key' => 'SubPathname'
  153. ];
  154. $list = Folder::all($folder, $options);
  155. } else {
  156. $list = [];
  157. }
  158. return [$path => $list];
  159. }
  160. }