From f2bd6f4e02df04858c3548f77e969869382fb9d3 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sun, 30 Dec 2012 12:09:59 -0800 Subject: [PATCH] Allow framework to be reinitialized --- flight/Flight.php | 89 ++++++++++++++++++-------------------- flight/core/Dispatcher.php | 11 ++++- flight/core/Loader.php | 30 ++++++++----- flight/net/Request.php | 4 +- flight/template/View.php | 4 +- 5 files changed, 76 insertions(+), 62 deletions(-) diff --git a/flight/Flight.php b/flight/Flight.php index 4d4fe32..326be9a 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -65,59 +65,54 @@ class Flight { * 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')); - - // 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); - } + // Handle errors internally + set_error_handler(array(__CLASS__, 'handleError')); + + // Handle exceptions internally + set_exception_handler(array(__CLASS__, 'handleException')); - // Load core components + // Load core components + if (self::$loader == null) { self::$loader = new \flight\core\Loader(); - self::$dispatcher = new \flight\core\Dispatcher(); + self::$loader->start(); + } + else { + self::$loader->reset(); + } - // 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)); - } + if (self::$dispatcher == null) { + self::$dispatcher = new \flight\core\Dispatcher(); + } + else { + self::$dispatcher->reset(); + } - // Default settings - self::set('flight.views.path', './views'); - self::set('flight.log_errors', false); + // Register framework directory + 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)); + } - // Enable output buffering - ob_start(); + // Default settings + self::set('flight.views.path', './views'); + self::set('flight.log_errors', false); - $initialized = true; - } + // Enable output buffering + ob_start(); } /** diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index d73b58e..82de9d1 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -141,6 +141,7 @@ class Dispatcher { self::invokeMethod($callback, $params) : self::callFunction($callback, $params); } + return null; } /** @@ -210,5 +211,13 @@ class Dispatcher { return call_user_func_array($func, $params); } } + + /** + * Resets the object to the initial state. + */ + public function reset() { + $this->events = array(); + $this->filters = array(); + } } -?> +?> \ No newline at end of file diff --git a/flight/core/Loader.php b/flight/core/Loader.php index cf90714..7ecfd75 100644 --- a/flight/core/Loader.php +++ b/flight/core/Loader.php @@ -34,7 +34,7 @@ class Loader { * * @var array */ - protected $dirs = array('.', __DIR__); + protected $dirs = array(); /** * Registers a class. @@ -147,16 +147,17 @@ class Loader { } /** - * Initializes the autoloader. + * Starts autoloader. */ - public function init() { - static $initialized = false; - - if (!$initialized) { - spl_autoload_register(array(__CLASS__, 'autoload')); + public function start() { + spl_autoload_register(array($this, 'autoload')); + } - $initialized = true; - } + /** + * Stops autoloading. + */ + public function stop() { + spl_autoload_unregister(array($this, 'autoload')); } /** @@ -183,5 +184,14 @@ class Loader { throw new \Exception('Unable to load file: '.$class_file); } } + + /** + * Resets the object to the initial state. + */ + public function reset() { + $this->classes = array(); + $this->instances = array(); + $this->dirs = array(); + } } -?> +?> \ No newline at end of file diff --git a/flight/net/Request.php b/flight/net/Request.php index c698806..0db3b33 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -73,7 +73,7 @@ class Request { $this->$name = $value; } - if ($this->base != '/' && strpos($this->url, $this->base) === 0) { + if ($this->base != '/' && strlen($this->base) > 0 && strpos($this->url, $this->base) === 0) { $this->url = substr($this->url, strlen($this->base)); } @@ -130,7 +130,7 @@ class Request { } } - return $_SERVER['REMOTE_ADDR']; + return ''; } } ?> diff --git a/flight/template/View.php b/flight/template/View.php index 362b432..d3d83f9 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -41,10 +41,10 @@ class View { * Gets a template variable. * * @param string $key Key - * @return mixed + * @return mixed Value */ public function get($key) { - return $this->vars[$key]; + return isset($this->vars[$key]) ? $this->vars[$key] : null; } /**