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