PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Config/FileLoader.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 259 lines | 103 code | 36 blank | 120 comment | 8 complexity | d3548c89c58db21293379ab3ba3c78bc MD5 | raw file
  1. <?php namespace Illuminate\Config;
  2. use Illuminate\Filesystem\Filesystem;
  3. class FileLoader implements LoaderInterface {
  4. /**
  5. * The filesystem instance.
  6. *
  7. * @var \Illuminate\Filesystem\Filesystem
  8. */
  9. protected $files;
  10. /**
  11. * The default configuration path.
  12. *
  13. * @var string
  14. */
  15. protected $defaultPath;
  16. /**
  17. * All of the named path hints.
  18. *
  19. * @var array
  20. */
  21. protected $hints = array();
  22. /**
  23. * A cache of whether namespaces and groups exists.
  24. *
  25. * @var array
  26. */
  27. protected $exists = array();
  28. /**
  29. * Create a new file configuration loader.
  30. *
  31. * @param \Illuminate\Filesystem\Filesystem $files
  32. * @param string $defaultPath
  33. * @return void
  34. */
  35. public function __construct(Filesystem $files, $defaultPath)
  36. {
  37. $this->files = $files;
  38. $this->defaultPath = $defaultPath;
  39. }
  40. /**
  41. * Load the given configuration group.
  42. *
  43. * @param string $environment
  44. * @param string $group
  45. * @param string $namespace
  46. * @return array
  47. */
  48. public function load($environment, $group, $namespace = null)
  49. {
  50. $items = array();
  51. // First we'll get the root configuration path for the environment which is
  52. // where all of the configuration files live for that namespace, as well
  53. // as any environment folders with their specific configuration items.
  54. $path = $this->getPath($namespace);
  55. if (is_null($path))
  56. {
  57. return $items;
  58. }
  59. // First we'll get the main configuration file for the groups. Once we have
  60. // that we can check for any environment specific files, which will get
  61. // merged on top of the main arrays to make the environments cascade.
  62. $file = "{$path}/{$group}.php";
  63. if ($this->files->exists($file))
  64. {
  65. $items = $this->getRequire($file);
  66. }
  67. // Finally we're ready to check for the environment specific configuration
  68. // file which will be merged on top of the main arrays so that they get
  69. // precedence over them if we are currently in an environments setup.
  70. $file = "{$path}/{$environment}/{$group}.php";
  71. if ($this->files->exists($file))
  72. {
  73. $items = $this->mergeEnvironment($items, $file);
  74. }
  75. return $items;
  76. }
  77. /**
  78. * Merge the items in the given file into the items.
  79. *
  80. * @param array $items
  81. * @param string $file
  82. * @return array
  83. */
  84. protected function mergeEnvironment(array $items, $file)
  85. {
  86. return array_replace_recursive($items, $this->getRequire($file));
  87. }
  88. /**
  89. * Determine if the given group exists.
  90. *
  91. * @param string $group
  92. * @param string $namespace
  93. * @return bool
  94. */
  95. public function exists($group, $namespace = null)
  96. {
  97. $key = $group.$namespace;
  98. // We'll first check to see if we have determined if this namespace and
  99. // group combination have been checked before. If they have, we will
  100. // just return the cached result so we don't have to hit the disk.
  101. if (isset($this->exists[$key]))
  102. {
  103. return $this->exists[$key];
  104. }
  105. $path = $this->getPath($namespace);
  106. // To check if a group exists, we will simply get the path based on the
  107. // namespace, and then check to see if this files exists within that
  108. // namespace. False is returned if no path exists for a namespace.
  109. if (is_null($path))
  110. {
  111. return $this->exists[$key] = false;
  112. }
  113. $file = "{$path}/{$group}.php";
  114. // Finally, we can simply check if this file exists. We will also cache
  115. // the value in an array so we don't have to go through this process
  116. // again on subsequent checks for the existing of the config file.
  117. $exists = $this->files->exists($file);
  118. return $this->exists[$key] = $exists;
  119. }
  120. /**
  121. * Apply any cascades to an array of package options.
  122. *
  123. * @param string $env
  124. * @param string $package
  125. * @param string $group
  126. * @param array $items
  127. * @return array
  128. */
  129. public function cascadePackage($env, $package, $group, $items)
  130. {
  131. // First we will look for a configuration file in the packages configuration
  132. // folder. If it exists, we will load it and merge it with these original
  133. // options so that we will easily "cascade" a package's configurations.
  134. $file = "packages/{$package}/{$group}.php";
  135. if ($this->files->exists($path = $this->defaultPath.'/'.$file))
  136. {
  137. $items = array_merge(
  138. $items, $this->getRequire($path)
  139. );
  140. }
  141. // Once we have merged the regular package configuration we need to look for
  142. // an environment specific configuration file. If one exists, we will get
  143. // the contents and merge them on top of this array of options we have.
  144. $path = $this->getPackagePath($env, $package, $group);
  145. if ($this->files->exists($path))
  146. {
  147. $items = array_merge(
  148. $items, $this->getRequire($path)
  149. );
  150. }
  151. return $items;
  152. }
  153. /**
  154. * Get the package path for an environment and group.
  155. *
  156. * @param string $env
  157. * @param string $package
  158. * @param string $group
  159. * @return string
  160. */
  161. protected function getPackagePath($env, $package, $group)
  162. {
  163. $file = "packages/{$package}/{$env}/{$group}.php";
  164. return $this->defaultPath.'/'.$file;
  165. }
  166. /**
  167. * Get the configuration path for a namespace.
  168. *
  169. * @param string $namespace
  170. * @return string
  171. */
  172. protected function getPath($namespace)
  173. {
  174. if (is_null($namespace))
  175. {
  176. return $this->defaultPath;
  177. }
  178. elseif (isset($this->hints[$namespace]))
  179. {
  180. return $this->hints[$namespace];
  181. }
  182. }
  183. /**
  184. * Add a new namespace to the loader.
  185. *
  186. * @param string $namespace
  187. * @param string $hint
  188. * @return void
  189. */
  190. public function addNamespace($namespace, $hint)
  191. {
  192. $this->hints[$namespace] = $hint;
  193. }
  194. /**
  195. * Returns all registered namespaces with the config
  196. * loader.
  197. *
  198. * @return array
  199. */
  200. public function getNamespaces()
  201. {
  202. return $this->hints;
  203. }
  204. /**
  205. * Get a file's contents by requiring it.
  206. *
  207. * @param string $path
  208. * @return mixed
  209. */
  210. protected function getRequire($path)
  211. {
  212. return $this->files->getRequire($path);
  213. }
  214. /**
  215. * Get the Filesystem instance.
  216. *
  217. * @return \Illuminate\Filesystem\Filesystem
  218. */
  219. public function getFilesystem()
  220. {
  221. return $this->files;
  222. }
  223. }