ConfigServiceProvider.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * @package Grav\Common\Service
  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\Service;
  9. use DirectoryIterator;
  10. use Grav\Common\Config\CompiledBlueprints;
  11. use Grav\Common\Config\CompiledConfig;
  12. use Grav\Common\Config\CompiledLanguages;
  13. use Grav\Common\Config\Config;
  14. use Grav\Common\Config\ConfigFileFinder;
  15. use Grav\Common\Config\Setup;
  16. use Grav\Common\Language\Language;
  17. use Grav\Framework\Mime\MimeTypes;
  18. use Pimple\Container;
  19. use Pimple\ServiceProviderInterface;
  20. use RocketTheme\Toolbox\File\YamlFile;
  21. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  22. /**
  23. * Class ConfigServiceProvider
  24. * @package Grav\Common\Service
  25. */
  26. class ConfigServiceProvider implements ServiceProviderInterface
  27. {
  28. /**
  29. * @param Container $container
  30. * @return void
  31. */
  32. public function register(Container $container)
  33. {
  34. $container['setup'] = function ($c) {
  35. $setup = new Setup($c);
  36. $setup->init();
  37. return $setup;
  38. };
  39. $container['blueprints'] = function ($c) {
  40. return static::blueprints($c);
  41. };
  42. $container['config'] = function ($c) {
  43. $config = static::load($c);
  44. // After configuration has been loaded, we can disable YAML compatibility if strict mode has been enabled.
  45. if (!$config->get('system.strict_mode.yaml_compat', true)) {
  46. YamlFile::globalSettings(['compat' => false, 'native' => true]);
  47. }
  48. return $config;
  49. };
  50. $container['mime'] = function ($c) {
  51. /** @var Config $config */
  52. $config = $c['config'];
  53. $mimes = $config->get('mime.types', []);
  54. foreach ($config->get('media.types', []) as $ext => $media) {
  55. if (!empty($media['mime'])) {
  56. $mimes[$ext] = array_unique(array_merge([$media['mime']], $mimes[$ext] ?? []));
  57. }
  58. }
  59. return MimeTypes::createFromMimes($mimes);
  60. };
  61. $container['languages'] = function ($c) {
  62. return static::languages($c);
  63. };
  64. $container['language'] = function ($c) {
  65. return new Language($c);
  66. };
  67. }
  68. /**
  69. * @param Container $container
  70. * @return mixed
  71. */
  72. public static function blueprints(Container $container)
  73. {
  74. /** Setup $setup */
  75. $setup = $container['setup'];
  76. /** @var UniformResourceLocator $locator */
  77. $locator = $container['locator'];
  78. $cache = $locator->findResource('cache://compiled/blueprints', true, true);
  79. $files = [];
  80. $paths = $locator->findResources('blueprints://config');
  81. $files += (new ConfigFileFinder)->locateFiles($paths);
  82. $paths = $locator->findResources('plugins://');
  83. $files += (new ConfigFileFinder)->setBase('plugins')->locateInFolders($paths, 'blueprints');
  84. $paths = $locator->findResources('themes://');
  85. $files += (new ConfigFileFinder)->setBase('themes')->locateInFolders($paths, 'blueprints');
  86. $blueprints = new CompiledBlueprints($cache, $files, GRAV_ROOT);
  87. return $blueprints->name("master-{$setup->environment}")->load();
  88. }
  89. /**
  90. * @param Container $container
  91. * @return Config
  92. */
  93. public static function load(Container $container)
  94. {
  95. /** Setup $setup */
  96. $setup = $container['setup'];
  97. /** @var UniformResourceLocator $locator */
  98. $locator = $container['locator'];
  99. $cache = $locator->findResource('cache://compiled/config', true, true);
  100. $files = [];
  101. $paths = $locator->findResources('config://');
  102. $files += (new ConfigFileFinder)->locateFiles($paths);
  103. $paths = $locator->findResources('plugins://');
  104. $files += (new ConfigFileFinder)->setBase('plugins')->locateInFolders($paths);
  105. $paths = $locator->findResources('themes://');
  106. $files += (new ConfigFileFinder)->setBase('themes')->locateInFolders($paths);
  107. $compiled = new CompiledConfig($cache, $files, GRAV_ROOT);
  108. $compiled->setBlueprints(function () use ($container) {
  109. return $container['blueprints'];
  110. });
  111. $config = $compiled->name("master-{$setup->environment}")->load();
  112. $config->environment = $setup->environment;
  113. return $config;
  114. }
  115. /**
  116. * @param Container $container
  117. * @return mixed
  118. */
  119. public static function languages(Container $container)
  120. {
  121. /** @var Setup $setup */
  122. $setup = $container['setup'];
  123. /** @var Config $config */
  124. $config = $container['config'];
  125. /** @var UniformResourceLocator $locator */
  126. $locator = $container['locator'];
  127. $cache = $locator->findResource('cache://compiled/languages', true, true);
  128. $files = [];
  129. // Process languages only if enabled in configuration.
  130. if ($config->get('system.languages.translations', true)) {
  131. $paths = $locator->findResources('languages://');
  132. $files += (new ConfigFileFinder)->locateFiles($paths);
  133. $paths = $locator->findResources('plugins://');
  134. $files += (new ConfigFileFinder)->setBase('plugins')->locateInFolders($paths, 'languages');
  135. $paths = static::pluginFolderPaths($paths, 'languages');
  136. $files += (new ConfigFileFinder)->locateFiles($paths);
  137. }
  138. $languages = new CompiledLanguages($cache, $files, GRAV_ROOT);
  139. return $languages->name("master-{$setup->environment}")->load();
  140. }
  141. /**
  142. * Find specific paths in plugins
  143. *
  144. * @param array $plugins
  145. * @param string $folder_path
  146. * @return array
  147. */
  148. protected static function pluginFolderPaths($plugins, $folder_path)
  149. {
  150. $paths = [];
  151. foreach ($plugins as $path) {
  152. $iterator = new DirectoryIterator($path);
  153. /** @var DirectoryIterator $directory */
  154. foreach ($iterator as $directory) {
  155. if (!$directory->isDir() || $directory->isDot()) {
  156. continue;
  157. }
  158. // Path to the languages folder
  159. $lang_path = $directory->getPathName() . '/' . $folder_path;
  160. // If this folder exists, add it to the list of paths
  161. if (file_exists($lang_path)) {
  162. $paths []= $lang_path;
  163. }
  164. }
  165. }
  166. return $paths;
  167. }
  168. }