ConfigServiceProvider.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @package Grav\Common\Service
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Service;
  9. use Grav\Common\Config\CompiledBlueprints;
  10. use Grav\Common\Config\CompiledConfig;
  11. use Grav\Common\Config\CompiledLanguages;
  12. use Grav\Common\Config\Config;
  13. use Grav\Common\Config\ConfigFileFinder;
  14. use Grav\Common\Config\Setup;
  15. use Grav\Common\Language\Language;
  16. use Pimple\Container;
  17. use Pimple\ServiceProviderInterface;
  18. use RocketTheme\Toolbox\File\YamlFile;
  19. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  20. class ConfigServiceProvider implements ServiceProviderInterface
  21. {
  22. public function register(Container $container)
  23. {
  24. $container['setup'] = function ($c) {
  25. $setup = new Setup($c);
  26. $setup->init();
  27. return $setup;
  28. };
  29. $container['blueprints'] = function ($c) {
  30. return static::blueprints($c);
  31. };
  32. $container['config'] = function ($c) {
  33. $config = static::load($c);
  34. // After configuration has been loaded, we can disable YAML compatibility if strict mode has been enabled.
  35. if (!$config->get('system.strict_mode.yaml_compat', true)) {
  36. YamlFile::globalSettings(['compat' => false, 'native' => true]);
  37. }
  38. return $config;
  39. };
  40. $container['languages'] = function ($c) {
  41. return static::languages($c);
  42. };
  43. $container['language'] = function ($c) {
  44. return new Language($c);
  45. };
  46. }
  47. public static function blueprints(Container $container)
  48. {
  49. /** Setup $setup */
  50. $setup = $container['setup'];
  51. /** @var UniformResourceLocator $locator */
  52. $locator = $container['locator'];
  53. $cache = $locator->findResource('cache://compiled/blueprints', true, true);
  54. $files = [];
  55. $paths = $locator->findResources('blueprints://config');
  56. $files += (new ConfigFileFinder)->locateFiles($paths);
  57. $paths = $locator->findResources('plugins://');
  58. $files += (new ConfigFileFinder)->setBase('plugins')->locateInFolders($paths, 'blueprints');
  59. $blueprints = new CompiledBlueprints($cache, $files, GRAV_ROOT);
  60. return $blueprints->name("master-{$setup->environment}")->load();
  61. }
  62. /**
  63. * @param Container $container
  64. * @return Config
  65. */
  66. public static function load(Container $container)
  67. {
  68. /** Setup $setup */
  69. $setup = $container['setup'];
  70. /** @var UniformResourceLocator $locator */
  71. $locator = $container['locator'];
  72. $cache = $locator->findResource('cache://compiled/config', true, true);
  73. $files = [];
  74. $paths = $locator->findResources('config://');
  75. $files += (new ConfigFileFinder)->locateFiles($paths);
  76. $paths = $locator->findResources('plugins://');
  77. $files += (new ConfigFileFinder)->setBase('plugins')->locateInFolders($paths);
  78. $compiled = new CompiledConfig($cache, $files, GRAV_ROOT);
  79. $compiled->setBlueprints(function() use ($container) {
  80. return $container['blueprints'];
  81. });
  82. $config = $compiled->name("master-{$setup->environment}")->load();
  83. $config->environment = $setup->environment;
  84. return $config;
  85. }
  86. public static function languages(Container $container)
  87. {
  88. /** @var Setup $setup */
  89. $setup = $container['setup'];
  90. /** @var Config $config */
  91. $config = $container['config'];
  92. /** @var UniformResourceLocator $locator */
  93. $locator = $container['locator'];
  94. $cache = $locator->findResource('cache://compiled/languages', true, true);
  95. $files = [];
  96. // Process languages only if enabled in configuration.
  97. if ($config->get('system.languages.translations', true)) {
  98. $paths = $locator->findResources('languages://');
  99. $files += (new ConfigFileFinder)->locateFiles($paths);
  100. $paths = $locator->findResources('plugins://');
  101. $files += (new ConfigFileFinder)->setBase('plugins')->locateInFolders($paths, 'languages');
  102. $paths = static::pluginFolderPaths($paths, 'languages');
  103. $files += (new ConfigFileFinder)->locateFiles($paths);
  104. }
  105. $languages = new CompiledLanguages($cache, $files, GRAV_ROOT);
  106. return $languages->name("master-{$setup->environment}")->load();
  107. }
  108. /**
  109. * Find specific paths in plugins
  110. *
  111. * @param array $plugins
  112. * @param string $folder_path
  113. * @return array
  114. */
  115. private static function pluginFolderPaths($plugins, $folder_path)
  116. {
  117. $paths = [];
  118. foreach ($plugins as $path) {
  119. $iterator = new \DirectoryIterator($path);
  120. /** @var \DirectoryIterator $directory */
  121. foreach ($iterator as $directory) {
  122. if (!$directory->isDir() || $directory->isDot()) {
  123. continue;
  124. }
  125. // Path to the languages folder
  126. $lang_path = $directory->getPathName() . '/' . $folder_path;
  127. // If this folder exists, add it to the list of paths
  128. if (file_exists($lang_path)) {
  129. $paths []= $lang_path;
  130. }
  131. }
  132. }
  133. return $paths;
  134. }
  135. }