Code cleanup

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

@ -45,7 +45,7 @@ class Flight {
* Handles calls to static methods. * Handles calls to static methods.
* *
* @param string $name Method name * @param string $name Method name
* @param array $args Method parameters * @param array $params Method parameters
*/ */
public static function __callStatic($name, $params) { public static function __callStatic($name, $params) {
$callback = self::$dispatcher->get($name); $callback = self::$dispatcher->get($name);
@ -137,7 +137,7 @@ class Flight {
/** /**
* Custom exception handler. Logs exceptions. * Custom exception handler. Logs exceptions.
* *
* @param object $e Exception * @param Exception $e Thrown exception
*/ */
public static function handleException(Exception $e) { public static function handleException(Exception $e) {
if (self::get('flight.log_errors')) { if (self::get('flight.log_errors')) {
@ -151,6 +151,7 @@ class Flight {
* *
* @param string $name Method name * @param string $name Method name
* @param callback $callback Callback function * @param callback $callback Callback function
* @throws Exception If trying to map over a framework method
*/ */
public static function map($name, $callback) { public static function map($name, $callback) {
if (method_exists(__CLASS__, $name)) { if (method_exists(__CLASS__, $name)) {
@ -167,6 +168,7 @@ class Flight {
* @param string $class Class name * @param string $class Class name
* @param array $params Class initialization parameters * @param array $params Class initialization parameters
* @param callback $callback Function to call after object instantiation * @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) { public static function register($name, $class, array $params = array(), $callback = null) {
if (method_exists(__CLASS__, $name)) { if (method_exists(__CLASS__, $name)) {
@ -301,7 +303,7 @@ class Flight {
* Stops processing and returns a given response. * Stops processing and returns a given response.
* *
* @param int $code HTTP status code * @param int $code HTTP status code
* @param int $message Response message * @param string $message Response message
*/ */
public static function _halt($code = 200, $message = '') { public static function _halt($code = 200, $message = '') {
self::response(false) self::response(false)
@ -314,7 +316,7 @@ class Flight {
/** /**
* Sends an HTTP 500 response for any errors. * Sends an HTTP 500 response for any errors.
* *
* @param object $e Exception * @param \Exception Thrown exception
*/ */
public static function _error(Exception $e) { public static function _error(Exception $e) {
$msg = sprintf('<h1>500 Internal Server Error</h1>'. $msg = sprintf('<h1>500 Internal Server Error</h1>'.
@ -364,6 +366,7 @@ class Flight {
* Redirects the current request to another URL. * Redirects the current request to another URL.
* *
* @param string $url URL * @param string $url URL
* @param int $code HTTP status code
*/ */
public static function _redirect($url, $code = 303) { public static function _redirect($url, $code = 303) {
$base = self::request()->base; $base = self::request()->base;
@ -418,8 +421,7 @@ class Flight {
self::response()->header('ETag', $id); self::response()->header('ETag', $id);
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && if ($id === getenv('HTTP_IF_NONE_MATCH')) {
$_SERVER['HTTP_IF_NONE_MATCH'] === $id) {
self::halt(304); self::halt(304);
} }
} }
@ -432,8 +434,7 @@ class Flight {
public static function _lastModified($time) { public static function _lastModified($time) {
self::response()->header('Last-Modified', date(DATE_RFC1123, $time)); self::response()->header('Last-Modified', date(DATE_RFC1123, $time));
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && if ($time === strtotime(getenv('HTTP_IF_MODIFIED_SINCE'))) {
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $time) {
self::halt(304); self::halt(304);
} }
} }

@ -34,6 +34,7 @@ class Dispatcher {
* *
* @param string $name Event name * @param string $name Event name
* @param array $params Callback parameters * @param array $params Callback parameters
* @return string Output of callback
*/ */
public function run($name, $params) { public function run($name, $params) {
$output = ''; $output = '';
@ -68,7 +69,7 @@ class Dispatcher {
* Gets an assigned callback. * Gets an assigned callback.
* *
* @param string $name Event name * @param string $name Event name
* @param callback $callback Callback function * @return callback $callback Callback function
*/ */
public function get($name) { public function get($name) {
return isset($this->events[$name]) ? $this->events[$name] : null; return isset($this->events[$name]) ? $this->events[$name] : null;
@ -104,7 +105,7 @@ class Dispatcher {
/** /**
* Hooks a callback to an event. * Hooks a callback to an event.
* *
* @param string $event Event name * @param string $name Event name
* @param string $type Filter type * @param string $type Filter type
* @param callback $callback Callback function * @param callback $callback Callback function
*/ */
@ -116,8 +117,8 @@ class Dispatcher {
* Executes a chain of method filters. * Executes a chain of method filters.
* *
* @param array $filters Chain of filters * @param array $filters Chain of filters
* @param reference $params Method parameters * @param object $params Method parameters
* @param reference $output Method output * @param object $output Method output
*/ */
public function filter($filters, &$params, &$output) { public function filter($filters, &$params, &$output) {
$args = array(&$params, &$output); $args = array(&$params, &$output);
@ -146,7 +147,8 @@ class Dispatcher {
* Calls a function. * Calls a function.
* *
* @param string $func Name of function to call * @param string $func Name of function to call
* @param array $params Function parameters * @param array $params Function parameters
* @return mixed Function results
*/ */
public static function callFunction($func, array &$params = array()) { public static function callFunction($func, array &$params = array()) {
switch (count($params)) { switch (count($params)) {
@ -172,6 +174,7 @@ class Dispatcher {
* *
* @param mixed $func Class method * @param mixed $func Class method
* @param array $params Class method parameters * @param array $params Class method parameters
* @return mixed Function results
*/ */
public static function invokeMethod($func, array &$params = array()) { public static function invokeMethod($func, array &$params = array()) {
list($class, $method) = $func; list($class, $method) = $func;
@ -180,17 +183,29 @@ class Dispatcher {
switch (count($params)) { switch (count($params)) {
case 0: case 0:
return ($instance) ? $class->$method() : $class::$method(); return ($instance) ?
$class->$method() :
$class::$method();
case 1: case 1:
return ($instance) ? $class->$method($params[0]) : $class::$method($params[0]); return ($instance) ?
$class->$method($params[0]) :
$class::$method($params[0]);
case 2: case 2:
return ($instance) ? $class->$method($params[0], $params[1]) : $class::$method($params[0], $params[1]); return ($instance) ?
$class->$method($params[0], $params[1]) :
$class::$method($params[0], $params[1]);
case 3: case 3:
return ($instance) ? $class->$method($params[0], $params[1], $params[2]) : $class::$method($params[0], $params[1], $params[2]); return ($instance) ?
$class->$method($params[0], $params[1], $params[2]) :
$class::$method($params[0], $params[1], $params[2]);
case 4: case 4:
return ($instance) ? $class->$method($params[0], $params[1], $params[2], $params[3]) : $class::$method($params[0], $params[1], $params[2], $params[3]); return ($instance) ?
$class->$method($params[0], $params[1], $params[2], $params[3]) :
$class::$method($params[0], $params[1], $params[2], $params[3]);
case 5: case 5:
return ($instance) ? $class->$method($params[0], $params[1], $params[2], $params[3], $params[4]) : $class::$method($params[0], $params[1], $params[2], $params[3], $params[4]); return ($instance) ?
$class->$method($params[0], $params[1], $params[2], $params[3], $params[4]) :
$class::$method($params[0], $params[1], $params[2], $params[3], $params[4]);
default: default:
return call_user_func_array($func, $params); return call_user_func_array($func, $params);
} }

@ -64,6 +64,7 @@ class Loader {
* *
* @param string $name Method name * @param string $name Method name
* @param bool $shared Shared instance * @param bool $shared Shared instance
* @return object Class instance
*/ */
public function load($name, $shared = true) { public function load($name, $shared = true) {
if (isset($this->classes[$name])) { if (isset($this->classes[$name])) {
@ -107,6 +108,7 @@ class Loader {
* *
* @param string $class Class name * @param string $class Class name
* @param array $params Class initialization parameters * @param array $params Class initialization parameters
* @return object Class instance
*/ */
public function newInstance($class, array $params = array()) { public function newInstance($class, array $params = array()) {
switch (count($params)) { switch (count($params)) {
@ -123,7 +125,7 @@ class Loader {
case 5: case 5:
return new $class($params[0], $params[1], $params[2], $params[3], $params[4]); return new $class($params[0], $params[1], $params[2], $params[3], $params[4]);
default: default:
$refClass = new ReflectionClass($class); $refClass = new \ReflectionClass($class);
return $refClass->newInstanceArgs($params); return $refClass->newInstanceArgs($params);
} }
} }
@ -161,6 +163,7 @@ class Loader {
* Autoloads classes. * Autoloads classes.
* *
* @param string $class Class name * @param string $class Class name
* @throws \Exception If class not found
*/ */
public function autoload($class) { public function autoload($class) {
$class_file = str_replace('\\', '/', str_replace('_', '/', $class)).'.php'; $class_file = str_replace('\\', '/', str_replace('_', '/', $class)).'.php';
@ -177,7 +180,7 @@ class Loader {
$loaders = spl_autoload_functions(); $loaders = spl_autoload_functions();
$loader = array_pop($loaders); $loader = array_pop($loaders);
if (is_array($loader) && $loader[0] == __CLASS__ && $loader[1] == __FUNCTION__) { if (is_array($loader) && $loader[0] == __CLASS__ && $loader[1] == __FUNCTION__) {
throw new Exception('Unable to load file: '.$class_file); throw new \Exception('Unable to load file: '.$class_file);
} }
} }
} }

@ -42,17 +42,17 @@ class Request {
// Default properties // Default properties
if (empty($config)) { if (empty($config)) {
$config = array( $config = array(
'url' => $_SERVER['REQUEST_URI'], 'url' => getenv('REQUEST_URI') ?: '/',
'base' => str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])), 'base' => str_replace('\\', '/', dirname(getenv('SCRIPT_NAME'))),
'method' => $_SERVER['REQUEST_METHOD'], 'method' => getenv('REQUEST_METHOD') ?: 'GET',
'referrer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', 'referrer' => getenv('HTTP_REFERER') ?: '',
'ip' => $this->getIpAddress(), 'ip' => $this->getIpAddress(),
'ajax' => isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') : false, 'ajax' => getenv('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest',
'scheme' => $_SERVER['SERVER_PROTOCOL'], 'scheme' => getenv('SERVER_PROTOCOL') ?: 'HTTP/1.1',
'user_agent' => $_SERVER['HTTP_USER_AGENT'], 'user_agent' => getenv('HTTP_USER_AGENT') ?: '',
'body' => file_get_contents('php://input'), 'body' => file_get_contents('php://input'),
'type' => isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '', 'type' => getenv('CONTENT_TYPE') ?: '',
'length' => isset($_SERVER['CONTENT_LENGTH']) ? $_SERVER['CONTENT_LENGTH'] : 0, 'length' => getenv('CONTENT_LENGTH') ?: 0,
'query' => new Collection($_GET), 'query' => new Collection($_GET),
'data' => new Collection($_POST), 'data' => new Collection($_POST),
'cookies' => new Collection($_COOKIE), 'cookies' => new Collection($_COOKIE),
@ -129,6 +129,8 @@ class Request {
} }
} }
} }
return $_SERVER['REMOTE_ADDR'];
} }
} }
?> ?>

@ -65,6 +65,8 @@ class Response {
* Sets the HTTP status of the response. * Sets the HTTP status of the response.
* *
* @param int $code HTTP status code. * @param int $code HTTP status code.
* @return object Self reference
* @throws \Exception If invalid status code
*/ */
public function status($code) { public function status($code) {
if (array_key_exists($code, self::$codes)) { if (array_key_exists($code, self::$codes)) {
@ -82,7 +84,7 @@ class Response {
header( header(
sprintf( sprintf(
'%s %d %s', '%s %d %s',
(isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'), getenv('SERVER_PROTOCOL') ?: 'HTTP/1.1',
$code, $code,
self::$codes[$code]), self::$codes[$code]),
true, true,
@ -100,8 +102,9 @@ class Response {
/** /**
* Adds a header to the response. * Adds a header to the response.
* *
* @param string|array $key Header name or array of names and values * @param string|array $name Header name or array of names and values
* @param string $value Header value * @param string $value Header value
* @return object Self reference
*/ */
public function header($name, $value = null) { public function header($name, $value = null) {
if (is_array($name)) { if (is_array($name)) {
@ -120,6 +123,7 @@ class Response {
* Writes content to the response body. * Writes content to the response body.
* *
* @param string $str Response content * @param string $str Response content
* @return object Self reference
*/ */
public function write($str) { public function write($str) {
$this->body .= $str; $this->body .= $str;
@ -129,6 +133,8 @@ class Response {
/** /**
* Clears the response. * Clears the response.
*
* @return object Self reference
*/ */
public function clear() { public function clear() {
$this->headers = array(); $this->headers = array();
@ -142,6 +148,7 @@ class Response {
* Sets caching headers for the response. * Sets caching headers for the response.
* *
* @param int|string $expires Expiration time * @param int|string $expires Expiration time
* @return object Self reference
*/ */
public function cache($expires) { public function cache($expires) {
if ($expires === false) { if ($expires === false) {

@ -75,6 +75,7 @@ class Router {
* *
* @param string $pattern URL pattern * @param string $pattern URL pattern
* @param string $url Requested URL * @param string $url Requested URL
* @return boolean Match status
*/ */
public function match($pattern, $url) { public function match($pattern, $url) {
$ids = array(); $ids = array();
@ -116,8 +117,8 @@ class Router {
/** /**
* Routes the current request. * Routes the current request.
* *
* @param object $request Request object * @param Request $request Request object
* @return callable Matched callback function * @return callable|boolean Matched callback function or false if not found
*/ */
public function route(Request $request) { public function route(Request $request) {
$this->matched = null; $this->matched = null;

@ -68,6 +68,7 @@ class View {
* Checks if a template variable is set. * Checks if a template variable is set.
* *
* @param string $key Key * @param string $key Key
* @return boolean If key exists
*/ */
public function has($key) { public function has($key) {
return isset($this->vars[$key]); return isset($this->vars[$key]);
@ -92,6 +93,7 @@ class View {
* *
* @param string $file Template file * @param string $file Template file
* @param array $data Template data * @param array $data Template data
* @throws \Exception If template not found
*/ */
public function render($file, $data = null) { public function render($file, $data = null) {
$template = $this->getTemplate($file); $template = $this->getTemplate($file);
@ -114,6 +116,7 @@ class View {
* *
* @param string $file Template file * @param string $file Template file
* @param array $data Template data * @param array $data Template data
* @return string Output of template
*/ */
public function fetch($file, $data = null) { public function fetch($file, $data = null) {
ob_start(); ob_start();

Loading…
Cancel
Save