From 87831344954887d2fd9b40fd98f75dd00680d5f0 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Tue, 20 Dec 2011 20:08:32 +0000 Subject: [PATCH] Changed variable set method. Fixed error logging. --- flight/Flight.php | 176 +++++++++++++++++++++------------------ flight/template/View.php | 29 ++++--- 2 files changed, 111 insertions(+), 94 deletions(-) diff --git a/flight/Flight.php b/flight/Flight.php index 0aa0e66..6328db3 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -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 { '
'.$e->getTraceAsString().'
'; try { - if (self::get('flight.log_errors')) { - error_log($e->getMessage()); - } self::response(false) ->status(500) ->write($msg) diff --git a/flight/template/View.php b/flight/template/View.php index 0d0bbab..049c2ba 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -26,7 +26,7 @@ class View { * * @var array */ - protected $data = array(); + protected $vars = array(); /** * Constructor. @@ -44,7 +44,7 @@ class View { * @return mixed */ public function get($key) { - return $this->data[$key]; + return $this->vars[$key]; } /** @@ -54,14 +54,13 @@ class View { * @param string $value Value */ public function set($key, $value = null) { - // If key is an array, save each key value pair if (is_array($key) || is_object($key)) { foreach ($key as $k => $v) { - $this->data[$k] = $v; + $this->vars[$k] = $v; } } - else if (is_string($key)) { - $this->data[$key] = $value; + else { + $this->vars[$key] = $value; } } @@ -71,7 +70,7 @@ class View { * @param string $key Key */ public function has($key) { - return isset($this->data[$key]); + return isset($this->vars[$key]); } /** @@ -81,10 +80,10 @@ class View { */ public function clear($key = null) { if (is_null($key)) { - $this->data = array(); + $this->vars = array(); } else { - unset($this->data[$key]); + unset($this->vars[$key]); } } @@ -97,16 +96,16 @@ class View { public function render($file, $data = null) { $template = $this->getTemplate($file); - if (is_array($data)) { - $this->data = array_merge($this->data, $data); - } - - extract($this->data); - if (!file_exists($template)) { throw new \Exception("Template file not found: $template."); } + if (is_array($data)) { + $this->vars = array_merge($this->vars, $data); + } + + extract($this->vars); + include $template; }