Configuration.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * La classe Configuration permet de gérer la configuration de l'application
  8. */
  9. class Configuration {
  10. const CONF_PATH_NAME = '/configuration/application.ini';
  11. /**
  12. * VERSION est la version actuelle de MINZ
  13. */
  14. const VERSION = '1.3.1';
  15. /**
  16. * valeurs possibles pour l'"environment"
  17. * SILENT rend l'application muette (pas de log)
  18. * PRODUCTION est recommandée pour une appli en production
  19. * (log les erreurs critiques)
  20. * DEVELOPMENT log toutes les erreurs
  21. */
  22. const SILENT = 0;
  23. const PRODUCTION = 1;
  24. const DEVELOPMENT = 2;
  25. /**
  26. * définition des variables de configuration
  27. * $sel_application une chaîne de caractères aléatoires (obligatoire)
  28. * $environment gère le niveau d'affichage pour log et erreurs
  29. * $use_url_rewriting indique si on utilise l'url_rewriting
  30. * $base_url le chemin de base pour accéder à l'application
  31. * $title le nom de l'application
  32. * $language la langue par défaut de l'application
  33. * $cacheEnabled permet de savoir si le cache doit être activé
  34. * $delayCache la limite de cache
  35. * $db paramètres pour la base de données (tableau)
  36. * - host le serveur de la base
  37. * - user nom d'utilisateur
  38. * - password mot de passe de l'utilisateur
  39. * - base le nom de la base de données
  40. */
  41. private static $sel_application = '';
  42. private static $environment = Configuration::PRODUCTION;
  43. private static $base_url = '';
  44. private static $use_url_rewriting = false;
  45. private static $title = '';
  46. private static $language = 'en';
  47. private static $cache_enabled = false;
  48. private static $delay_cache = 3600;
  49. private static $db = array (
  50. 'host' => false,
  51. 'user' => false,
  52. 'password' => false,
  53. 'base' => false
  54. );
  55. /*
  56. * Getteurs
  57. */
  58. public static function selApplication () {
  59. return self::$sel_application;
  60. }
  61. public static function environment () {
  62. return self::$environment;
  63. }
  64. public static function baseUrl () {
  65. return self::$base_url;
  66. }
  67. public static function useUrlRewriting () {
  68. return self::$use_url_rewriting;
  69. }
  70. public static function title () {
  71. return self::$title;
  72. }
  73. public static function language () {
  74. return self::$language;
  75. }
  76. public static function cacheEnabled () {
  77. return self::$cache_enabled;
  78. }
  79. public static function delayCache () {
  80. return self::$delay_cache;
  81. }
  82. public static function dataBase () {
  83. return self::$db;
  84. }
  85. /**
  86. * Initialise les variables de configuration
  87. * @exception FileNotExistException si le CONF_PATH_NAME n'existe pas
  88. * @exception BadConfigurationException si CONF_PATH_NAME mal formaté
  89. */
  90. public static function init () {
  91. try {
  92. self::parseFile ();
  93. self::setReporting ();
  94. } catch (BadConfigurationException $e) {
  95. throw $e;
  96. } catch (FileNotExistException $e) {
  97. throw $e;
  98. }
  99. }
  100. /**
  101. * Parse un fichier de configuration de type ".ini"
  102. * @exception FileNotExistException si le CONF_PATH_NAME n'existe pas
  103. * @exception BadConfigurationException si CONF_PATH_NAME mal formaté
  104. */
  105. private static function parseFile () {
  106. if (!file_exists (APP_PATH . self::CONF_PATH_NAME)) {
  107. throw new FileNotExistException (
  108. APP_PATH . self::CONF_PATH_NAME,
  109. MinzException::ERROR
  110. );
  111. }
  112. $ini_array = parse_ini_file (
  113. APP_PATH . self::CONF_PATH_NAME,
  114. true
  115. );
  116. // [general] est obligatoire
  117. if (!isset ($ini_array['general'])) {
  118. throw new BadConfigurationException (
  119. '[general]',
  120. MinzException::ERROR
  121. );
  122. }
  123. $general = $ini_array['general'];
  124. // sel_application est obligatoire
  125. if (!isset ($general['sel_application'])) {
  126. throw new BadConfigurationException (
  127. 'sel_application',
  128. MinzException::ERROR
  129. );
  130. }
  131. self::$sel_application = $general['sel_application'];
  132. if (isset ($general['environment'])) {
  133. switch ($general['environment']) {
  134. case 'silent':
  135. self::$environment = Configuration::SILENT;
  136. break;
  137. case 'development':
  138. self::$environment = Configuration::DEVELOPMENT;
  139. break;
  140. case 'production':
  141. self::$environment = Configuration::PRODUCTION;
  142. break;
  143. default:
  144. throw new BadConfigurationException (
  145. 'environment',
  146. MinzException::ERROR
  147. );
  148. }
  149. }
  150. if (isset ($general['base_url'])) {
  151. self::$base_url = $general['base_url'];
  152. }
  153. if (isset ($general['use_url_rewriting'])) {
  154. self::$use_url_rewriting = $general['use_url_rewriting'];
  155. }
  156. if (isset ($general['title'])) {
  157. self::$title = $general['title'];
  158. }
  159. if (isset ($general['language'])) {
  160. self::$language = $general['language'];
  161. }
  162. if (isset ($general['cache_enabled'])) {
  163. self::$cache_enabled = $general['cache_enabled'];
  164. if (CACHE_PATH === false && self::$cache_enabled) {
  165. throw new FileNotExistException (
  166. 'CACHE_PATH',
  167. MinzException::ERROR
  168. );
  169. }
  170. }
  171. if (isset ($general['delay_cache'])) {
  172. self::$delay_cache = $general['delay_cache'];
  173. }
  174. // Base de données
  175. $db = false;
  176. if (isset ($ini_array['db'])) {
  177. $db = $ini_array['db'];
  178. }
  179. if ($db) {
  180. if (!isset ($db['host'])) {
  181. throw new BadConfigurationException (
  182. 'host',
  183. MinzException::ERROR
  184. );
  185. }
  186. if (!isset ($db['user'])) {
  187. throw new BadConfigurationException (
  188. 'user',
  189. MinzException::ERROR
  190. );
  191. }
  192. if (!isset ($db['password'])) {
  193. throw new BadConfigurationException (
  194. 'password',
  195. MinzException::ERROR
  196. );
  197. }
  198. if (!isset ($db['base'])) {
  199. throw new BadConfigurationException (
  200. 'base',
  201. MinzException::ERROR
  202. );
  203. }
  204. self::$db['host'] = $db['host'];
  205. self::$db['user'] = $db['user'];
  206. self::$db['password'] = $db['password'];
  207. self::$db['base'] = $db['base'];
  208. }
  209. }
  210. private static function setReporting () {
  211. if (self::environment () == self::DEVELOPMENT) {
  212. error_reporting (E_ALL);
  213. ini_set ('display_errors','On');
  214. ini_set('log_errors', 'On');
  215. } elseif (self::environment () == self::PRODUCTION) {
  216. error_reporting(E_ALL);
  217. ini_set('display_errors','Off');
  218. ini_set('log_errors', 'On');
  219. } else {
  220. error_reporting(0);
  221. }
  222. }
  223. }