ConfigFileFinder.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * @package Grav\Common\Config
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Config;
  9. use DirectoryIterator;
  10. use Grav\Common\Filesystem\Folder;
  11. use RecursiveDirectoryIterator;
  12. /**
  13. * Class ConfigFileFinder
  14. * @package Grav\Common\Config
  15. */
  16. class ConfigFileFinder
  17. {
  18. /** @var string */
  19. protected $base = '';
  20. /**
  21. * @param string $base
  22. * @return $this
  23. */
  24. public function setBase($base)
  25. {
  26. $this->base = $base ? "{$base}/" : '';
  27. return $this;
  28. }
  29. /**
  30. * Return all locations for all the files with a timestamp.
  31. *
  32. * @param array $paths List of folders to look from.
  33. * @param string $pattern Pattern to match the file. Pattern will also be removed from the key.
  34. * @param int $levels Maximum number of recursive directories.
  35. * @return array
  36. */
  37. public function locateFiles(array $paths, $pattern = '|\.yaml$|', $levels = -1)
  38. {
  39. $list = [];
  40. foreach ($paths as $folder) {
  41. $list += $this->detectRecursive($folder, $pattern, $levels);
  42. }
  43. return $list;
  44. }
  45. /**
  46. * Return all locations for all the files with a timestamp.
  47. *
  48. * @param array $paths List of folders to look from.
  49. * @param string $pattern Pattern to match the file. Pattern will also be removed from the key.
  50. * @param int $levels Maximum number of recursive directories.
  51. * @return array
  52. */
  53. public function getFiles(array $paths, $pattern = '|\.yaml$|', $levels = -1)
  54. {
  55. $list = [];
  56. foreach ($paths as $folder) {
  57. $path = trim(Folder::getRelativePath($folder), '/');
  58. $files = $this->detectRecursive($folder, $pattern, $levels);
  59. $list += $files[trim($path, '/')];
  60. }
  61. return $list;
  62. }
  63. /**
  64. * Return all paths for all the files with a timestamp.
  65. *
  66. * @param array $paths List of folders to look from.
  67. * @param string $pattern Pattern to match the file. Pattern will also be removed from the key.
  68. * @param int $levels Maximum number of recursive directories.
  69. * @return array
  70. */
  71. public function listFiles(array $paths, $pattern = '|\.yaml$|', $levels = -1)
  72. {
  73. $list = [];
  74. foreach ($paths as $folder) {
  75. $list = array_merge_recursive($list, $this->detectAll($folder, $pattern, $levels));
  76. }
  77. return $list;
  78. }
  79. /**
  80. * Find filename from a list of folders.
  81. *
  82. * Note: Only finds the last override.
  83. *
  84. * @param string $filename
  85. * @param array $folders
  86. * @return array
  87. */
  88. public function locateFileInFolder($filename, array $folders)
  89. {
  90. $list = [];
  91. foreach ($folders as $folder) {
  92. $list += $this->detectInFolder($folder, $filename);
  93. }
  94. return $list;
  95. }
  96. /**
  97. * Find filename from a list of folders.
  98. *
  99. * @param array $folders
  100. * @param string|null $filename
  101. * @return array
  102. */
  103. public function locateInFolders(array $folders, $filename = null)
  104. {
  105. $list = [];
  106. foreach ($folders as $folder) {
  107. $path = trim(Folder::getRelativePath($folder), '/');
  108. $list[$path] = $this->detectInFolder($folder, $filename);
  109. }
  110. return $list;
  111. }
  112. /**
  113. * Return all existing locations for a single file with a timestamp.
  114. *
  115. * @param array $paths Filesystem paths to look up from.
  116. * @param string $name Configuration file to be located.
  117. * @param string $ext File extension (optional, defaults to .yaml).
  118. * @return array
  119. */
  120. public function locateFile(array $paths, $name, $ext = '.yaml')
  121. {
  122. $filename = preg_replace('|[.\/]+|', '/', $name) . $ext;
  123. $list = [];
  124. foreach ($paths as $folder) {
  125. $path = trim(Folder::getRelativePath($folder), '/');
  126. if (is_file("{$folder}/{$filename}")) {
  127. $modified = filemtime("{$folder}/{$filename}");
  128. } else {
  129. $modified = 0;
  130. }
  131. $basename = $this->base . $name;
  132. $list[$path] = [$basename => ['file' => "{$path}/{$filename}", 'modified' => $modified]];
  133. }
  134. return $list;
  135. }
  136. /**
  137. * Detects all directories with a configuration file and returns them with last modification time.
  138. *
  139. * @param string $folder Location to look up from.
  140. * @param string $pattern Pattern to match the file. Pattern will also be removed from the key.
  141. * @param int $levels Maximum number of recursive directories.
  142. * @return array
  143. * @internal
  144. */
  145. protected function detectRecursive($folder, $pattern, $levels)
  146. {
  147. $path = trim(Folder::getRelativePath($folder), '/');
  148. if (is_dir($folder)) {
  149. // Find all system and user configuration files.
  150. $options = [
  151. 'levels' => $levels,
  152. 'compare' => 'Filename',
  153. 'pattern' => $pattern,
  154. 'filters' => [
  155. 'pre-key' => $this->base,
  156. 'key' => $pattern,
  157. 'value' => function (RecursiveDirectoryIterator $file) use ($path) {
  158. return ['file' => "{$path}/{$file->getSubPathname()}", 'modified' => $file->getMTime()];
  159. }
  160. ],
  161. 'key' => 'SubPathname'
  162. ];
  163. $list = Folder::all($folder, $options);
  164. ksort($list);
  165. } else {
  166. $list = [];
  167. }
  168. return [$path => $list];
  169. }
  170. /**
  171. * Detects all directories with the lookup file and returns them with last modification time.
  172. *
  173. * @param string $folder Location to look up from.
  174. * @param string|null $lookup Filename to be located (defaults to directory name).
  175. * @return array
  176. * @internal
  177. */
  178. protected function detectInFolder($folder, $lookup = null)
  179. {
  180. $folder = rtrim($folder, '/');
  181. $path = trim(Folder::getRelativePath($folder), '/');
  182. $base = $path === $folder ? '' : ($path ? substr($folder, 0, -strlen($path)) : $folder . '/');
  183. $list = [];
  184. if (is_dir($folder)) {
  185. $iterator = new DirectoryIterator($folder);
  186. foreach ($iterator as $directory) {
  187. if (!$directory->isDir() || $directory->isDot()) {
  188. continue;
  189. }
  190. $name = $directory->getFilename();
  191. $find = ($lookup ?: $name) . '.yaml';
  192. $filename = "{$path}/{$name}/{$find}";
  193. if (file_exists($base . $filename)) {
  194. $basename = $this->base . $name;
  195. $list[$basename] = ['file' => $filename, 'modified' => filemtime($base . $filename)];
  196. }
  197. }
  198. }
  199. return $list;
  200. }
  201. /**
  202. * Detects all plugins with a configuration file and returns them with last modification time.
  203. *
  204. * @param string $folder Location to look up from.
  205. * @param string $pattern Pattern to match the file. Pattern will also be removed from the key.
  206. * @param int $levels Maximum number of recursive directories.
  207. * @return array
  208. * @internal
  209. */
  210. protected function detectAll($folder, $pattern, $levels)
  211. {
  212. $path = trim(Folder::getRelativePath($folder), '/');
  213. if (is_dir($folder)) {
  214. // Find all system and user configuration files.
  215. $options = [
  216. 'levels' => $levels,
  217. 'compare' => 'Filename',
  218. 'pattern' => $pattern,
  219. 'filters' => [
  220. 'pre-key' => $this->base,
  221. 'key' => $pattern,
  222. 'value' => function (RecursiveDirectoryIterator $file) use ($path) {
  223. return ["{$path}/{$file->getSubPathname()}" => $file->getMTime()];
  224. }
  225. ],
  226. 'key' => 'SubPathname'
  227. ];
  228. $list = Folder::all($folder, $options);
  229. ksort($list);
  230. } else {
  231. $list = [];
  232. }
  233. return $list;
  234. }
  235. }