123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace Grav\Common\Config;
- use Grav\Common\Filesystem\Folder;
- /**
- * The Configuration Finder class.
- *
- * @author RocketTheme
- * @license MIT
- */
- class ConfigFinder
- {
- /**
- * Get all locations for blueprint files (including plugins).
- *
- * @param array $blueprints
- * @param array $plugins
- * @return array
- */
- public function locateBlueprintFiles(array $blueprints, array $plugins)
- {
- $list = [];
- foreach (array_reverse($plugins) as $folder) {
- $list += $this->detectInFolder($folder, 'blueprints');
- }
- foreach (array_reverse($blueprints) as $folder) {
- $list += $this->detectRecursive($folder);
- }
- return $list;
- }
- /**
- * Get all locations for configuration files (including plugins).
- *
- * @param array $configs
- * @param array $plugins
- * @return array
- */
- public function locateConfigFiles(array $configs, array $plugins)
- {
- $list = [];
- foreach (array_reverse($plugins) as $folder) {
- $list += $this->detectInFolder($folder);
- }
- foreach (array_reverse($configs) as $folder) {
- $list += $this->detectRecursive($folder);
- }
- return $list;
- }
- public function locateLanguageFiles(array $languages, array $plugins)
- {
- $list = [];
- foreach (array_reverse($plugins) as $folder) {
- $list += $this->detectLanguagesInFolder($folder, 'languages');
- }
- foreach (array_reverse($languages) as $folder) {
- $list += $this->detectRecursive($folder);
- }
- return $list;
- }
- /**
- * Get all locations for a single configuration file.
- *
- * @param array $folders Locations to look up from.
- * @param string $name Filename to be located.
- * @return array
- */
- public function locateConfigFile(array $folders, $name)
- {
- $filename = "{$name}.yaml";
- $list = [];
- foreach ($folders as $folder) {
- $path = trim(Folder::getRelativePath($folder), '/');
- if (is_file("{$folder}/{$filename}")) {
- $modified = filemtime("{$folder}/{$filename}");
- } else {
- $modified = 0;
- }
- $list[$path] = [$name => ['file' => "{$path}/{$filename}", 'modified' => $modified]];
- }
- return $list;
- }
- /**
- * Detects all plugins with a configuration file and returns them with last modification time.
- *
- * @param string $folder Location to look up from.
- * @param string $lookup Filename to be located.
- * @return array
- * @internal
- */
- protected function detectInFolder($folder, $lookup = null)
- {
- $path = trim(Folder::getRelativePath($folder), '/');
- $list = [];
- if (is_dir($folder)) {
- $iterator = new \FilesystemIterator($folder);
- /** @var \DirectoryIterator $directory */
- foreach ($iterator as $directory) {
- if (!$directory->isDir()) {
- continue;
- }
- $name = $directory->getBasename();
- $find = ($lookup ?: $name) . '.yaml';
- $filename = "{$path}/{$name}/$find";
- if (file_exists($filename)) {
- $list["plugins/{$name}"] = ['file' => $filename, 'modified' => filemtime($filename)];
- }
- }
- }
- return [$path => $list];
- }
- protected function detectLanguagesInFolder($folder, $lookup = null)
- {
- $path = trim(Folder::getRelativePath($folder), '/');
- $list = [];
- if (is_dir($folder)) {
- $iterator = new \FilesystemIterator($folder);
- /** @var \DirectoryIterator $directory */
- foreach ($iterator as $directory) {
- if (!$directory->isDir()) {
- continue;
- }
- $name = $directory->getBasename();
- $find = ($lookup ?: $name) . '.yaml';
- $filename = "{$path}/{$name}/$find";
- if (file_exists($filename)) {
- $list[$name] = ['file' => $filename, 'modified' => filemtime($filename)];
- }
- }
- }
- return [$path => $list];
- }
- /**
- * Detects all plugins with a configuration file and returns them with last modification time.
- *
- * @param string $folder Location to look up from.
- * @return array
- * @internal
- */
- protected function detectRecursive($folder)
- {
- $path = trim(Folder::getRelativePath($folder), '/');
- if (is_dir($folder)) {
- // Find all system and user configuration files.
- $options = [
- 'compare' => 'Filename',
- 'pattern' => '|\.yaml$|',
- 'filters' => [
- 'key' => '|\.yaml$|',
- 'value' => function (\RecursiveDirectoryIterator $file) use ($path) {
- return ['file' => "{$path}/{$file->getSubPathname()}", 'modified' => $file->getMTime()];
- }
- ],
- 'key' => 'SubPathname'
- ];
- $list = Folder::all($folder, $options);
- } else {
- $list = [];
- }
- return [$path => $list];
- }
- }
|