|  |  |  | @ -59,6 +59,96 @@ class Flight { | 
			
		
	
		
			
				
					|  |  |  |  |         return self::$loader->load($name, $shared); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /*** Core Methods ***/ | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * Initializes the framework. | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     public static function init() { | 
			
		
	
		
			
				
					|  |  |  |  |         static $initialized = false; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         if (!$initialized) { | 
			
		
	
		
			
				
					|  |  |  |  |             // Handle errors internally | 
			
		
	
		
			
				
					|  |  |  |  |             set_error_handler(array(__CLASS__, 'handleError')); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Handle exceptions internally | 
			
		
	
		
			
				
					|  |  |  |  |             set_exception_handler(array(__CLASS__, 'handleException')); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Turn off notices | 
			
		
	
		
			
				
					|  |  |  |  |             error_reporting (E_ALL ^ E_NOTICE); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Fix magic quotes | 
			
		
	
		
			
				
					|  |  |  |  |             if (get_magic_quotes_gpc()) { | 
			
		
	
		
			
				
					|  |  |  |  |                 $func = function ($value) use (&$func) { | 
			
		
	
		
			
				
					|  |  |  |  |                     return is_array($value) ? array_map($func, $value) : stripslashes($value); | 
			
		
	
		
			
				
					|  |  |  |  |                 }; | 
			
		
	
		
			
				
					|  |  |  |  |                 $_GET = array_map($func, $_GET); | 
			
		
	
		
			
				
					|  |  |  |  |                 $_POST = array_map($func, $_POST); | 
			
		
	
		
			
				
					|  |  |  |  |                 $_COOKIE = array_map($func, $_COOKIE); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Load core components | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader = new \flight\core\Loader(); | 
			
		
	
		
			
				
					|  |  |  |  |             self::$dispatcher = new \flight\core\Dispatcher(); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Initialize autoloading | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->init(); | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->addDirectory(dirname(__DIR__)); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Register default components | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->register('request', '\flight\net\Request'); | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->register('response', '\flight\net\Response'); | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->register('router', '\flight\net\Router'); | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->register('view', '\flight\template\View', array(), function($view){ | 
			
		
	
		
			
				
					|  |  |  |  |                 $view->path = Flight::get('flight.views.path'); | 
			
		
	
		
			
				
					|  |  |  |  |             }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Register framework methods | 
			
		
	
		
			
				
					|  |  |  |  |             $methods = array( | 
			
		
	
		
			
				
					|  |  |  |  |                 'start','stop','route','halt','error','notFound', | 
			
		
	
		
			
				
					|  |  |  |  |                 'render','redirect','etag','lastModified','json' | 
			
		
	
		
			
				
					|  |  |  |  |             ); | 
			
		
	
		
			
				
					|  |  |  |  |             foreach ($methods as $name) { | 
			
		
	
		
			
				
					|  |  |  |  |                 self::$dispatcher->set($name, array(__CLASS__, '_'.$name)); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Default settings | 
			
		
	
		
			
				
					|  |  |  |  |             self::set('flight.views.path', './views'); | 
			
		
	
		
			
				
					|  |  |  |  |             self::set('flight.log_errors', false); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Enable output buffering | 
			
		
	
		
			
				
					|  |  |  |  |             ob_start(); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             $initialized = true; | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * Custom error handler. Converts errors into exceptions. | 
			
		
	
		
			
				
					|  |  |  |  |      * | 
			
		
	
		
			
				
					|  |  |  |  |      * @param int $errno Error number | 
			
		
	
		
			
				
					|  |  |  |  |      * @param int $errstr Error string | 
			
		
	
		
			
				
					|  |  |  |  |      * @param int $errfile Error file name | 
			
		
	
		
			
				
					|  |  |  |  |      * @param int $errline Error file line number | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     public static function handleError($errno, $errstr, $errfile, $errline) { | 
			
		
	
		
			
				
					|  |  |  |  |         if (in_array($errno, array(E_USER_ERROR, E_RECOVERABLE_ERROR))) { | 
			
		
	
		
			
				
					|  |  |  |  |             static::handleException(new ErrorException($errstr, 0, $errno, $errfile, $errline)); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * Custom exception handler. Logs exceptions. | 
			
		
	
		
			
				
					|  |  |  |  |      * | 
			
		
	
		
			
				
					|  |  |  |  |      * @param object $e Exception | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     public static function handleException(Exception $e) { | 
			
		
	
		
			
				
					|  |  |  |  |         if (self::get('flight.log_errors')) { | 
			
		
	
		
			
				
					|  |  |  |  |             error_log($e->getMessage()); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         static::error($e); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * Maps a callback to a framework method. | 
			
		
	
		
			
				
					|  |  |  |  |      * | 
			
		
	
	
		
			
				
					|  |  |  | @ -109,15 +199,6 @@ class Flight { | 
			
		
	
		
			
				
					|  |  |  |  |         self::$dispatcher->hook($name, 'after', $callback); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * Adds a path for class autoloading. | 
			
		
	
		
			
				
					|  |  |  |  |      * | 
			
		
	
		
			
				
					|  |  |  |  |      * @param string $dir Directory path | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     public static function path($dir) { | 
			
		
	
		
			
				
					|  |  |  |  |         self::$loader->addDirectory($dir); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * Gets a variable. | 
			
		
	
		
			
				
					|  |  |  |  |      * | 
			
		
	
	
		
			
				
					|  |  |  | @ -141,7 +222,7 @@ class Flight { | 
			
		
	
		
			
				
					|  |  |  |  |                 self::$vars[$k] = $v; | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         else if (is_string($key)) { | 
			
		
	
		
			
				
					|  |  |  |  |         else { | 
			
		
	
		
			
				
					|  |  |  |  |             self::$vars[$key] = $value; | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
	
		
			
				
					|  |  |  | @ -171,75 +252,15 @@ class Flight { | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * Initializes the framework. | 
			
		
	
		
			
				
					|  |  |  |  |      * Adds a path for class autoloading. | 
			
		
	
		
			
				
					|  |  |  |  |      * | 
			
		
	
		
			
				
					|  |  |  |  |      * @param string $dir Directory path | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     public static function init() { | 
			
		
	
		
			
				
					|  |  |  |  |         static $initialized = false; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         if (!$initialized) { | 
			
		
	
		
			
				
					|  |  |  |  |             // Handle errors internally | 
			
		
	
		
			
				
					|  |  |  |  |             set_error_handler(array(__CLASS__, 'handleError')); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Handle exceptions internally | 
			
		
	
		
			
				
					|  |  |  |  |             set_exception_handler(array(__CLASS__, 'error')); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Turn off notices | 
			
		
	
		
			
				
					|  |  |  |  |             error_reporting (E_ALL ^ E_NOTICE); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Fix magic quotes | 
			
		
	
		
			
				
					|  |  |  |  |             if (get_magic_quotes_gpc()) { | 
			
		
	
		
			
				
					|  |  |  |  |                 $func = function ($value) use (&$func) { | 
			
		
	
		
			
				
					|  |  |  |  |                     return is_array($value) ? array_map($func, $value) : stripslashes($value); | 
			
		
	
		
			
				
					|  |  |  |  |                 }; | 
			
		
	
		
			
				
					|  |  |  |  |                 $_GET = array_map($func, $_GET); | 
			
		
	
		
			
				
					|  |  |  |  |                 $_POST = array_map($func, $_POST); | 
			
		
	
		
			
				
					|  |  |  |  |                 $_COOKIE = array_map($func, $_COOKIE); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Load core components | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader = new \flight\core\Loader(); | 
			
		
	
		
			
				
					|  |  |  |  |             self::$dispatcher = new \flight\core\Dispatcher(); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Initialize autoloading | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->init(); | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->addDirectory(dirname(__DIR__)); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Register default components | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->register('request', '\flight\net\Request'); | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->register('response', '\flight\net\Response'); | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->register('router', '\flight\net\Router'); | 
			
		
	
		
			
				
					|  |  |  |  |             self::$loader->register('view', '\flight\template\View', array(), function($view){ | 
			
		
	
		
			
				
					|  |  |  |  |                 $view->path = Flight::get('flight.views.path'); | 
			
		
	
		
			
				
					|  |  |  |  |             }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Register framework methods | 
			
		
	
		
			
				
					|  |  |  |  |             $methods = array( | 
			
		
	
		
			
				
					|  |  |  |  |                 'start','stop','route','halt','error','notFound', | 
			
		
	
		
			
				
					|  |  |  |  |                 'render','redirect','etag','lastModified','json' | 
			
		
	
		
			
				
					|  |  |  |  |             ); | 
			
		
	
		
			
				
					|  |  |  |  |             foreach ($methods as $name) { | 
			
		
	
		
			
				
					|  |  |  |  |                 self::$dispatcher->set($name, array(__CLASS__, '_'.$name)); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Default settings | 
			
		
	
		
			
				
					|  |  |  |  |             self::set('flight.views.path', './views'); | 
			
		
	
		
			
				
					|  |  |  |  |             self::set('flight.log_errors', false); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             // Enable output buffering | 
			
		
	
		
			
				
					|  |  |  |  |             ob_start(); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             $initialized = true; | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |     public static function path($dir) { | 
			
		
	
		
			
				
					|  |  |  |  |         self::$loader->addDirectory($dir); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * Custom error handler. | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     public static function handleError($errno, $errstr, $errfile, $errline) { | 
			
		
	
		
			
				
					|  |  |  |  |         if (in_array($errno, array(E_USER_ERROR, E_RECOVERABLE_ERROR))) { | 
			
		
	
		
			
				
					|  |  |  |  |             static::error(new ErrorException($errstr, 0, $errno, $errfile, $errline)); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  |     /*** Extensible Methods ***/ | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * Starts the framework. | 
			
		
	
	
		
			
				
					|  |  |  | @ -304,9 +325,6 @@ class Flight { | 
			
		
	
		
			
				
					|  |  |  |  |             '<pre>'.$e->getTraceAsString().'</pre>'; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         try { | 
			
		
	
		
			
				
					|  |  |  |  |             if (self::get('flight.log_errors')) { | 
			
		
	
		
			
				
					|  |  |  |  |                 error_log($e->getMessage()); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |             self::response(false) | 
			
		
	
		
			
				
					|  |  |  |  |                 ->status(500) | 
			
		
	
		
			
				
					|  |  |  |  |                 ->write($msg) | 
			
		
	
	
		
			
				
					|  |  |  | 
 |