Configuration.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Manage configuration for the application.
  4. */
  5. class Minz_Configuration {
  6. /**
  7. * The list of configurations.
  8. */
  9. private static $config_list = array();
  10. /**
  11. * Add a new configuration to the list of configuration.
  12. *
  13. * @param $namespace the name of the current configuration
  14. * @param $config_filename the filename of the configuration
  15. * @param $default_filename a filename containing default values for the configuration
  16. * @throws Minz_ConfigurationNamespaceException if the namespace already exists.
  17. */
  18. public static function register($namespace, $config_filename, $default_filename = null) {
  19. if (isset(self::$config_list[$namespace])) {
  20. throw new Minz_ConfigurationNamespaceException(
  21. $namespace . ' namespace already exists'
  22. );
  23. }
  24. self::$config_list[$namespace] = new Minz_Configuration(
  25. $namespace, $config_filename, $default_filename
  26. );
  27. }
  28. /**
  29. * Parse a file and return its data.
  30. *
  31. * If the file does not contain a valid PHP code returning an array, an
  32. * empty array is returned anyway.
  33. *
  34. * @param $filename the name of the file to parse.
  35. * @return an array of values
  36. * @throws Minz_FileNotExistException if the file does not exist.
  37. */
  38. public static function parseFile($filename) {
  39. if (!file_exists($filename)) {
  40. throw new Minz_FileNotExistException($filename);
  41. }
  42. $data = @include($filename);
  43. if (is_array($data)) {
  44. return $data;
  45. } else {
  46. return array();
  47. }
  48. }
  49. /**
  50. * Return the configuration related to a given namespace.
  51. *
  52. * @param $namespace the name of the configuration to get.
  53. * @return a Minz_Configuration object
  54. * @throws Minz_ConfigurationNamespaceException if the namespace does not exist.
  55. */
  56. public static function get($namespace) {
  57. if (!isset(self::$config_list[$namespace])) {
  58. throw new Minz_ConfigurationNamespaceException(
  59. $namespace . ' namespace does not exist'
  60. );
  61. }
  62. return self::$config_list[$namespace];
  63. }
  64. /**
  65. * The namespace of the current configuration.
  66. */
  67. private $namespace = '';
  68. /**
  69. * The filename for the current configuration.
  70. */
  71. private $config_filename = '';
  72. /**
  73. * The filename for the current default values, null by default.
  74. */
  75. private $default_filename = null;
  76. /**
  77. * The configuration values, an empty array by default.
  78. */
  79. private $data = array();
  80. /**
  81. * The default values, an empty array by default.
  82. */
  83. private $data_default = array();
  84. /**
  85. * Create a new Minz_Configuration object.
  86. *
  87. * @param $namespace the name of the current configuration.
  88. * @param $config_filename the file containing configuration values.
  89. * @param $default_filename the file containing default values, null by default.
  90. */
  91. private function __construct($namespace, $config_filename, $default_filename = null) {
  92. $this->namespace = $namespace;
  93. $this->config_filename = $config_filename;
  94. try {
  95. $this->data = self::parseFile($this->config_filename);
  96. } catch (Minz_FileNotExistException $e) {
  97. if (is_null($default_filename)) {
  98. throw $e;
  99. }
  100. }
  101. $this->default_filename = $default_filename;
  102. if (!is_null($this->default_filename)) {
  103. $this->data_default = self::parseFile($this->default_filename);
  104. }
  105. }
  106. /**
  107. * Return the value of the given param.
  108. *
  109. * @param $key the name of the param.
  110. * @param $default default value to return if key does not exist.
  111. * @return the value corresponding to the key.
  112. * @throws Minz_ConfigurationParamException if the param does not exist
  113. */
  114. public function param($key, $default = null) {
  115. if (isset($this->data[$key])) {
  116. return $this->data[$key];
  117. } elseif (!is_null($default)) {
  118. return $default;
  119. } elseif (isset($this->data_default[$key])) {
  120. return $this->data_default[$key];
  121. } else {
  122. throw new Minz_ConfigurationParamException(
  123. $key . ' param does not exist'
  124. );
  125. }
  126. }
  127. /**
  128. * A wrapper for param().
  129. */
  130. public function __get($key) {
  131. return $this->param($key);
  132. }
  133. /**
  134. * Set or remove a param.
  135. *
  136. * @param $key the param name to set.
  137. * @param $value the value to set. If null, the key is removed from the configuration.
  138. */
  139. public function _param($key, $value = null) {
  140. if (isset($this->data[$key]) && is_null($value)) {
  141. unset($this->data[$key]);
  142. } else {
  143. $this->data[$key] = $value;
  144. }
  145. }
  146. /**
  147. * A wrapper for _param().
  148. */
  149. public function __set($key, $value) {
  150. $this->_param($key, $value);
  151. }
  152. /**
  153. * Save the current configuration in the configuration file.
  154. */
  155. public function save() {
  156. $back_filename = $this->config_filename . '.bak.php';
  157. @rename($this->config_filename, $back_filename);
  158. if (file_put_contents($this->config_filename,
  159. "<?php\nreturn " . var_export($this->data, true) . ';',
  160. LOCK_EX) === false) {
  161. return false;
  162. }
  163. // Clear PHP 5.5+ cache for include
  164. if (function_exists('opcache_invalidate')) {
  165. opcache_invalidate($this->config_filename);
  166. }
  167. return true;
  168. }
  169. }