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.
*
* @param string $name Method name
* @param array $args Method parameters
* @param array $params Method parameters
*/
public static function __callStatic($name, $params) {
$callback = self::$dispatcher->get($name);
@ -137,7 +137,7 @@ class Flight {
/**
* Custom exception handler. Logs exceptions.
*
* @param object $e Exception
* @param Exception $e Thrown exception
*/
public static function handleException(Exception $e) {
if (self::get('flight.log_errors')) {
@ -151,6 +151,7 @@ class Flight {
*
* @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)) {
@ -167,6 +168,7 @@ class Flight {
* @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)) {
@ -301,7 +303,7 @@ class Flight {
* Stops processing and returns a given response.
*
* @param int $code HTTP status code
* @param int $message Response message
* @param string $message Response message
*/
public static function _halt($code = 200, $message = '') {
self::response(false)
@ -314,7 +316,7 @@ class Flight {
/**
* Sends an HTTP 500 response for any errors.
*
* @param object $e Exception
* @param \Exception Thrown exception
*/
public static function _error(Exception $e) {
$msg = sprintf('<h1>500 Internal Server Error</h1>'.
@ -364,6 +366,7 @@ class Flight {
* 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;
@ -418,8 +421,7 @@ class Flight {
self::response()->header('ETag', $id);
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
$_SERVER['HTTP_IF_NONE_MATCH'] === $id) {
if ($id === getenv('HTTP_IF_NONE_MATCH')) {
self::halt(304);
}
}
@ -432,8 +434,7 @@ class Flight {
public static function _lastModified($time) {
self::response()->header('Last-Modified', date(DATE_RFC1123, $time));
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $time) {
if ($time === strtotime(getenv('HTTP_IF_MODIFIED_SINCE'))) {
self::halt(304);
}
}

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

@ -64,6 +64,7 @@ class Loader {
*
* @param string $name Method name
* @param bool $shared Shared instance
* @return object Class instance
*/
public function load($name, $shared = true) {
if (isset($this->classes[$name])) {
@ -107,6 +108,7 @@ class Loader {
*
* @param string $class Class name
* @param array $params Class initialization parameters
* @return object Class instance
*/
public function newInstance($class, array $params = array()) {
switch (count($params)) {
@ -123,7 +125,7 @@ class Loader {
case 5:
return new $class($params[0], $params[1], $params[2], $params[3], $params[4]);
default:
$refClass = new ReflectionClass($class);
$refClass = new \ReflectionClass($class);
return $refClass->newInstanceArgs($params);
}
}
@ -161,6 +163,7 @@ class Loader {
* Autoloads classes.
*
* @param string $class Class name
* @throws \Exception If class not found
*/
public function autoload($class) {
$class_file = str_replace('\\', '/', str_replace('_', '/', $class)).'.php';
@ -177,7 +180,7 @@ class Loader {
$loaders = spl_autoload_functions();
$loader = array_pop($loaders);
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
if (empty($config)) {
$config = array(
'url' => $_SERVER['REQUEST_URI'],
'base' => str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])),
'method' => $_SERVER['REQUEST_METHOD'],
'referrer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
'url' => getenv('REQUEST_URI') ?: '/',
'base' => str_replace('\\', '/', dirname(getenv('SCRIPT_NAME'))),
'method' => getenv('REQUEST_METHOD') ?: 'GET',
'referrer' => getenv('HTTP_REFERER') ?: '',
'ip' => $this->getIpAddress(),
'ajax' => isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') : false,
'scheme' => $_SERVER['SERVER_PROTOCOL'],
'user_agent' => $_SERVER['HTTP_USER_AGENT'],
'ajax' => getenv('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest',
'scheme' => getenv('SERVER_PROTOCOL') ?: 'HTTP/1.1',
'user_agent' => getenv('HTTP_USER_AGENT') ?: '',
'body' => file_get_contents('php://input'),
'type' => isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '',
'length' => isset($_SERVER['CONTENT_LENGTH']) ? $_SERVER['CONTENT_LENGTH'] : 0,
'type' => getenv('CONTENT_TYPE') ?: '',
'length' => getenv('CONTENT_LENGTH') ?: 0,
'query' => new Collection($_GET),
'data' => new Collection($_POST),
'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.
*
* @param int $code HTTP status code.
* @return object Self reference
* @throws \Exception If invalid status code
*/
public function status($code) {
if (array_key_exists($code, self::$codes)) {
@ -82,7 +84,7 @@ class Response {
header(
sprintf(
'%s %d %s',
(isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'),
getenv('SERVER_PROTOCOL') ?: 'HTTP/1.1',
$code,
self::$codes[$code]),
true,
@ -100,8 +102,9 @@ class 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
* @return object Self reference
*/
public function header($name, $value = null) {
if (is_array($name)) {
@ -120,6 +123,7 @@ class Response {
* Writes content to the response body.
*
* @param string $str Response content
* @return object Self reference
*/
public function write($str) {
$this->body .= $str;
@ -129,6 +133,8 @@ class Response {
/**
* Clears the response.
*
* @return object Self reference
*/
public function clear() {
$this->headers = array();
@ -142,6 +148,7 @@ class Response {
* Sets caching headers for the response.
*
* @param int|string $expires Expiration time
* @return object Self reference
*/
public function cache($expires) {
if ($expires === false) {

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

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

Loading…
Cancel
Save