PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/trax/vendor/trax/trax.php

https://github.com/phpontrax/trax
PHP | 284 lines | 197 code | 27 blank | 60 comment | 22 complexity | af5dd7d0e868f5faa5e00e167bbc016d MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the Trax class for framework configs
  4. *
  5. * (PHP 5)
  6. *
  7. * @package PHPonTrax
  8. * @version $Id:$
  9. * @copyright (c) 2005 John Peterson
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining
  12. * a copy of this software and associated documentation files (the
  13. * "Software"), to deal in the Software without restriction, including
  14. * without limitation the rights to use, copy, modify, merge, publish,
  15. * distribute, sublicense, and/or sell copies of the Software, and to
  16. * permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be
  20. * included in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. /**
  31. * @todo Document this class
  32. * @package PHPonTrax
  33. */
  34. class Trax {
  35. const
  36. MAJOR = 0,
  37. MINOR = 18,
  38. TINY = 0;
  39. public static
  40. $models_path = null,
  41. $views_path = null,
  42. $controllers_path = null,
  43. $helpers_path = null,
  44. $layouts_path = null,
  45. $config_path = null,
  46. $environments_path = null,
  47. $lib_path = null,
  48. $app_path = null,
  49. $log_path = null,
  50. $plugins_path = null,
  51. $vendor_path = null,
  52. $public_path = null,
  53. $tmp_path = null,
  54. $assets_path = null,
  55. $url_prefix = null,
  56. $url_word_seperator = null, # used to put dashes in the url for seo
  57. $views_extension = 'phtml',
  58. $path_seperator = ":", # default is Unix
  59. $current_controller_path = null,
  60. $current_controller_name = null,
  61. $current_action_name = null,
  62. $current_controller_object = null,
  63. $session_store = "file_store",
  64. $session_class_name = "ActiveRecordStore",
  65. $session_save_path = "",
  66. $session_name = "TRAXSESSID",
  67. $session_cookie_domain = null,
  68. $session_lifetime = "0",
  69. $session_maxlifetime_minutes = "20",
  70. $version = null,
  71. $show_trax_errors = false,
  72. $server_default_include_path = null,
  73. $include_paths = array(),
  74. $plugins = array(),
  75. $assets_compress = false,
  76. $assets_compile = false;
  77. public static function initialize() {
  78. self::register_autoload();
  79. self::$version = self::version();
  80. if(substr(PHP_OS, 0, 3) == 'WIN') {
  81. # Windows
  82. self::$path_seperator = ";";
  83. }
  84. # Set include paths
  85. self::$models_path = TRAX_ROOT."/app/models";
  86. self::$views_path = TRAX_ROOT."/app/views";
  87. self::$controllers_path = TRAX_ROOT."/app/controllers";
  88. self::$helpers_path = TRAX_ROOT."/app/helpers";
  89. self::$layouts_path = TRAX_ROOT."/app/views/layouts";
  90. self::$assets_path = TRAX_ROOT."/app/assets";
  91. self::$config_path = TRAX_ROOT."/config";
  92. self::$environments_path = TRAX_ROOT."/config/environments";
  93. self::$lib_path = TRAX_ROOT."/lib";
  94. self::$app_path = TRAX_ROOT."/app";
  95. self::$log_path = TRAX_ROOT."/log";
  96. self::$vendor_path = TRAX_ROOT."/vendor";
  97. self::$plugins_path = TRAX_ROOT."/vendor/plugins";
  98. self::$public_path = TRAX_ROOT."/public";
  99. self::$tmp_path = TRAX_ROOT."/tmp";
  100. # Set which file to log php errors to for this application
  101. # As well in your application you can do error_log("whatever") and it will go to this log file.
  102. ini_set("log_errors", "On");
  103. ini_set("error_log", self::$log_path."/".TRAX_ENV.".log");
  104. if(TRAX_ENV == "development") {
  105. # Display errors to browser if in development mode for debugging
  106. ini_set("display_errors", "On");
  107. } else {
  108. # Hide errors from browser if not in development mode
  109. ini_set("display_errors", "Off");
  110. }
  111. # Get the include_path so we know what the original path was
  112. self::$server_default_include_path = ini_get("include_path");
  113. # Set the include_paths
  114. self::set_default_include_paths();
  115. # Include Trax library files.
  116. include_once(TRAX_LIB_ROOT."/session.php");
  117. include_once(TRAX_LIB_ROOT."/input_filter.php");
  118. include_once(TRAX_LIB_ROOT."/trax_exceptions.php");
  119. include_once(TRAX_LIB_ROOT."/inflector.php");
  120. include_once(TRAX_LIB_ROOT."/active_record.php");
  121. include_once(TRAX_LIB_ROOT."/action_controller.php");
  122. include_once(TRAX_LIB_ROOT."/action_view.php");
  123. include_once(TRAX_LIB_ROOT."/action_mailer.php");
  124. include_once(TRAX_LIB_ROOT."/dispatcher.php");
  125. include_once(TRAX_LIB_ROOT."/router.php");
  126. self::load_active_record_connections_config();
  127. ViewHandlers::register_extension('html');
  128. # legancy phtml extension
  129. ViewHandlers::register_extension('phtml');
  130. }
  131. public static function add_include_path($path, $prepend = false, $use_trax_root = false) {
  132. if(is_array($path)) {
  133. foreach($path as $new_path) {
  134. if(!in_array($new_path, self::$include_paths)) {
  135. $new_paths[] = $use_trax_root ? TRAX_ROOT."/".$new_path : $new_path;
  136. }
  137. }
  138. } elseif(!in_array($path, self::$include_paths)) {
  139. $new_paths[] = $use_trax_root ? TRAX_ROOT."/".$path : $path;
  140. }
  141. if(is_array($new_paths) && is_array(self::$include_paths)) {
  142. foreach($new_paths as $path) {
  143. if($prepend) {
  144. array_unshift(self::$include_paths, $path);
  145. } else {
  146. array_push(self::$include_paths, $path);
  147. }
  148. }
  149. ini_set("include_path", implode(self::$path_seperator, self::$include_paths));
  150. }
  151. }
  152. public static function set_default_include_paths() {
  153. # first clear out all the current paths
  154. self::$include_paths = array();
  155. # now add the default paths
  156. self::add_include_path(array(
  157. ".", # current directory
  158. TRAX_LIB_ROOT, # trax libs (vendor/trax or server trax libs)
  159. PHP_LIB_ROOT, # php libs dir (ex: /usr/local/lib/php)
  160. self::$lib_path, # app specific libs extra libs to include
  161. self::$plugins_path, # plugins / engines
  162. self::$vendor_path, # 3rd party libs to include in vendor (vendor/third_party)
  163. self::$server_default_include_path # tack on the old include_path to the end
  164. ));
  165. }
  166. public static function load_active_record_connections_config() {
  167. # Make sure database settings are cleared out
  168. ActiveRecord::$database_settings = array();
  169. ActiveRecord::clear_all_connections();
  170. if(file_exists(self::$config_path."/database.ini")) {
  171. # Load databse settings
  172. ActiveRecord::$database_settings = parse_ini_file(self::$config_path."/database.ini", true);
  173. #error_log("db settings:".print_r(ActiveRecord::$database_settings, true));
  174. }
  175. ActiveRecord::$environment = TRAX_ENV;
  176. ActiveRecord::$logger = new Trax;
  177. }
  178. public static function include_env_config() {
  179. # Include the application environment specific config file
  180. if(file_exists(self::$environments_path."/".TRAX_ENV.".php")) {
  181. include_once(self::$environments_path."/".TRAX_ENV.".php");
  182. }
  183. }
  184. public static function load_plugins($plugins = array()) {
  185. $plugins = count($plugins) ? $plugins : Trax::$plugins;
  186. foreach((array)$plugins as $plugin) {
  187. if(file_exists(self::$plugins_path."/{$plugin}/init.php")) {
  188. include_once(self::$plugins_path."/{$plugin}/init.php");
  189. $loaded_plugins[] = $plugin;
  190. }
  191. }
  192. Trax::$plugins = (array)$loaded_plugins;
  193. }
  194. public static function version() {
  195. return implode('.', array(self::MAJOR, self::MINOR, self::TINY));
  196. }
  197. public static function log($message, $filename = '') {
  198. $date = date("[d-M-Y H:i:s]");
  199. $message = "$date $message\n";
  200. $log_file = self::$log_path.'/'.($filename != '' ? $filename : TRAX_ENV).'.log';
  201. if(file_exists($log_file)) {
  202. error_log($message, 3, $log_file);
  203. } else {
  204. exec("touch {$log_file}");
  205. error_log($message, 3, $log_file);
  206. }
  207. }
  208. public static function register_autoload() {
  209. if(function_exists('spl_autoload_register')) {
  210. # Register ourselves with SPL
  211. return spl_autoload_register('trax_autoload');
  212. } else {
  213. throw new Exception("Your php install must support spl_autoload_register");
  214. }
  215. }
  216. }
  217. ###################################################################
  218. # Auto include model / controller / other app specific libs files
  219. ###################################################################
  220. function trax_autoload($class_name) {
  221. $file = Inflector::underscore($class_name).".php";
  222. $file_org = $class_name.".php";
  223. if(file_exists(Trax::$models_path."/$file")) {
  224. # Include model classes
  225. include_once(Trax::$models_path."/$file");
  226. } elseif(file_exists(Trax::$controllers_path."/$file")) {
  227. # Include extra controller classes
  228. include_once(Trax::$controllers_path."/$file");
  229. } elseif(file_exists(Trax::$lib_path."/$file")) {
  230. # Include users application libs
  231. include_once(Trax::$lib_path."/$file");
  232. } elseif(file_exists(Trax::$lib_path."/$file_org")) {
  233. # Include users application libs
  234. include_once(Trax::$lib_path."/$file_org");
  235. }
  236. if(count(Trax::$plugins)) {
  237. foreach(Trax::$plugins as $plugin) {
  238. $model_path = Trax::$plugins_path."/$plugin/app/models";
  239. $controller_path = Trax::$plugins_path."/$plugin/app/controllers";
  240. $lib_path = Trax::$plugins_path."/$plugin/lib";
  241. if(file_exists("$model_path/$file")) {
  242. # Include model classes
  243. include_once("$model_path/$file");
  244. } elseif(file_exists("$controller_path/$file")) {
  245. # Include extra controller classes
  246. include_once("$controller_path/$file");
  247. } elseif(file_exists("$lib_path/$file")) {
  248. # Include users application libs
  249. include_once("$lib_path/$file");
  250. } elseif(file_exists("$lib_path/$file_org")) {
  251. # Include users application libs
  252. include_once("$lib_path/$file_org");
  253. }
  254. }
  255. }
  256. }
  257. ?>