diff --git a/README.md b/README.md
index 0095a46..bdfe887 100644
--- a/README.md
+++ b/README.md
@@ -20,7 +20,7 @@ Flight requires `PHP 5.3` or greater.
## License
-Flight is released under the [MIT](http://www.opensource.org/licenses/mit-license.php) license.
+Flight is released under the [MIT](http://flightphp.com/license) license.
## Installation
diff --git a/VERSION b/VERSION
index 9f8e9b6..b123147 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.0
\ No newline at end of file
+1.1
\ No newline at end of file
diff --git a/composer.json b/composer.json
index 95f3438..5405380 100644
--- a/composer.json
+++ b/composer.json
@@ -1,10 +1,8 @@
{
"name": "mikecao/flight",
- "autoload": {
- "files": ["flight/Flight.php"]
- },
- "license": "MIT",
"description": "Flight is a fast, simple, extensible framework for PHP. Flight enables you to quickly and easily build RESTful web applications.",
+ "homepage": "http://flightphp.com",
+ "license": "MIT",
"authors": [
{
"name": "Mike Cao",
@@ -15,5 +13,8 @@
],
"require": {
"php": ">=5.3.0"
+ },
+ "autoload": {
+ "files": [ "flight/Flight.php" ]
}
}
diff --git a/flight/Engine.php b/flight/Engine.php
new file mode 100644
index 0000000..ca185d6
--- /dev/null
+++ b/flight/Engine.php
@@ -0,0 +1,458 @@
+
+ * @license MIT, http://flightphp.com/license
+ */
+
+namespace flight;
+
+use flight\core\Loader;
+use flight\core\Dispatcher;
+
+/**
+ * The Engine class contains the core functionality of the framework.
+ * It is responsible for loading an HTTP request, running the assigned services,
+ * and generating an HTTP response.
+ */
+class Engine {
+ /**
+ * Stored variables.
+ *
+ * @var array
+ */
+ protected $vars;
+
+ /**
+ * Class loader.
+ *
+ * @var object
+ */
+ protected $loader;
+
+ /**
+ * Event dispatcher.
+ *
+ * @var object
+ */
+ protected $dispatcher;
+
+ /**
+ * Constructor.
+ */
+ public function __construct() {
+ $this->vars = array();
+
+ $this->loader = new Loader();
+ $this->loader->autoload(true, dirname(dirname(__DIR__)));
+
+ $this->dispatcher = new Dispatcher();
+
+ $this->init();
+ }
+
+ /**
+ * Handles calls to class methods.
+ *
+ * @param string $name Method name
+ * @param array $params Method parameters
+ */
+ public function __call($name, $params) {
+ $callback = $this->dispatcher->get($name);
+
+ if (is_callable($callback)) {
+ return $this->dispatcher->run($name, $params);
+ }
+
+ $shared = (!empty($params)) ? (bool)$params[0] : true;
+
+ return $this->loader->load($name, $shared);
+ }
+
+ /*** Core Methods ***/
+
+ /**
+ * Initializes the framework.
+ */
+ public function init() {
+ static $initialized = false;
+
+ if ($initialized) {
+ $this->loader->reset();
+ $this->dispatcher->reset();
+ }
+
+ // Register default components
+ $this->loader->register('request', '\flight\net\Request');
+ $this->loader->register('response', '\flight\net\Response');
+ $this->loader->register('router', '\flight\net\Router');
+ $this->loader->register('view', '\flight\template\View', array(), function($view) {
+ $view->path = $this->get('flight.views.path');
+ });
+
+ // Register framework methods
+ $methods = array(
+ 'start','stop','route','halt','error','notFound',
+ 'render','redirect','etag','lastModified','json'
+ );
+ foreach ($methods as $name) {
+ $this->dispatcher->set($name, array($this, '_'.$name));
+ }
+
+ // Default configuration settings
+ $this->set('flight.views.path', './views');
+ $this->set('flight.log_errors', false);
+ $this->set('flight.handle_errors', true);
+
+ $initialized = true;
+ }
+
+ /**
+ * Enables/disables custom error handling.
+ *
+ * @param bool $enabled True or false
+ */
+ public function handleErrors($enabled)
+ {
+ if ($enabled) {
+ set_error_handler(array($this, 'handleError'));
+ set_exception_handler(array($this, 'handleException'));
+ }
+ else {
+ restore_error_handler();
+ restore_exception_handler();
+ }
+ }
+
+ /**
+ * 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 function handleError($errno, $errstr, $errfile, $errline) {
+ if ($errno & error_reporting()) {
+ $this->handleException(new \ErrorException($errstr, $errno, 0, $errfile, $errline));
+ }
+ }
+
+ /**
+ * Custom exception handler. Logs exceptions.
+ *
+ * @param \Exception $e Thrown exception
+ */
+ public function handleException(\Exception $e) {
+ if ($this->get('flight.log_errors')) {
+ error_log($e->getMessage());
+ }
+
+ $this->error($e);
+ }
+
+ /**
+ * Maps a callback to a framework method.
+ *
+ * @param string $name Method name
+ * @param callback $callback Callback function
+ * @throws \Exception If trying to map over a framework method
+ */
+ public function map($name, $callback) {
+ if (method_exists($this, $name)) {
+ throw new \Exception('Cannot override an existing framework method.');
+ }
+
+ $this->dispatcher->set($name, $callback);
+ }
+
+ /**
+ * Registers a class to a framework method.
+ *
+ * @param string $name Method name
+ * @param string $class Class name
+ * @param array $params Class initialization parameters
+ * @param callback $callback Function to call after object instantiation
+ * @throws \Exception If trying to map over a framework method
+ */
+ public function register($name, $class, array $params = array(), $callback = null) {
+ if (method_exists($this, $name)) {
+ throw new \Exception('Cannot override an existing framework method.');
+ }
+
+ $this->loader->register($name, $class, $params, $callback);
+ }
+
+ /**
+ * Adds a pre-filter to a method.
+ *
+ * @param string $name Method name
+ * @param callback $callback Callback function
+ */
+ public function before($name, $callback) {
+ $this->dispatcher->hook($name, 'before', $callback);
+ }
+
+ /**
+ * Adds a post-filter to a method.
+ *
+ * @param string $name Method name
+ * @param callback $callback Callback function
+ */
+ public function after($name, $callback) {
+ $this->dispatcher->hook($name, 'after', $callback);
+ }
+
+ /**
+ * Gets a variable.
+ *
+ * @param string $key Key
+ * @return mixed
+ */
+ public function get($key) {
+ return isset($this->vars[$key]) ? $this->vars[$key] : null;
+ }
+
+ /**
+ * Sets a variable.
+ *
+ * @param mixed $key Key
+ * @param string $value Value
+ */
+ public function set($key, $value = null) {
+ if (is_array($key) || is_object($key)) {
+ foreach ($key as $k => $v) {
+ $this->vars[$k] = $v;
+ }
+ }
+ else {
+ $this->vars[$key] = $value;
+ }
+ }
+
+ /**
+ * Checks if a variable has been set.
+ *
+ * @param string $key Key
+ * @return bool Variable status
+ */
+ public function has($key) {
+ return isset($this->vars[$key]);
+ }
+
+ /**
+ * Unsets a variable. If no key is passed in, clear all variables.
+ *
+ * @param string $key Key
+ */
+ public function clear($key = null) {
+ if (is_null($key)) {
+ $this->vars = array();
+ }
+ else {
+ unset($this->vars[$key]);
+ }
+ }
+
+ /**
+ * Adds a path for class autoloading.
+ *
+ * @param string $dir Directory path
+ */
+ public function path($dir) {
+ $this->loader->addDirectory($dir);
+ }
+
+ /*** Extensible Methods ***/
+
+ /**
+ * Starts the framework.
+ */
+ public function _start() {
+ $dispatched = false;
+
+ // Enable output buffering
+ ob_start();
+
+ // Enable error handling
+ $this->handleErrors($this->get('flight.handle_errors'));
+
+ // Disable caching for AJAX requests
+ if ($this->request()->ajax) {
+ $this->response()->cache(false);
+ }
+
+ // Allow post-filters to run
+ $this->after('start', array($this, 'stop'));
+
+ // Route the request
+ while ($route = $this->router()->route($this->request())) {
+ $params = array_values($route->params);
+
+ $continue = $this->dispatcher->execute(
+ $route->callback,
+ $params
+ );
+
+ $dispatched = true;
+
+ if (!$continue) break;
+
+ $this->router()->next();
+ }
+
+ if (!$dispatched) {
+ $this->notFound();
+ }
+ }
+
+ /**
+ * Stops the framework and outputs the current response.
+ */
+ public function _stop() {
+ $this->response()
+ ->write(ob_get_clean())
+ ->send();
+ }
+
+ /**
+ * Stops processing and returns a given response.
+ *
+ * @param int $code HTTP status code
+ * @param string $message Response message
+ */
+ public function _halt($code = 200, $message = '') {
+ $this->response(false)
+ ->status($code)
+ ->write($message)
+ ->cache(false)
+ ->send();
+ }
+
+ /**
+ * Sends an HTTP 500 response for any errors.
+ *
+ * @param \Exception Thrown exception
+ */
+ public function _error(\Exception $e) {
+ $msg = sprintf('
500 Internal Server Error
'.
+ '%s (%s)
'.
+ '%s
',
+ $e->getMessage(),
+ $e->getCode(),
+ $e->getTraceAsString()
+ );
+
+ try {
+ $this->response(false)
+ ->status(500)
+ ->write($msg)
+ ->send();
+ }
+ catch (\Exception $ex) {
+ exit($msg);
+ }
+ }
+
+ /**
+ * Sends an HTTP 404 response when a URL is not found.
+ */
+ public function _notFound() {
+ $this->response(false)
+ ->status(404)
+ ->write(
+ '404 Not Found
'.
+ 'The page you have requested could not be found.
'.
+ str_repeat(' ', 512)
+ )
+ ->send();
+ }
+
+ /**
+ * Routes a URL to a callback function.
+ *
+ * @param string $pattern URL pattern to match
+ * @param callback $callback Callback function
+ */
+ public function _route($pattern, $callback) {
+ $this->router()->map($pattern, $callback);
+ }
+
+ /**
+ * Redirects the current request to another URL.
+ *
+ * @param string $url URL
+ * @param int $code HTTP status code
+ */
+ public function _redirect($url, $code = 303) {
+ $base = $this->request()->base;
+ if ($base != '/' && strpos($url, '://') === false) {
+ $url = $base.(($url[0] == '/') ? '' : '/').$url;
+ }
+
+ $this->response(false)
+ ->status($code)
+ ->header('Location', $url)
+ ->write($url)
+ ->send();
+ }
+
+ /**
+ * Renders a template.
+ *
+ * @param string $file Template file
+ * @param array $data Template data
+ * @param string $key View variable name
+ */
+ public function _render($file, $data = null, $key = null) {
+ if ($key !== null) {
+ $this->view()->set($key, $this->view()->fetch($file, $data));
+ }
+ else {
+ $this->view()->render($file, $data);
+ }
+ }
+
+ /**
+ * Sends a JSON response.
+ *
+ * @param mixed $data Data to JSON encode
+ */
+ public function _json($data) {
+ $this->response()
+ ->status(200)
+ ->header('Content-Type', 'application/json')
+ ->write(json_encode($data))
+ ->send();
+ }
+
+ /**
+ * Handles ETag HTTP caching.
+ *
+ * @param string $id ETag identifier
+ * @param string $type ETag type
+ */
+ public function _etag($id, $type = 'strong') {
+ $id = (($type === 'weak') ? 'W/' : '').$id;
+
+ $this->response()->header('ETag', $id);
+
+ if ($id === getenv('HTTP_IF_NONE_MATCH')) {
+ $this->halt(304);
+ }
+ }
+
+ /**
+ * Handles last modified HTTP caching.
+ *
+ * @param int $time Unix timestamp
+ */
+ public function _lastModified($time) {
+ $this->response()->header('Last-Modified', date(DATE_RFC1123, $time));
+
+ if ($time === strtotime(getenv('HTTP_IF_MODIFIED_SINCE'))) {
+ $this->halt(304);
+ }
+ }
+}
diff --git a/flight/Flight.php b/flight/Flight.php
index 70aba8b..60407fa 100644
--- a/flight/Flight.php
+++ b/flight/Flight.php
@@ -3,38 +3,19 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
-include __DIR__.'/core/Loader.php';
-include __DIR__.'/core/Dispatcher.php';
-
/**
- * The Flight class represents the framework itself. It is responsible
- * loading an HTTP request, running the assigned services, and generating
- * an HTTP response.
+ * The Flight class is a static representation of the framework.
*/
class Flight {
/**
- * Stored variables.
- *
- * @var array
- */
- protected static $vars = array();
-
- /**
- * Class loader.
+ * Framework engine.
*
* @var object
*/
- protected static $loader;
-
- /**
- * Event dispatcher.
- *
- * @var object
- */
- protected static $dispatcher;
+ private static $engine;
// Don't allow object instantiation
private function __construct() {}
@@ -46,401 +27,21 @@ class Flight {
*
* @param string $name Method name
* @param array $params Method parameters
+ * @return mixed Callback results
*/
public static function __callStatic($name, $params) {
- $callback = self::$dispatcher->get($name);
-
- if (is_callable($callback)) {
- return self::$dispatcher->run($name, $params);
- }
-
- $shared = (!empty($params)) ? (bool)$params[0] : true;
-
- return self::$loader->load($name, $shared);
- }
-
- /*** Core Methods ***/
-
- /**
- * Initializes the framework.
- */
- public static function init() {
- // Handle errors internally
- set_error_handler(array(__CLASS__, 'handleError'));
-
- // Handle exceptions internally
- set_exception_handler(array(__CLASS__, 'handleException'));
-
- // 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();
- }
-
- // 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));
- }
-
- // Default settings
- self::set('flight.views.path', './views');
- self::set('flight.log_errors', false);
-
- // Enable output buffering
- ob_start();
- }
-
- /**
- * 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 ($errno & error_reporting()) {
- static::handleException(new ErrorException($errstr, $errno, 0, $errfile, $errline));
- }
- }
-
- /**
- * Custom exception handler. Logs exceptions.
- *
- * @param Exception $e Thrown 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.
- *
- * @param string $name Method name
- * @param callback $callback Callback function
- * @throws Exception If trying to map over a framework method
- */
- public static function map($name, $callback) {
- if (method_exists(__CLASS__, $name)) {
- throw new Exception('Cannot override an existing framework method.');
- }
-
- self::$dispatcher->set($name, $callback);
- }
-
- /**
- * Registers a class to a framework method.
- *
- * @param string $name Method name
- * @param string $class Class name
- * @param array $params Class initialization parameters
- * @param callback $callback Function to call after object instantiation
- * @throws Exception If trying to map over a framework method
- */
- public static function register($name, $class, array $params = array(), $callback = null) {
- if (method_exists(__CLASS__, $name)) {
- throw new Exception('Cannot override an existing framework method.');
- }
-
- self::$loader->register($name, $class, $params, $callback);
- }
-
- /**
- * Adds a pre-filter to a method.
- *
- * @param string $name Method name
- * @param callback $callback Callback function
- */
- public static function before($name, $callback) {
- self::$dispatcher->hook($name, 'before', $callback);
- }
-
- /**
- * Adds a post-filter to a method.
- *
- * @param string $name Method name
- * @param callback $callback Callback function
- */
- public static function after($name, $callback) {
- self::$dispatcher->hook($name, 'after', $callback);
- }
-
- /**
- * Gets a variable.
- *
- * @param string $key Key
- * @return mixed
- */
- public static function get($key) {
- return isset(self::$vars[$key]) ? self::$vars[$key] : null;
- }
-
- /**
- * Sets a variable.
- *
- * @param mixed $key Key
- * @param string $value Value
- */
- public static function set($key, $value = null) {
- if (is_array($key) || is_object($key)) {
- foreach ($key as $k => $v) {
- self::$vars[$k] = $v;
- }
- }
- else {
- self::$vars[$key] = $value;
- }
- }
-
- /**
- * Checks if a variable has been set.
- *
- * @param string $key Key
- * @return bool Variable status
- */
- public static function has($key) {
- return isset(self::$vars[$key]);
- }
-
- /**
- * Unsets a variable. If no key is passed in, clear all variables.
- *
- * @param string $key Key
- */
- public static function clear($key = null) {
- if (is_null($key)) {
- self::$vars = array();
- }
- else {
- unset(self::$vars[$key]);
- }
- }
-
- /**
- * Adds a path for class autoloading.
- *
- * @param string $dir Directory path
- */
- public static function path($dir) {
- self::$loader->addDirectory($dir);
- }
-
- /*** Extensible Methods ***/
-
- /**
- * Starts the framework.
- */
- public static function _start() {
- $dispatched = false;
-
- // Route the request
- while ($route = self::router()->route(self::request())) {
- $params = array_values($route->params);
- $continue = self::$dispatcher->execute(
- $route->callback,
- $params
- );
- $dispatched = true;
-
- if ($continue) {
- self::router()->next();
- }
- else {
- break;
- }
- }
-
- if (!$dispatched) {
- self::notFound();
- }
-
- // Disable caching for AJAX requests
- if (self::request()->ajax) {
- self::response()->cache(false);
- }
-
- // Allow post-filters to run
- self::after('start', array(__CLASS__, 'stop'));
- }
-
- /**
- * Stops the framework and outputs the current response.
- */
- public static function _stop() {
- self::response()
- ->write(ob_get_clean())
- ->send();
- }
-
- /**
- * Stops processing and returns a given response.
- *
- * @param int $code HTTP status code
- * @param string $message Response message
- */
- public static function _halt($code = 200, $message = '') {
- self::response(false)
- ->status($code)
- ->write($message)
- ->cache(false)
- ->send();
- }
-
- /**
- * Sends an HTTP 500 response for any errors.
- *
- * @param \Exception Thrown exception
- */
- public static function _error(Exception $e) {
- $msg = sprintf('500 Internal Server Error
'.
- '%s (%s)
'.
- '%s
',
- $e->getMessage(),
- $e->getCode(),
- $e->getTraceAsString()
- );
-
- try {
- self::response(false)
- ->status(500)
- ->write($msg)
- ->send();
- }
- catch (Exception $ex) {
- exit($msg);
- }
- }
-
- /**
- * Sends an HTTP 404 response when a URL is not found.
- */
- public static function _notFound() {
- self::response(false)
- ->status(404)
- ->write(
- '404 Not Found
'.
- 'The page you have requested could not be found.
'.
- str_repeat(' ', 512)
- )
- ->send();
- }
-
- /**
- * Routes a URL to a callback function.
- *
- * @param string $pattern URL pattern to match
- * @param callback $callback Callback function
- */
- public static function _route($pattern, $callback) {
- self::router()->map($pattern, $callback);
- }
-
- /**
- * Redirects the current request to another URL.
- *
- * @param string $url URL
- * @param int $code HTTP status code
- */
- public static function _redirect($url, $code = 303) {
- $base = self::request()->base;
- if ($base != '/' && strpos($url, '://') === false) {
- $url = $base.(($url[0] == '/') ? '' : '/').$url;
- }
-
- self::response(false)
- ->status($code)
- ->header('Location', $url)
- ->write($url)
- ->send();
- }
+ static $initialized = false;
- /**
- * Renders a template.
- *
- * @param string $file Template file
- * @param array $data Template data
- * @param string $key View variable name
- */
- public static function _render($file, $data = null, $key = null) {
- if ($key !== null) {
- self::view()->set($key, self::view()->fetch($file, $data));
- }
- else {
- self::view()->render($file, $data);
- }
- }
+ if (!$initialized) {
+ require_once __DIR__.'/autoload.php';
- /**
- * Sends a JSON response.
- *
- * @param mixed $data Data to JSON encode
- */
- public static function _json($data) {
- self::response()
- ->status(200)
- ->header('Content-Type', 'application/json')
- ->write(json_encode($data))
- ->send();
- }
+ self::$engine = new \flight\Engine();
+ self::$engine->init();
- /**
- * Handles ETag HTTP caching.
- *
- * @param string $id ETag identifier
- * @param string $type ETag type
- */
- public static function _etag($id, $type = 'strong') {
- $id = (($type === 'weak') ? 'W/' : '').$id;
-
- self::response()->header('ETag', $id);
-
- if ($id === getenv('HTTP_IF_NONE_MATCH')) {
- self::halt(304);
+ $initialized = true;
}
- }
- /**
- * Handles last modified HTTP caching.
- *
- * @param int $time Unix timestamp
- */
- public static function _lastModified($time) {
- self::response()->header('Last-Modified', date(DATE_RFC1123, $time));
-
- if ($time === strtotime(getenv('HTTP_IF_MODIFIED_SINCE'))) {
- self::halt(304);
- }
+ return \flight\core\Dispatcher::invokeMethod(array(self::$engine, $name), $params);
}
}
-// Initialize the framework on include
-Flight::init();
-?>
diff --git a/flight/autoload.php b/flight/autoload.php
new file mode 100644
index 0000000..ec082f6
--- /dev/null
+++ b/flight/autoload.php
@@ -0,0 +1,11 @@
+
+ * @license MIT, http://flightphp.com/license
+ */
+
+require_once __DIR__.'/core/Loader.php';
+
+\flight\core\Loader::autoload(true, dirname(__DIR__));
diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php
index 8817af7..10aa004 100644
--- a/flight/core/Dispatcher.php
+++ b/flight/core/Dispatcher.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
namespace flight\core;
@@ -220,4 +220,3 @@ class Dispatcher {
$this->filters = array();
}
}
-?>
\ No newline at end of file
diff --git a/flight/core/Loader.php b/flight/core/Loader.php
index 7ecfd75..2b79151 100644
--- a/flight/core/Loader.php
+++ b/flight/core/Loader.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
namespace flight\core;
@@ -34,7 +34,7 @@ class Loader {
*
* @var array
*/
- protected $dirs = array();
+ protected static $dirs = array();
/**
* Registers a class.
@@ -131,33 +131,32 @@ class Loader {
}
/**
- * Adds a directory for autoloading classes.
- *
- * @param mixed $dir Directory path
+ * Resets the object to the initial state.
*/
- public function addDirectory($dir) {
- if (is_array($dir) || is_object($dir)) {
- foreach ($dir as $value) {
- $this->dirs[] = $value;
- }
- }
- else if (is_string($dir)) {
- $this->dirs[] = $dir;
- }
+ public function reset() {
+ $this->classes = array();
+ $this->instances = array();
}
- /**
- * Starts autoloader.
- */
- public function start() {
- spl_autoload_register(array($this, 'autoload'));
- }
+ /*** Autoloading Functions ***/
/**
- * Stops autoloading.
+ * Starts/stops autoloader.
+ *
+ * @param bool $enabled Enable/disable autoloading
+ * @param mixed $dirs Autoload directories
*/
- public function stop() {
- spl_autoload_unregister(array($this, 'autoload'));
+ public static function autoload($enabled = true, $dirs = array()) {
+ if ($enabled) {
+ spl_autoload_register(array(__CLASS__, 'loadClass'));
+ }
+ else {
+ spl_autoload_unregister(array(__CLASS__, 'loadClass'));
+ }
+
+ if (!empty($dirs)) {
+ self::addDirectory($dirs);
+ }
}
/**
@@ -166,10 +165,10 @@ class Loader {
* @param string $class Class name
* @throws \Exception If class not found
*/
- public function autoload($class) {
+ public static function loadClass($class) {
$class_file = str_replace('\\', '/', str_replace('_', '/', $class)).'.php';
- foreach ($this->dirs as $dir) {
+ foreach (self::$dirs as $dir) {
$file = $dir.'/'.$class_file;
if (file_exists($file)) {
require $file;
@@ -186,12 +185,18 @@ class Loader {
}
/**
- * Resets the object to the initial state.
+ * Adds a directory for autoloading classes.
+ *
+ * @param mixed $dir Directory path
*/
- public function reset() {
- $this->classes = array();
- $this->instances = array();
- $this->dirs = array();
+ public static function addDirectory($dir) {
+ if (is_array($dir) || is_object($dir)) {
+ foreach ($dir as $value) {
+ self::$dirs[] = $value;
+ }
+ }
+ else if (is_string($dir)) {
+ self::$dirs[] = $dir;
+ }
}
}
-?>
\ No newline at end of file
diff --git a/flight/net/Request.php b/flight/net/Request.php
index c3da429..c003513 100644
--- a/flight/net/Request.php
+++ b/flight/net/Request.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
namespace flight\net;
@@ -136,4 +136,3 @@ class Request {
return '';
}
}
-?>
diff --git a/flight/net/Response.php b/flight/net/Response.php
index 70888de..4ce1d00 100644
--- a/flight/net/Response.php
+++ b/flight/net/Response.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
namespace flight\net;
@@ -193,4 +193,4 @@ class Response {
exit($this->body);
}
}
-?>
+
diff --git a/flight/net/Route.php b/flight/net/Route.php
index b4dba0c..4007e35 100644
--- a/flight/net/Route.php
+++ b/flight/net/Route.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
namespace flight\net;
diff --git a/flight/net/Router.php b/flight/net/Router.php
index 606b89f..c04b5af 100644
--- a/flight/net/Router.php
+++ b/flight/net/Router.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
namespace flight\net;
@@ -67,7 +67,7 @@ class Router {
* Routes the current request.
*
* @param Request $request Request object
- * @return callable|boolean Matched callback function or false if not found
+ * @return Route Matching route
*/
public function route(Request $request) {
while ($route = $this->current()) {
@@ -105,4 +105,4 @@ class Router {
$this->index = 0;
}
}
-?>
+
diff --git a/flight/template/View.php b/flight/template/View.php
index 1f44a64..a9d4052 100644
--- a/flight/template/View.php
+++ b/flight/template/View.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
namespace flight\template;
@@ -162,4 +162,4 @@ class View {
echo htmlentities($str);
}
}
-?>
+
diff --git a/flight/util/Collection.php b/flight/util/Collection.php
index eae2b28..196ea66 100644
--- a/flight/util/Collection.php
+++ b/flight/util/Collection.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
namespace flight\util;
@@ -201,4 +201,3 @@ class Collection implements \ArrayAccess, \Iterator, \Countable {
$this->data = array();
}
}
-?>
diff --git a/tests/AutoloadTest.php b/tests/AutoloadTest.php
index 47007e2..6125110 100644
--- a/tests/AutoloadTest.php
+++ b/tests/AutoloadTest.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
@@ -12,7 +12,6 @@ require_once __DIR__.'/../flight/Flight.php';
class AutoloadTest extends PHPUnit_Framework_TestCase
{
function setUp() {
- Flight::init();
}
// Autoload a class
@@ -25,6 +24,7 @@ class AutoloadTest extends PHPUnit_Framework_TestCase
$test = Flight::test();
+ $this->assertTrue(sizeof($loaders) > 0);
$this->assertTrue(is_object($test));
$this->assertEquals('TestClass', get_class($test));
}
diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php
index a6a6ffe..3222a96 100644
--- a/tests/DispatcherTest.php
+++ b/tests/DispatcherTest.php
@@ -3,11 +3,10 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
-require_once __DIR__.'/../flight/core/Dispatcher.php';
require_once __DIR__.'/classes/Hello.php';
class DispatcherTest extends PHPUnit_Framework_TestCase
diff --git a/tests/FilterTest.php b/tests/FilterTest.php
index d3f402b..2855272 100644
--- a/tests/FilterTest.php
+++ b/tests/FilterTest.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
@@ -12,7 +12,6 @@ require_once __DIR__.'/../flight/Flight.php';
class FilterTest extends PHPUnit_Framework_TestCase
{
function setUp() {
- Flight::init();
}
// Run before and after filters
diff --git a/tests/LoaderTest.php b/tests/LoaderTest.php
index 09709a7..657dd80 100644
--- a/tests/LoaderTest.php
+++ b/tests/LoaderTest.php
@@ -3,11 +3,10 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
-require_once __DIR__.'/../flight/core/Loader.php';
class LoaderTest extends PHPUnit_Framework_TestCase
{
@@ -18,8 +17,7 @@ class LoaderTest extends PHPUnit_Framework_TestCase
function setUp(){
$this->loader = new \flight\core\Loader();
- $this->loader->start();
- $this->loader->addDirectory(__DIR__.'/classes');
+ $this->loader->autoload(true, __DIR__.'/classes');
}
// Autoload a class
diff --git a/tests/MapTest.php b/tests/MapTest.php
index 0ce9277..60f6d7d 100644
--- a/tests/MapTest.php
+++ b/tests/MapTest.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
@@ -13,7 +13,6 @@ require_once __DIR__.'/classes/Hello.php';
class MapTest extends PHPUnit_Framework_TestCase
{
function setUp(){
- Flight::init();
}
// Map a closure
diff --git a/tests/RegisterTest.php b/tests/RegisterTest.php
index a3b184e..885215e 100644
--- a/tests/RegisterTest.php
+++ b/tests/RegisterTest.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
@@ -12,7 +12,6 @@ require_once __DIR__.'/../flight/Flight.php';
class RegisterTest extends PHPUnit_Framework_TestCase
{
function setUp() {
- Flight::init();
}
// Register a class
diff --git a/tests/RenderTest.php b/tests/RenderTest.php
index 063e625..719b2df 100644
--- a/tests/RenderTest.php
+++ b/tests/RenderTest.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
@@ -12,7 +12,6 @@ require_once __DIR__.'/../flight/Flight.php';
class RenderTest extends PHPUnit_Framework_TestCase
{
function setUp(){
- Flight::init();
Flight::set('flight.views.path', __DIR__.'/views');
}
diff --git a/tests/RequestTest.php b/tests/RequestTest.php
index 8c47cfa..807f7ac 100644
--- a/tests/RequestTest.php
+++ b/tests/RequestTest.php
@@ -3,14 +3,16 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
-require_once __DIR__.'/../flight/net/Request.php';
class RequestTest extends PHPUnit_Framework_TestCase
{
+ /**
+ * @var \flight\net\Request
+ */
private $request;
function setUp() {
diff --git a/tests/RouterTest.php b/tests/RouterTest.php
index f50b47b..52da466 100644
--- a/tests/RouterTest.php
+++ b/tests/RouterTest.php
@@ -3,12 +3,10 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
-require_once __DIR__.'/../flight/net/Router.php';
-require_once __DIR__.'/../flight/net/Request.php';
class RouterTest extends PHPUnit_Framework_TestCase
{
diff --git a/tests/VariableTest.php b/tests/VariableTest.php
index ae5d9e2..8bee67a 100644
--- a/tests/VariableTest.php
+++ b/tests/VariableTest.php
@@ -3,7 +3,7 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
@@ -12,7 +12,6 @@ require_once __DIR__.'/../flight/Flight.php';
class VariableTest extends PHPUnit_Framework_TestCase
{
function setUp() {
- Flight::init();
}
// Set and get a variable
diff --git a/tests/ViewTest.php b/tests/ViewTest.php
index 141eba7..db603e3 100644
--- a/tests/ViewTest.php
+++ b/tests/ViewTest.php
@@ -3,11 +3,10 @@
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao
- * @license http://www.opensource.org/licenses/mit-license.php
+ * @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
-require_once __DIR__.'/../flight/template/View.php';
class ViewTest extends PHPUnit_Framework_TestCase
{