Allow framework to be reinitialized

pull/22/merge
Mike Cao 12 years ago
parent cec890c585
commit f2bd6f4e02

@ -65,31 +65,29 @@ 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);
}
// Load core components
if (self::$loader == null) {
self::$loader = new \flight\core\Loader();
self::$loader->start();
}
else {
self::$loader->reset();
}
if (self::$dispatcher == null) {
self::$dispatcher = new \flight\core\Dispatcher();
}
else {
self::$dispatcher->reset();
}
// Initialize autoloading
self::$loader->init();
// Register framework directory
self::$loader->addDirectory(dirname(__DIR__));
// Register default components
@ -115,9 +113,6 @@ class Flight {
// Enable output buffering
ob_start();
$initialized = true;
}
}
/**

@ -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();
}
}
?>

@ -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'));
$initialized = true;
public function start() {
spl_autoload_register(array($this, 'autoload'));
}
/**
* 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();
}
}
?>

@ -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 '';
}
}
?>

@ -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;
}
/**

Loading…
Cancel
Save