Allow framework to be reinitialized

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

@ -65,59 +65,54 @@ class Flight {
* Initializes the framework. * Initializes the framework.
*/ */
public static function init() { public static function init() {
static $initialized = false; // Handle errors internally
set_error_handler(array(__CLASS__, 'handleError'));
if (!$initialized) {
// Handle errors internally // Handle exceptions internally
set_error_handler(array(__CLASS__, 'handleError')); set_exception_handler(array(__CLASS__, 'handleException'));
// 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 // Load core components
if (self::$loader == null) {
self::$loader = new \flight\core\Loader(); self::$loader = new \flight\core\Loader();
self::$dispatcher = new \flight\core\Dispatcher(); self::$loader->start();
}
else {
self::$loader->reset();
}
// Initialize autoloading if (self::$dispatcher == null) {
self::$loader->init(); self::$dispatcher = new \flight\core\Dispatcher();
self::$loader->addDirectory(dirname(__DIR__)); }
else {
// Register default components self::$dispatcher->reset();
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 // Register framework directory
self::set('flight.views.path', './views'); self::$loader->addDirectory(dirname(__DIR__));
self::set('flight.log_errors', false);
// 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 // Default settings
ob_start(); self::set('flight.views.path', './views');
self::set('flight.log_errors', false);
$initialized = true; // Enable output buffering
} ob_start();
} }
/** /**

@ -141,6 +141,7 @@ class Dispatcher {
self::invokeMethod($callback, $params) : self::invokeMethod($callback, $params) :
self::callFunction($callback, $params); self::callFunction($callback, $params);
} }
return null;
} }
/** /**
@ -210,5 +211,13 @@ class Dispatcher {
return call_user_func_array($func, $params); 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 * @var array
*/ */
protected $dirs = array('.', __DIR__); protected $dirs = array();
/** /**
* Registers a class. * Registers a class.
@ -147,16 +147,17 @@ class Loader {
} }
/** /**
* Initializes the autoloader. * Starts autoloader.
*/ */
public function init() { public function start() {
static $initialized = false; spl_autoload_register(array($this, 'autoload'));
}
if (!$initialized) {
spl_autoload_register(array(__CLASS__, '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); 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; $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)); $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. * Gets a template variable.
* *
* @param string $key Key * @param string $key Key
* @return mixed * @return mixed Value
*/ */
public function get($key) { public function get($key) {
return $this->vars[$key]; return isset($this->vars[$key]) ? $this->vars[$key] : null;
} }
/** /**

Loading…
Cancel
Save