Setup.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * @package Grav.Common.Config
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Config;
  9. use Grav\Common\File\CompiledYamlFile;
  10. use Grav\Common\Data\Data;
  11. use Grav\Common\Utils;
  12. use Pimple\Container;
  13. use RocketTheme\Toolbox\File\YamlFile;
  14. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  15. class Setup extends Data
  16. {
  17. public static $environment;
  18. protected $streams = [
  19. 'system' => [
  20. 'type' => 'ReadOnlyStream',
  21. 'prefixes' => [
  22. '' => ['system'],
  23. ]
  24. ],
  25. 'user' => [
  26. 'type' => 'ReadOnlyStream',
  27. 'force' => true,
  28. 'prefixes' => [
  29. '' => ['user'],
  30. ]
  31. ],
  32. 'environment' => [
  33. 'type' => 'ReadOnlyStream'
  34. // If not defined, environment will be set up in the constructor.
  35. ],
  36. 'asset' => [
  37. 'type' => 'ReadOnlyStream',
  38. 'prefixes' => [
  39. '' => ['assets'],
  40. ]
  41. ],
  42. 'blueprints' => [
  43. 'type' => 'ReadOnlyStream',
  44. 'prefixes' => [
  45. '' => ['environment://blueprints', 'user://blueprints', 'system/blueprints'],
  46. ]
  47. ],
  48. 'config' => [
  49. 'type' => 'ReadOnlyStream',
  50. 'prefixes' => [
  51. '' => ['environment://config', 'user://config', 'system/config'],
  52. ]
  53. ],
  54. 'plugins' => [
  55. 'type' => 'ReadOnlyStream',
  56. 'prefixes' => [
  57. '' => ['user://plugins'],
  58. ]
  59. ],
  60. 'plugin' => [
  61. 'type' => 'ReadOnlyStream',
  62. 'prefixes' => [
  63. '' => ['user://plugins'],
  64. ]
  65. ],
  66. 'themes' => [
  67. 'type' => 'ReadOnlyStream',
  68. 'prefixes' => [
  69. '' => ['user://themes'],
  70. ]
  71. ],
  72. 'languages' => [
  73. 'type' => 'ReadOnlyStream',
  74. 'prefixes' => [
  75. '' => ['environment://languages', 'user://languages', 'system/languages'],
  76. ]
  77. ],
  78. 'cache' => [
  79. 'type' => 'Stream',
  80. 'force' => true,
  81. 'prefixes' => [
  82. '' => ['cache'],
  83. 'images' => ['images']
  84. ]
  85. ],
  86. 'log' => [
  87. 'type' => 'Stream',
  88. 'force' => true,
  89. 'prefixes' => [
  90. '' => ['logs']
  91. ]
  92. ],
  93. 'backup' => [
  94. 'type' => 'Stream',
  95. 'force' => true,
  96. 'prefixes' => [
  97. '' => ['backup']
  98. ]
  99. ],
  100. 'tmp' => [
  101. 'type' => 'Stream',
  102. 'force' => true,
  103. 'prefixes' => [
  104. '' => ['tmp']
  105. ]
  106. ],
  107. 'image' => [
  108. 'type' => 'ReadOnlyStream',
  109. 'prefixes' => [
  110. '' => ['user://images', 'system://images']
  111. ]
  112. ],
  113. 'page' => [
  114. 'type' => 'ReadOnlyStream',
  115. 'prefixes' => [
  116. '' => ['user://pages']
  117. ]
  118. ],
  119. 'account' => [
  120. 'type' => 'ReadOnlyStream',
  121. 'prefixes' => [
  122. '' => ['user://accounts']
  123. ]
  124. ],
  125. ];
  126. /**
  127. * @param Container|array $container
  128. */
  129. public function __construct($container)
  130. {
  131. $environment = null !== static::$environment ? static::$environment : ($container['uri']->environment() ?: 'localhost');
  132. // Pre-load setup.php which contains our initial configuration.
  133. // Configuration may contain dynamic parts, which is why we need to always load it.
  134. // If "GRAVE_SETUP_PATH" has been defined, use it, otherwise use defaults.
  135. $file = defined('GRAV_SETUP_PATH') ? GRAV_SETUP_PATH : GRAV_ROOT . '/setup.php';
  136. $setup = is_file($file) ? (array) include $file : [];
  137. // Add default streams defined in beginning of the class.
  138. if (!isset($setup['streams']['schemes'])) {
  139. $setup['streams']['schemes'] = [];
  140. }
  141. $setup['streams']['schemes'] += $this->streams;
  142. // Initialize class.
  143. parent::__construct($setup);
  144. // Set up environment.
  145. $this->def('environment', $environment ?: 'cli');
  146. $this->def('streams.schemes.environment.prefixes', ['' => $environment ? ["user://{$this->environment}"] : []]);
  147. }
  148. /**
  149. * @return $this
  150. * @throws \RuntimeException
  151. * @throws \InvalidArgumentException
  152. */
  153. public function init()
  154. {
  155. $locator = new UniformResourceLocator(GRAV_ROOT);
  156. $files = [];
  157. $guard = 5;
  158. do {
  159. $check = $files;
  160. $this->initializeLocator($locator);
  161. $files = $locator->findResources('config://streams.yaml');
  162. if ($check === $files) {
  163. break;
  164. }
  165. // Update streams.
  166. foreach (array_reverse($files) as $path) {
  167. $file = CompiledYamlFile::instance($path);
  168. $content = (array)$file->content();
  169. if (!empty($content['schemes'])) {
  170. $this->items['streams']['schemes'] = $content['schemes'] + $this->items['streams']['schemes'];
  171. }
  172. }
  173. } while (--$guard);
  174. if (!$guard) {
  175. throw new \RuntimeException('Setup: Configuration reload loop detected!');
  176. }
  177. // Make sure we have valid setup.
  178. $this->check($locator);
  179. return $this;
  180. }
  181. /**
  182. * Initialize resource locator by using the configuration.
  183. *
  184. * @param UniformResourceLocator $locator
  185. * @throws \BadMethodCallException
  186. */
  187. public function initializeLocator(UniformResourceLocator $locator)
  188. {
  189. $locator->reset();
  190. $schemes = (array) $this->get('streams.schemes', []);
  191. foreach ($schemes as $scheme => $config) {
  192. if (isset($config['paths'])) {
  193. $locator->addPath($scheme, '', $config['paths']);
  194. }
  195. $override = isset($config['override']) ? $config['override'] : false;
  196. $force = isset($config['force']) ? $config['force'] : false;
  197. if (isset($config['prefixes'])) {
  198. foreach ((array)$config['prefixes'] as $prefix => $paths) {
  199. $locator->addPath($scheme, $prefix, $paths, $override, $force);
  200. }
  201. }
  202. }
  203. }
  204. /**
  205. * Get available streams and their types from the configuration.
  206. *
  207. * @return array
  208. */
  209. public function getStreams()
  210. {
  211. $schemes = [];
  212. foreach ((array) $this->get('streams.schemes') as $scheme => $config) {
  213. $type = !empty($config['type']) ? $config['type'] : 'ReadOnlyStream';
  214. if ($type[0] !== '\\') {
  215. $type = '\\RocketTheme\\Toolbox\\StreamWrapper\\' . $type;
  216. }
  217. $schemes[$scheme] = $type;
  218. }
  219. return $schemes;
  220. }
  221. /**
  222. * @param UniformResourceLocator $locator
  223. * @throws \InvalidArgumentException
  224. * @throws \BadMethodCallException
  225. * @throws \RuntimeException
  226. */
  227. protected function check(UniformResourceLocator $locator)
  228. {
  229. $streams = isset($this->items['streams']['schemes']) ? $this->items['streams']['schemes'] : null;
  230. if (!is_array($streams)) {
  231. throw new \InvalidArgumentException('Configuration is missing streams.schemes!');
  232. }
  233. $diff = array_keys(array_diff_key($this->streams, $streams));
  234. if ($diff) {
  235. throw new \InvalidArgumentException(
  236. sprintf('Configuration is missing keys %s from streams.schemes!', implode(', ', $diff))
  237. );
  238. }
  239. try {
  240. if (!$locator->findResource('environment://config', true)) {
  241. // If environment does not have its own directory, remove it from the lookup.
  242. $this->set('streams.schemes.environment.prefixes', ['config' => []]);
  243. $this->initializeLocator($locator);
  244. }
  245. // Create security.yaml if it doesn't exist.
  246. $filename = $locator->findResource('config://security.yaml', true, true);
  247. $file = YamlFile::instance($filename);
  248. if (!$file->exists()) {
  249. $file->save(['salt' => Utils::generateRandomString(14)]);
  250. $file->free();
  251. }
  252. } catch (\RuntimeException $e) {
  253. throw new \RuntimeException(sprintf('Grav failed to initialize: %s', $e->getMessage()), 500, $e);
  254. }
  255. }
  256. }