PageRenderTime 70ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/php-senthot/Senthot/Lib/Core/Sen.class.php

https://github.com/jbrisbin/FrameworkBenchmarks
PHP | 292 lines | 191 code | 21 blank | 80 comment | 40 complexity | 495a5a55703e264e38751b092afe1c19 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, LGPL-2.0
  1. <?php
  2. // +--------------------------------------------------------------------------
  3. // | Senthot [ DEVELOPED BY ME ]
  4. // +--------------------------------------------------------------------------
  5. // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
  6. // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // | Author: ms134n ( ms134n@gmail.com )
  8. // +--------------------------------------------------------------------------
  9. /**
  10. * Senthot Portal category
  11. * @category Sen
  12. * @package Sen
  13. * @subpackage Core
  14. * @author ms134n <ms134n@gmail.com>
  15. */
  16. class Sen {
  17. private static $_instance = array();
  18. /**
  19. * Application initialization
  20. * @access public
  21. * @return void
  22. */
  23. static public function start() {
  24. // Setting error and exception handling
  25. register_shutdown_function(array('Sen','fatalError'));
  26. set_error_handler(array('Sen','appError'));
  27. set_exception_handler(array('Sen','appException'));
  28. // Registered AUTOLOAD method
  29. spl_autoload_register(array('Sen', 'autoload'));
  30. //[RUNTIME]
  31. Sen::buildApp(); // Precompiled project
  32. //[/RUNTIME]
  33. // Run the application
  34. App::run();
  35. return ;
  36. }
  37. //[RUNTIME]
  38. /**
  39. * Read configuration information Compile the project
  40. * @access private
  41. * @return string
  42. */
  43. static private function buildApp() {
  44. // Read operating mode
  45. if(defined('MODE_NAME')) { // Read mode setting
  46. $mode = include MODE_PATH.strtolower(MODE_NAME).'.php';
  47. }else{
  48. $mode = array();
  49. }
  50. // Core Conventions configuration file to load
  51. C(include SEN_PATH.'Conf/convention.php');
  52. if(isset($mode['config'])) {// Load mode profile
  53. C( is_array($mode['config'])?$mode['config']:include $mode['config'] );
  54. }
  55. // Load project configuration file
  56. if(is_file(CONF_PATH.'config.php'))
  57. C(include CONF_PATH.'config.php');
  58. // Loading framework underlying language pack
  59. L(include SEN_PATH.'Lang/'.strtolower(C('DEFAULT_LANG')).'.php');
  60. // Load model system behavior definition
  61. if(C('APP_TAGS_ON')) {
  62. if(isset($mode['extends'])) {
  63. C('extends',is_array($mode['extends'])?$mode['extends']:include $mode['extends']);
  64. }else{ // Addonsed definition of default load behavior of the system
  65. C('extends', include SEN_PATH.'Conf/tags.php');
  66. }
  67. }
  68. // Load application behavior definition
  69. if(isset($mode['tags'])) {
  70. C('tags', is_array($mode['tags'])?$mode['tags']:include $mode['tags']);
  71. }elseif(is_file(CONF_PATH.'tags.php')){
  72. // Default load the project file defines the configuration directory tags
  73. C('tags', include CONF_PATH.'tags.php');
  74. }
  75. $compile = '';
  76. // Core reading list compiled file
  77. if(isset($mode['core'])) {
  78. $list = $mode['core'];
  79. }else{
  80. $list = array(
  81. SEN_PATH.'Common/functions.php', // Standard mode function library
  82. CORE_PATH.'Core/Log.class.php', // Log processing class
  83. CORE_PATH.'Core/Dispatcher.class.php', // URL scheduling class
  84. CORE_PATH.'Core/App.class.php', // Application class
  85. CORE_PATH.'Core/Action.class.php', // Controller class
  86. CORE_PATH.'Core/View.class.php', // View class
  87. );
  88. }
  89. // Compile a list of additional core project documents
  90. if(is_file(CONF_PATH.'core.php')) {
  91. $list = array_merge($list,include CONF_PATH.'core.php');
  92. }
  93. foreach ($list as $file){
  94. if(is_file($file)) {
  95. require_cache($file);
  96. if(!APP_DEBUG) $compile .= compile($file);
  97. }
  98. }
  99. // Load the project public documents
  100. if(is_file(COMMON_PATH.'common.php')) {
  101. include COMMON_PATH.'common.php';
  102. // Compiled file
  103. if(!APP_DEBUG) $compile .= compile(COMMON_PATH.'common.php');
  104. }
  105. // Loading mode alias definitions
  106. if(isset($mode['alias'])) {
  107. $alias = is_array($mode['alias'])?$mode['alias']:include $mode['alias'];
  108. alias_import($alias);
  109. if(!APP_DEBUG) $compile .= 'alias_import('.var_export($alias,true).');';
  110. }
  111. // Loading a project alias definitions
  112. if(is_file(CONF_PATH.'alias.php')){
  113. $alias = include CONF_PATH.'alias.php';
  114. alias_import($alias);
  115. if(!APP_DEBUG) $compile .= 'alias_import('.var_export($alias,true).');';
  116. }
  117. if(APP_DEBUG) {
  118. // Debug mode to load the system default configuration file
  119. C(include SEN_PATH.'Conf/debug.php');
  120. // Read the debug mode application status
  121. $status = C('APP_STATUS');
  122. // Project configuration file to load the corresponding
  123. if(is_file(CONF_PATH.$status.'.php'))
  124. // Allow the project to increase the development mode configuration definition
  125. C(include CONF_PATH.$status.'.php');
  126. }else{
  127. // Deployment model generates compiled files below
  128. build_runtime_cache($compile);
  129. }
  130. return ;
  131. }
  132. //[/RUNTIME]
  133. /**
  134. * The system automatically loads the library Senthot
  135. * And Support configure automatic loading path
  136. * @param string $class Object class name
  137. * @return void
  138. */
  139. public static function autoload($class) {
  140. // Check for alias definitions
  141. if(alias_import($class)) return ;
  142. $libPath = defined('BASE_LIB_PATH')?BASE_LIB_PATH:LIB_PATH;
  143. $group = defined('GROUP_NAME') && C('APP_GROUP_MODE')==0 ?GROUP_NAME.'/':'';
  144. $file = $class.'.class.php';
  145. if(substr($class,-8)=='Behavior') { // Load Behavior
  146. if(require_array(array(
  147. CORE_PATH.'Behavior/'.$file,
  148. ADDONS_PATH.'Behavior/'.$file,
  149. LIB_PATH.'Behavior/'.$file,
  150. $libPath.'Behavior/'.$file),true)
  151. || (defined('MODE_NAME') && require_cache(MODE_PATH.ucwords(MODE_NAME).'/Behavior/'.$file))) {
  152. return ;
  153. }
  154. }elseif(substr($class,-5)=='Model'){ // Load Model
  155. if(require_array(array(
  156. LIB_PATH.'Model/'.$group.$file,
  157. $libPath.'Model/'.$file,
  158. ADDONS_PATH.'Model/'.$file),true)) {
  159. return ;
  160. }
  161. }elseif(substr($class,-6)=='Action'){ // Load Controller
  162. if(require_array(array(
  163. LIB_PATH.'Action/'.$group.$file,
  164. $libPath.'Action/'.$file,
  165. ADDONS_PATH.'Action/'.$file),true)) {
  166. return ;
  167. }
  168. }elseif(substr($class,0,5)=='Cache'){ // Load cache drive
  169. if(require_array(array(
  170. ADDONS_PATH.'Driver/Cache/'.$file,
  171. CORE_PATH.'Driver/Cache/'.$file),true)){
  172. return ;
  173. }
  174. }elseif(substr($class,0,2)=='Db'){ // Load database driver
  175. if(require_array(array(
  176. ADDONS_PATH.'Driver/Db/'.$file,
  177. CORE_PATH.'Driver/Db/'.$file),true)){
  178. return ;
  179. }
  180. }elseif(substr($class,0,8)=='Template'){ // Loading template engine driven
  181. if(require_array(array(
  182. ADDONS_PATH.'Driver/Template/'.$file,
  183. CORE_PATH.'Driver/Template/'.$file),true)){
  184. return ;
  185. }
  186. }elseif(substr($class,0,6)=='TagLib'){ // Load tag library drive
  187. if(require_array(array(
  188. ADDONS_PATH.'Driver/TagLib/'.$file,
  189. CORE_PATH.'Driver/TagLib/'.$file),true)) {
  190. return ;
  191. }
  192. }
  193. // According to the settings automatically load path try to search
  194. $paths = explode(',',C('APP_AUTOLOAD_PATH'));
  195. foreach ($paths as $path){
  196. if(import($path.'.'.$class))
  197. // If you load the class success, returns
  198. return ;
  199. }
  200. }
  201. /**
  202. * Get object instance Support call the class static method
  203. * @param string $class Object class name
  204. * @param string $method Static class method name
  205. * @return object
  206. */
  207. static public function instance($class,$method='') {
  208. $identify = $class.$method;
  209. if(!isset(self::$_instance[$identify])) {
  210. if(class_exists($class)){
  211. $o = new $class();
  212. if(!empty($method) && method_exists($o,$method))
  213. self::$_instance[$identify] = call_user_func_array(array(&$o, $method));
  214. else
  215. self::$_instance[$identify] = $o;
  216. }
  217. else
  218. halt(L('_CLASS_NOT_EXIST_').':'.$class);
  219. }
  220. return self::$_instance[$identify];
  221. }
  222. /**
  223. * Custom Exception Handling
  224. * @access public
  225. * @param mixed $e Exception object
  226. */
  227. static public function appException($e) {
  228. halt($e->__toString());
  229. }
  230. /**
  231. * Custom Error Handling
  232. * @access public
  233. * @param int $errno Error Type
  234. * @param string $errstr Error Messages
  235. * @param string $errfile Error File
  236. * @param int $errline Error Rows
  237. * @return void
  238. */
  239. static public function appError($errno, $errstr, $errfile, $errline) {
  240. switch ($errno) {
  241. case E_ERROR:
  242. case E_PARSE:
  243. case E_CORE_ERROR:
  244. case E_COMPILE_ERROR:
  245. case E_USER_ERROR:
  246. ob_end_clean();
  247. // Page Compression Output Support
  248. if(C('OUTPUT_ENCODE')){
  249. $zlib = ini_get('zlib.output_compression');
  250. if(empty($zlib)) ob_start('ob_gzhandler');
  251. }
  252. $errorStr = "$errstr ".$errfile." subsection $errline line.";
  253. if(C('LOG_RECORD')) Log::write("[$errno] ".$errorStr,Log::ERR);
  254. function_exists('halt')?halt($errorStr):exit('ERROR:'.$errorStr);
  255. break;
  256. case E_STRICT:
  257. case E_USER_WARNING:
  258. case E_USER_NOTICE:
  259. default:
  260. $errorStr = "[$errno] $errstr ".$errfile." subsection $errline line.";
  261. trace($errorStr,'','NOTIC');
  262. break;
  263. }
  264. }
  265. // Fatal error trapping
  266. static public function fatalError() {
  267. if ($e = error_get_last()) {
  268. Sen::appError($e['type'],$e['message'],$e['file'],$e['line']);
  269. }
  270. }
  271. }