/environment/classes/Env.class.php

https://gitlab.com/x33n/ProjectPier-Core · PHP · 325 lines · 158 code · 33 blank · 134 comment · 31 complexity · 73b14fb971242f7a0877a84fe4941811 MD5 · raw file

  1. <?php
  2. class Env {
  3. /**
  4. * Check if environment is in debug mode
  5. *
  6. * @access public
  7. * @param void
  8. * @return boolean
  9. */
  10. static function isDebugging() {
  11. return isset($_GET['debug']);
  12. } // isDebugging
  13. /**
  14. * Use specific library. This function will look in application directory
  15. * first and then in environment library folder. If it doesn't finds requested
  16. * class in it LibraryDnxError will be raised
  17. *
  18. * @access public
  19. * @param string $library Library name
  20. * @return null
  21. * @throws LibraryDnxError
  22. */
  23. static function useLibrary($library, $plugin = null) {
  24. static $included = array();
  25. if (isset($included[$library]) && $included[$library]) {
  26. return;
  27. } // if
  28. $library_path = ENVIRONMENT_PATH . "/library/$library/";
  29. if (!file_exists($library_path)) {
  30. $library_path = ROOT . "/library/$library/";
  31. } // if
  32. if (!is_dir($library_path)) {
  33. if ($plugin) {
  34. $library_path = APPLICATION_PATH . "/plugins/$plugin/library/$library/";
  35. }
  36. if (!is_dir($library_path)) {
  37. throw new LibraryDnxError($library);
  38. } // if
  39. } // if
  40. // Call init library file if it exists
  41. $library_init_file = $library_path . $library . '.php';
  42. if (is_file($library_init_file)) {
  43. include_once $library_init_file;
  44. } // if
  45. $included[$library] = true;
  46. } // useLibrary
  47. /**
  48. * Include library error class if class is not already included
  49. *
  50. * @access public
  51. * @param string $error_class
  52. * @param string $library Library name
  53. * @return boolean
  54. */
  55. static function useLibraryError($error_class, $library) {
  56. if (class_exists($error_class)) {
  57. return true;
  58. } // if
  59. $expected_path = ENVIRONMENT_PATH . "/library/$library/errors/$error_class.class.php";
  60. if (is_file($expected_path)) {
  61. include_once $expected_path;
  62. return true;
  63. } else {
  64. throw new FileDnxError($expected_path);
  65. } // if
  66. } // useLibraryError
  67. /**
  68. * Show nice error output.
  69. *
  70. * @access public
  71. * @param Error $error
  72. * @param boolean $die Die when done, default value is true
  73. * @return null
  74. */
  75. static function dumpError($error, $die = true) {
  76. static $css_rendered = false;
  77. // Check error instance...
  78. if (!instance_of($error, 'Error')) {
  79. if (!instance_of($error, 'Exception')) {
  80. print '$error is not an <i>Error</i> or <i>Exception</i> instance! ' . $error;
  81. return;
  82. } // if
  83. } // if
  84. // OK, include template...
  85. include ENVIRONMENT_PATH . '/templates/dump_error.php';
  86. // Die?
  87. if ($die) {
  88. die();
  89. } // if
  90. } // dumpError
  91. /**
  92. * Contruct controller and execute specific action
  93. *
  94. * @access public
  95. * @param string $controller_name
  96. * @param string $action
  97. * @return null
  98. */
  99. static function executeAction($controller_name, $action) {
  100. trace(__FILE__,"executeAction($controller_name, $action)");
  101. Env::useController($controller_name);
  102. $controller_class = Env::getControllerClass($controller_name);
  103. if (!class_exists($controller_class, false)) {
  104. trace(__FILE__,"executeAction($controller_name, $action) - $controller_class does not exist");
  105. throw new ControllerDnxError($controller_name);
  106. } // if
  107. trace(__FILE__,"executeAction($controller_name, $action) - new $controller_class()");
  108. $controller = new $controller_class();
  109. if (!instance_of($controller, 'Controller')) {
  110. trace(__FILE__,"executeAction($controller_name, $action) - $controller_class not Controller");
  111. throw new ControllerDnxError($controller_name);
  112. } // if
  113. trace(__FILE__,"executeAction($controller_name, $action) - $controller_class -> execute($action)");
  114. return $controller->execute($action);
  115. } // executeAction
  116. /**
  117. * Find and include specific controller based on controller name
  118. *
  119. * @access public
  120. * @param string $controller_name
  121. * @return boolean
  122. * @throws FileDnxError if controller file does not exists
  123. */
  124. static function useController($controller_name) {
  125. trace(__FILE__,"useController($controller_name)");
  126. $controller_class = Env::getControllerClass($controller_name);
  127. if (class_exists($controller_class, false)) {
  128. return true;
  129. } // if
  130. $controller_file = Env::getControllerPath($controller_name);
  131. if (is_file($controller_file)) {
  132. trace(__FILE__,"useController($controller_name) - include_once $controller_file");
  133. include_once $controller_file;
  134. return true;
  135. } else {
  136. throw new FileDnxError($controller_file, "Controller '$controller_name' does not exists (expected location '$controller_file')");
  137. } // if
  138. } // useController
  139. /**
  140. * Get Controller File Path, looks for controller file and returns the path
  141. *
  142. * @access public
  143. * @param string $controller_name
  144. * @return boolean
  145. * @throws FileDnxError if controller file does not exists
  146. */
  147. static function getControllerPath($controller_name) {
  148. trace(__FILE__,"getControllerPath($controller_name)");
  149. $controller_class = Env::getControllerClass($controller_name);
  150. $controller_file = APPLICATION_PATH . "/controllers/$controller_class.class.php";
  151. trace(__FILE__,"getControllerPath($controller_name) - core: $controller_file");
  152. $controller_file_plugin = APPLICATION_PATH . "/plugins/$controller_name/controllers/$controller_class.class.php";
  153. trace(__FILE__,"getControllerPath($controller_name) - plugin: $controller_file_plugin");
  154. if (is_file($controller_file)) return $controller_file;
  155. else if (is_file($controller_file_plugin)) return $controller_file_plugin;
  156. else throw new FileDnxError($controller_file, "Controller '$controller_name' does not exists (expected location '$controller_file' or '$controller_file_plugin')");
  157. } // getControllerFilePath
  158. /**
  159. * Use specific helper
  160. *
  161. * @access public
  162. * @param string $helper Helper name
  163. * @return boolean
  164. * @throws FileDnxError
  165. */
  166. static function useHelper($helper, $controller_name = null) {
  167. trace(__FILE__,"useHelper($helper, $controller_name)");
  168. $helper_file = Env::getHelperPath($helper, $controller_name);
  169. // If we have it include, else throw exception
  170. if (is_file($helper_file)) {
  171. trace(__FILE__,"useHelper($helper, $controller_name) including $helper_file");
  172. include_once $helper_file;
  173. return true;
  174. } // if
  175. throw new FileDnxError($helper_file, "Helper '$helper' does not exists (expected location '$helper_file')");
  176. } // useHelper
  177. /**
  178. * Check if specific helper exists
  179. *
  180. * @access public
  181. * @param string $helper
  182. * @return boolean
  183. */
  184. static function helperExists($helper, $controller_name = null) {
  185. return is_file(self::getHelperPath($helper, $controller_name));
  186. } // helperExists
  187. /**
  188. * Return controller name based on controller class
  189. *
  190. * @access public
  191. * @param string $controller_class
  192. * @return string
  193. */
  194. static function getControllerName($controller_class) {
  195. return Inflector::underscore( substr($controller_class, 0, strlen($controller_class) - 10) );
  196. } // getControllerName
  197. /**
  198. * Return controller class based on controller name
  199. *
  200. * @access public
  201. * @param string $controller_name
  202. * @return string
  203. */
  204. static function getControllerClass($controller_name) {
  205. trace(__FILE__,"getControllerClass($controller_name)");
  206. return Inflector::camelize($controller_name) . 'Controller';
  207. } // getControllerClass
  208. /**
  209. * Return path of specific template
  210. *
  211. * @access public
  212. * @param string $template
  213. * @param string $controller_name
  214. * @return string
  215. */
  216. static function getTemplatePath($template, $controller_name = null) {
  217. trace(__FILE__,"getTemplatePath($template, $controller_name)");
  218. // Look for template file in theme, core and plugin directories
  219. $template_path=THEMES_DIR.'/'.config_option('theme')."/views/$controller_name/$template.php";
  220. if (is_readable($template_path)) return $template_path;
  221. $template_path=APPLICATION_PATH."/views/$controller_name/$template.php";
  222. if (is_readable($template_path)) return $template_path;
  223. $template_path_plugin='';
  224. if ($controller_name) {
  225. $template_path_plugin=APPLICATION_PATH."/plugins/$controller_name/views/$template.php";
  226. if (is_readable($template_path_plugin)) return $template_path_plugin;
  227. } // if
  228. trace(__FILE__,"getTemplatePath($template, $controller_name) - can not read [$template_path] or [$template_path_plugin]");
  229. return false;
  230. } // getTemplatePath
  231. /**
  232. * Return layout
  233. *
  234. * @access public
  235. * @param string $layout
  236. * @return string
  237. */
  238. static function getLayoutPath($layout) {
  239. if (file_exists(THEMES_DIR.'/'.config_option('theme')."/layouts/$layout.php"))
  240. return THEMES_DIR.'/'.config_option('theme')."/layouts/$layout.php";
  241. else
  242. return APPLICATION_PATH . "/layouts/$layout.php";
  243. } // getLayoutPath
  244. /**
  245. * Return path of specific helper
  246. *
  247. * @access public
  248. * @param string $helper
  249. * @return string
  250. */
  251. static function getHelperPath($helper, $controller_name = null) {
  252. trace(__FILE__,"getHelperPath($helper, $controller_name)");
  253. //Look for helper file path into core and plugins directories
  254. $helper_path=APPLICATION_PATH . "/helpers/$helper.php";
  255. trace(__FILE__,"getHelperPath($helper, $controller_name): trying $helper_path");
  256. if (is_readable($helper_path)) return $helper_path;
  257. $helper_path_plugin='';
  258. if ($controller_name) {
  259. $helper_path_plugin=APPLICATION_PATH . "/plugins/$controller_name/helpers/$helper.php";
  260. trace(__FILE__,"getHelperPath($helper, $controller_name): trying $helper_path_plugin");
  261. if (file_exists($helper_path_plugin)) return $helper_path_plugin;
  262. }
  263. trace(__FILE__,"getHelperPath($helper, $controller_name) - can not read [$helper_path] or [$helper_path_plugin]");
  264. return false;
  265. } // getHelperPath
  266. /**
  267. * Return default base URL based on owner company status
  268. *
  269. * @access private
  270. * @param void
  271. * @return string
  272. */
  273. private function getDefaultBase() {
  274. return ROOT_URL;
  275. } // getDefaultBase
  276. } // Env
  277. // ---------------------------------------------------
  278. // This routines are used a lot in controllers and
  279. // templates so here are shortcut methods
  280. // ---------------------------------------------------
  281. /**
  282. * Interface to Env::getTemplatePath() function
  283. *
  284. * @access public
  285. * @param string $template Template name
  286. * @param string $controller_name
  287. * @return string
  288. */
  289. function get_template_path($template, $controller_name = null) {
  290. return Env::getTemplatePath($template, $controller_name);
  291. } // get_template_path
  292. ?>