PageRenderTime 68ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/core/classes/config/file.php

https://bitbucket.org/codeyash/bootstrap
PHP | 207 lines | 139 code | 20 blank | 48 comment | 8 complexity | 88cf78a98a7c1c1edcf8d9b4d0edb2e1 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. <?php
  2. namespace Fuel\Core;
  3. /**
  4. * A base Config File class for File based configs.
  5. */
  6. abstract class Config_File implements Config_Interface
  7. {
  8. protected $file;
  9. protected $vars = array();
  10. /**
  11. * Sets up the file to be parsed and variables
  12. *
  13. * @param string $file Config file name
  14. * @param array $vars Variables to parse in the file
  15. * @return void
  16. */
  17. public function __construct($file = null, $vars = array())
  18. {
  19. $this->file = $file;
  20. $this->vars = array(
  21. 'APPPATH' => APPPATH,
  22. 'COREPATH' => COREPATH,
  23. 'PKGPATH' => PKGPATH,
  24. 'DOCROOT' => DOCROOT,
  25. ) + $vars;
  26. }
  27. /**
  28. * Loads the config file(s).
  29. *
  30. * @param bool $overwrite Whether to overwrite existing values
  31. * @return array the config array
  32. */
  33. public function load($overwrite = false, $cache = true)
  34. {
  35. $paths = $this->find_file($cache);
  36. $config = array();
  37. foreach ($paths as $path)
  38. {
  39. $config = $overwrite ?
  40. array_merge($config, $this->load_file($path)) :
  41. \Arr::merge($config, $this->load_file($path));
  42. }
  43. return $config;
  44. }
  45. /**
  46. * Gets the default group name.
  47. *
  48. * @return string
  49. */
  50. public function group()
  51. {
  52. return $this->file;
  53. }
  54. /**
  55. * Parses a string using all of the previously set variables. Allows you to
  56. * use something like %APPPATH% in non-PHP files.
  57. *
  58. * @param string $string String to parse
  59. * @return string
  60. */
  61. protected function parse_vars($string)
  62. {
  63. foreach ($this->vars as $var => $val)
  64. {
  65. $string = str_replace("%$var%", $val, $string);
  66. }
  67. return $string;
  68. }
  69. /**
  70. * Replaces FuelPHP's path constants to their string counterparts.
  71. *
  72. * @param array $array array to be prepped
  73. * @return array prepped array
  74. */
  75. protected function prep_vars(&$array)
  76. {
  77. static $replacements = false;
  78. if ($replacements === false)
  79. {
  80. foreach ($this->vars as $i => $v)
  81. {
  82. $replacements['#^('.preg_quote($v).'){1}(.*)?#'] = "%".$i."%$2";
  83. }
  84. }
  85. foreach ($array as $i => $value)
  86. {
  87. if (is_string($value))
  88. {
  89. $array[$i] = preg_replace(array_keys($replacements), array_values($replacements), $value);
  90. }
  91. elseif(is_array($value))
  92. {
  93. $this->prep_vars($array[$i]);
  94. }
  95. }
  96. }
  97. /**
  98. * Finds the given config files
  99. *
  100. * @param bool $multiple Whether to load multiple files or not
  101. * @return array
  102. */
  103. protected function find_file($cache = true)
  104. {
  105. if (($this->file[0] === '/' or (isset($this->file[1]) and $this->file[1] === ':')) and is_file($this->file))
  106. {
  107. $paths = array($this->file);
  108. }
  109. else
  110. {
  111. $paths = array_merge(
  112. \Finder::search('config/'.\Fuel::$env, $this->file, $this->ext, true, $cache),
  113. \Finder::search('config', $this->file, $this->ext, true, $cache)
  114. );
  115. }
  116. if (empty($paths))
  117. {
  118. throw new \ConfigException(sprintf('File "%s" does not exist.', $this->file));
  119. }
  120. return array_reverse($paths);
  121. }
  122. /**
  123. * Formats the output and saved it to disc.
  124. *
  125. * @param $contents $contents config array to save
  126. * @return bool \File::update result
  127. */
  128. public function save($contents)
  129. {
  130. // get the formatted output
  131. $output = $this->export_format($contents);
  132. if ( ! $output)
  133. {
  134. return false;
  135. }
  136. if ( ! $path = \Finder::search('config', $this->file, $this->ext))
  137. {
  138. if ($pos = strripos($this->file, '::'))
  139. {
  140. // get the namespace path
  141. if ($path = \Autoloader::namespace_path('\\'.ucfirst(substr($this->file, 0, $pos))))
  142. {
  143. // strip the namespace from the filename
  144. $this->file = substr($this->file, $pos+2);
  145. // strip the classes directory as we need the module root
  146. $path = substr($path,0, -8).'config'.DS.$this->file;
  147. }
  148. else
  149. {
  150. // invalid namespace requested
  151. return false;
  152. }
  153. }
  154. }
  155. // absolute path requested?
  156. if ($this->file[0] === '/' or (isset($this->file[1]) and $this->file[1] === ':'))
  157. {
  158. $path = $this->file;
  159. }
  160. // make sure we have a fallback
  161. $path or $path = APPPATH.'config'.DS.$this->file.$this->ext;
  162. $path = pathinfo($path);
  163. if ( ! is_dir($path['dirname']))
  164. {
  165. mkdir($path['dirname'], 0777, true);
  166. }
  167. return \File::update($path['dirname'], $path['basename'], $output);
  168. }
  169. /**
  170. * Must be implemented by child class. Gets called for each file to load.
  171. */
  172. abstract protected function load_file($file);
  173. /**
  174. * Must be impletmented by child class. Gets called when saving a config file.
  175. *
  176. * @param array $contents config array to save
  177. * @return string formatted output
  178. */
  179. abstract protected function export_format($contents);
  180. }