PageRenderTime 94ms CodeModel.GetById 36ms RepoModel.GetById 14ms app.codeStats 0ms

/lib/Jelix/Core/App.php

https://github.com/gmarrot/jelix
PHP | 321 lines | 174 code | 42 blank | 105 comment | 23 complexity | 0bb3a7b110ef2dc5f68b2cba44bf6963 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @author Laurent Jouanneau
  4. * @contributor Olivier Demah
  5. * @copyright 2011-2014 Laurent Jouanneau, 2012 Olivier Demah
  6. * @link http://jelix.org
  7. * @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  8. */
  9. namespace Jelix\Core;
  10. /**
  11. *
  12. */
  13. class App {
  14. protected static $tempBasePath = '';
  15. protected static $appPath = '';
  16. protected static $varPath = '';
  17. protected static $logPath = '';
  18. protected static $configPath = '';
  19. protected static $wwwPath = '';
  20. protected static $scriptPath = '';
  21. protected static $_isInit = false;
  22. protected static $env = 'www/';
  23. protected static $configAutoloader = null;
  24. /**
  25. * initialize the application paths
  26. *
  27. * Warning: given paths should be ended by a directory separator.
  28. * @param string $appPath application directory
  29. * @param string $wwwPath www directory
  30. * @param string $varPath var directory
  31. * @param string $logPath log directory
  32. * @param string $configPath config directory
  33. * @param string $scriptPath scripts directory
  34. */
  35. public static function initPaths ($appPath,
  36. $wwwPath = null,
  37. $varPath = null,
  38. $logPath = null,
  39. $configPath = null,
  40. $scriptPath = null
  41. ) {
  42. self::$appPath = $appPath;
  43. self::$wwwPath = (is_null($wwwPath)?$appPath.'www/':$wwwPath);
  44. self::$varPath = (is_null($varPath)?$appPath.'var/':$varPath);
  45. self::$logPath = (is_null($logPath)?self::$varPath.'log/':$logPath);
  46. self::$configPath = (is_null($configPath)?self::$varPath.'config/':$configPath);
  47. self::$scriptPath = (is_null($scriptPath)?$appPath.'scripts/':$scriptPath);
  48. self::$_isInit = true;
  49. self::$_router = null;
  50. self::$_config = null;
  51. self::$configAutoloader = null;
  52. self::$_mainConfigFile = null;
  53. }
  54. /**
  55. * indicate if path have been set
  56. * @return boolean true if it is ok
  57. */
  58. public static function isInit() { return self::$_isInit; }
  59. public static function appPath($file='') { return self::$appPath.$file; }
  60. public static function varPath($file='') { return self::$varPath.$file; }
  61. public static function logPath($file='') { return self::$logPath.$file; }
  62. public static function configPath($file='') { return self::$configPath.$file; }
  63. public static function wwwPath($file='') { return self::$wwwPath.$file; }
  64. public static function scriptsPath($file='') { return self::$scriptPath.$file; }
  65. public static function tempPath($file='') { return self::$tempBasePath.self::$env.$file; }
  66. public static function tempBasePath() { return self::$tempBasePath; }
  67. public static function setTempBasePath($path) {
  68. self::$tempBasePath = $path;
  69. }
  70. public static function setEnv($env) {
  71. if (substr($env,-1) != '/')
  72. $env.='/';
  73. self::$env = $env;
  74. }
  75. /**
  76. * @var object object containing all configuration options of the application
  77. */
  78. protected static $_config = null;
  79. /**
  80. * @return object object containing all configuration options of the application
  81. */
  82. public static function config() {
  83. return self::$_config;
  84. }
  85. public static function setConfig($config) {
  86. if (self::$configAutoloader) {
  87. spl_autoload_unregister(array(self::$configAutoloader, 'loadClass'));
  88. self::$configAutoloader = null;
  89. }
  90. self::$_config = $config;
  91. if ($config) {
  92. date_default_timezone_set(self::$_config->timeZone);
  93. self::$configAutoloader = new Config\Autoloader($config);
  94. spl_autoload_register(array(self::$configAutoloader, 'loadClass'));
  95. foreach(self::$_config->_autoload_autoloader as $autoloader)
  96. require_once($autoloader);
  97. }
  98. }
  99. /**
  100. * Load the configuration from the given file.
  101. *
  102. * Call it after initPaths
  103. * @param string|object $configFile name of the ini file to configure the framework or a configuration object
  104. * @param boolean $enableErrorHandler enable the error handler of jelix.
  105. * keep it to true, unless you have something to debug
  106. * and really have to use the default handler or an other handler
  107. */
  108. public static function loadConfig ($configFile, $enableErrorHandler=true) {
  109. if ($enableErrorHandler) {
  110. \jBasicErrorHandler::register();
  111. }
  112. if (is_object($configFile))
  113. self::setConfig($configFile);
  114. else
  115. self::setConfig(Config::load($configFile));
  116. self::$_config->enableErrorHandler = $enableErrorHandler;
  117. }
  118. protected static $_mainConfigFile = null;
  119. /**
  120. * Main config file path
  121. */
  122. public static function mainConfigFile() {
  123. if (self::$_mainConfigFile)
  124. return self::$_mainConfigFile;
  125. $configFileName = self::configPath('mainconfig.ini.php');
  126. if (!file_exists ($configFileName) ) {
  127. // support of legacy configuration file
  128. // TODO: support of defaultconfig.ini.php should be dropped in version > 1.6
  129. $configFileName = self::configPath('defaultconfig.ini.php');
  130. trigger_error("the config file defaultconfig.ini.php is deprecated and will be removed in the next major release", E_USER_DEPRECATED);
  131. }
  132. self::$_mainConfigFile = $configFileName;
  133. return $configFileName;
  134. }
  135. /**
  136. * The current router
  137. * @var \Jelix\Routing\Router
  138. */
  139. protected static $_router = null;
  140. /**
  141. * @return \Jelix\Routing\Router current router
  142. */
  143. public static function router() {
  144. return self::$_router;
  145. }
  146. /**
  147. * @param \Jelix\Routing\Router $router set new current router
  148. */
  149. public static function setRouter($router) {
  150. self::$_router = $router;
  151. }
  152. /**
  153. * @deprecated
  154. */
  155. public static function coord() {
  156. //trigger_error("App::coord() is deprecated, use App::router() instead", E_USER_DEPRECATED);
  157. return self::$_router;
  158. }
  159. /**
  160. * @deprecated
  161. */
  162. public static function setCoord($router) {
  163. //trigger_error("App::setCoord() is deprecated, use App::setRouter() instead", E_USER_DEPRECATED);
  164. self::$_router = $router;
  165. }
  166. protected static $contextBackup = array();
  167. /**
  168. * save all path and others variables relatives to the application, so you can
  169. * temporary change the context to an other application
  170. */
  171. public static function saveContext() {
  172. if (self::$_config)
  173. $conf = clone self::$_config;
  174. else
  175. $conf = null;
  176. if (self::$_router)
  177. $router = clone self::$_router;
  178. else
  179. $router = null;
  180. self::$contextBackup[] = array(self::$appPath, self::$varPath, self::$logPath,
  181. self::$configPath, self::$wwwPath, self::$scriptPath,
  182. self::$tempBasePath, self::$env, $conf, $router,
  183. self::$modulesContext, self::$configAutoloader,
  184. self::$_mainConfigFile);
  185. }
  186. /**
  187. * restore the previous context of the application
  188. */
  189. public static function restoreContext() {
  190. if (!count(self::$contextBackup))
  191. return;
  192. list(self::$appPath, self::$varPath, self::$logPath, self::$configPath,
  193. self::$wwwPath, self::$scriptPath, self::$tempBasePath, self::$env,
  194. $conf, self::$_router, self::$modulesContext, self::$configAutoloader,
  195. self::$_mainConfigFile) = array_pop(self::$contextBackup);
  196. self::setConfig($conf);
  197. }
  198. /**
  199. * load a plugin from a plugin directory (any type of plugins)
  200. * @param string $name the name of the plugin
  201. * @param string $type the type of the plugin
  202. * @param string $suffix the suffix of the filename
  203. * @param string $classname the name of the class to instancy
  204. * @param mixed $args the argument for the constructor of the class. null = no argument.
  205. * @return null|object null if the plugin doesn't exists
  206. */
  207. public static function loadPlugin($name, $type, $suffix, $classname, $args = null) {
  208. if (!class_exists($classname,false)) {
  209. $optname = '_pluginsPathList_'.$type;
  210. if (!isset(self::config()->$optname))
  211. return null;
  212. $opt = & self::config()->$optname;
  213. if (!isset($opt[$name])
  214. || !file_exists($opt[$name].$name.$suffix) ){
  215. return null;
  216. }
  217. require_once($opt[$name].$name.$suffix);
  218. }
  219. if (!is_null($args))
  220. return new $classname($args);
  221. else
  222. return new $classname();
  223. }
  224. /**
  225. * Says if the given module $name is enabled
  226. * @param string $moduleName
  227. * @param boolean $includingExternal true if we want to know if the module
  228. * is also an external module, e.g. in an other entry point
  229. * @return boolean true : module is ok
  230. */
  231. public static function isModuleEnabled ($moduleName, $includingExternal = false) {
  232. if (!self::$_config)
  233. throw new \Exception ('Configuration is not loaded');
  234. if ($includingExternal && isset(self::$_config->_externalModulesPathList[$moduleName])) {
  235. return true;
  236. }
  237. return isset(self::$_config->_modulesPathList[$moduleName]);
  238. }
  239. /**
  240. * return the real path of a module
  241. * @param string $module a module name
  242. * @param boolean $includingExternal true if we want to know if the module
  243. * is also an external module, e.g. in an other entry point
  244. * @return string the corresponding path
  245. */
  246. public static function getModulePath($module, $includingExternal = false){
  247. if (!self::$_config)
  248. throw new \Exception ('Configuration is not loaded');
  249. if (!isset(self::$_config->_modulesPathList[$module])) {
  250. if ($includingExternal && isset(self::$_config->_externalModulesPathList[$module])) {
  251. return self::$_config->_externalModulesPathList[$module];
  252. }
  253. throw new \Exception('getModulePath : invalid module name');
  254. }
  255. return self::$_config->_modulesPathList[$module];
  256. }
  257. static protected $modulesContext = array();
  258. /**
  259. * set the context to the given module
  260. * @param string $module the module name
  261. */
  262. static function pushCurrentModule ($module){
  263. array_push (self::$modulesContext, $module);
  264. }
  265. /**
  266. * cancel the current context and set the context to the previous module
  267. * @return string the obsolet module name
  268. */
  269. static function popCurrentModule (){
  270. return array_pop (self::$modulesContext);
  271. }
  272. /**
  273. * get the module name of the current context
  274. * @return string name of the current module
  275. */
  276. static function getCurrentModule (){
  277. return end(self::$modulesContext);
  278. }
  279. }