PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/configuration/configuration.php

https://github.com/php-tox/tox
PHP | 270 lines | 128 code | 20 blank | 122 comment | 21 complexity | b075ba3adbcea4f01cfc226652c2d4fc MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Defines the abstract controller of applications.
  4. *
  5. * This file is part of Tox.
  6. *
  7. * Tox is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tox is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tox. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @copyright Š 2012-2013 PHP-Tox.org
  21. * @license GNU General Public License, version 3
  22. */
  23. namespace Tox\Application\Configuration;
  24. use Tox\Core;
  25. use Tox\Application;
  26. /**
  27. * Configuration management for applications.
  28. *
  29. * __*ALIAS*__ as `Tox\Application\Configuration`.
  30. *
  31. * @package tox.application.configuration
  32. * @author Trainxy Ho <trainxy@gmail.com>
  33. * @since 0.1.0-beta1
  34. */
  35. class Configuration extends Core\Assembly implements Application\IConfiguration
  36. {
  37. /**
  38. * Stores global configuration items.
  39. *
  40. * @var array
  41. */
  42. protected $items = array();
  43. /**
  44. * Stores imoprted configuration items.
  45. *
  46. * @var array
  47. */
  48. protected $imported = array();
  49. /**
  50. * Stores loaded configuration items.
  51. *
  52. * @var array
  53. */
  54. protected $loaded = array();
  55. /**
  56. * Stores seted configuration items.
  57. *
  58. * @var array
  59. */
  60. protected $seted = array();
  61. /**
  62. * Stores initial configuration items when constructed.
  63. *
  64. * @var array
  65. */
  66. protected $inited = array();
  67. /**
  68. * {@inheritdoc}
  69. *
  70. * @param string $path Path of php configuration file.
  71. */
  72. public function __construct($path = '')
  73. {
  74. if (!$path) {
  75. return;
  76. }
  77. $s_file = $this->getPath($path);
  78. if ($s_file) {
  79. require_once $s_file;
  80. if (isset($a_array) && is_array($a_array)) {
  81. $this->items = $this->inited = $a_array;
  82. unset($a_array);
  83. } else {
  84. throw new InvalidConfigurationFileException($path);
  85. }
  86. } else {
  87. throw new InvalidConfigurationFileException($path);
  88. }
  89. }
  90. /**
  91. * {@inheritdoc}
  92. *
  93. * @param string $path Path of php configuration file.
  94. * @return void
  95. */
  96. public function import($path)
  97. {
  98. $s_file = $this->getPath($path);
  99. if ($s_file) {
  100. require_once $s_file;
  101. if (isset($a_array) && is_array($a_array)) {
  102. $this->imported[$path] = $a_array;
  103. $this->items = $this->items + $a_array;
  104. unset($a_array);
  105. } else {
  106. throw new InvalidConfigurationFileException($path);
  107. }
  108. } else {
  109. throw new InvalidConfigurationFileException($path);
  110. }
  111. }
  112. /**
  113. * {@inheritdoc}
  114. *
  115. * @param array $items A has array of config
  116. * @return void
  117. */
  118. public function load(array $items)
  119. {
  120. $this->items = array_merge($this->items, $items);
  121. $this->loaded = array_merge($this->loaded, $items);
  122. }
  123. /**
  124. * {@inheritdoc}
  125. *
  126. * @param string $item Key of configuration
  127. * @param mixed $value Value of configuration
  128. * @return void
  129. */
  130. public function set($item, $value)
  131. {
  132. settype($item, 'string');
  133. $a_item = array($item => $value);
  134. $this->items = array_merge($this->items, $a_item);
  135. $this->seted = array_merge($this->seted, $a_item);
  136. }
  137. /**
  138. * {@inheritdoc}
  139. *
  140. * @param string $expr Expr rule to export
  141. * @param mixed $defaults Default value
  142. */
  143. public function export($expr, $defaults = null)
  144. {
  145. settype($expr, 'string');
  146. $s_expr = '@^' . str_replace(array('\\*', '\\\\.+'), array('.+', '\\*'), preg_quote($expr, '@')) . '$@';
  147. $a_ret = array();
  148. reset($this->items);
  149. for ($ii = 0, $jj = count($this->items); $ii < $jj; $ii++) {
  150. list($s_key, $m_value) = each($this->items);
  151. if (preg_match($s_expr, $s_key)) {
  152. $a_ret[$s_key] = $m_value;
  153. }
  154. }
  155. switch (count($a_ret)) {
  156. case 0:
  157. if (null !== $defaults) {
  158. return $defaults;
  159. }
  160. if (false === strpos($s_expr, '.+')) {
  161. return null;
  162. }
  163. break;
  164. case 1:
  165. if (false === strpos($s_expr, '.+')) {
  166. return $a_ret[0];
  167. }
  168. }
  169. return $a_ret;
  170. }
  171. /**
  172. * {@inheritdoc}
  173. *
  174. * @return array
  175. */
  176. public function dump()
  177. {
  178. return array(
  179. 'global' => $this->items,
  180. 'imported' => $this->imported,
  181. 'loaded' => $this->loaded,
  182. 'seted' => $this->seted
  183. );
  184. }
  185. /**
  186. * Be invoked on exploring a key in an array or not.
  187. *
  188. * @param string $offset Key of configuration item.
  189. * @return bool
  190. */
  191. public function offsetExists($offset)
  192. {
  193. settype($offset, 'string');
  194. return array_key_exists($offset, $this->items);
  195. }
  196. /**
  197. * Be invoked on getting a value from array by key.
  198. *
  199. * @param string $offset Key of configuration item.
  200. * @return mixed
  201. */
  202. public function offsetGet($offset)
  203. {
  204. settype($offset, 'string');
  205. if (!$this->offsetExists($offset)) {
  206. return null;
  207. }
  208. return $this->items[$offset];
  209. }
  210. /**
  211. * Be invoked on setting a value from array by key.
  212. *
  213. * @param string $offset Key of configuration item.
  214. * @param mixed $value Value of configuration item.
  215. * @return void
  216. */
  217. public function offsetSet($offset, $value)
  218. {
  219. settype($offset, 'string');
  220. $this->items[$offset] = $value;
  221. }
  222. /**
  223. * Be invoked on clearing a value from array by key.
  224. *
  225. * @param string $offset Key of configuration item.
  226. * @return void
  227. */
  228. public function offsetUnset($offset)
  229. {
  230. settype($offset, 'string');
  231. unset($this->items[$offset]);
  232. }
  233. /**
  234. * Change relative path to physical path, and verify path available or not.
  235. *
  236. * @param string $path Relative path of configuration file.
  237. * @return string|false
  238. */
  239. public function getPath($path)
  240. {
  241. settype($path, 'string');
  242. $s_file = getcwd() . '/' . $path;
  243. if (is_file($s_file) && is_readable($s_file)) {
  244. return $s_file;
  245. } else {
  246. return false;
  247. }
  248. }
  249. }
  250. // vi:ft=php fenc=utf-8 ff=unix ts=4 sts=4 et sw=4 fen fdm=indent fdl=1 tw=120