mirror of https://github.com/flightphp/core
parent
8af17f2215
commit
e2f1dc99bb
@ -0,0 +1,171 @@
|
||||
<?php
|
||||
/**
|
||||
* Flight: An extensible micro-framework.
|
||||
*
|
||||
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
|
||||
* @license http://www.opensource.org/licenses/mit-license.php
|
||||
*/
|
||||
|
||||
namespace flight\core;
|
||||
|
||||
/**
|
||||
* The Dispatcher class is responsible for dispatching events. Events
|
||||
* are simply aliases for class methods or functions. The Dispatcher
|
||||
* allows you to hook other function to an event that can modify the
|
||||
* input parameters and/or the output.
|
||||
*/
|
||||
class Dispatcher {
|
||||
/**
|
||||
* Mapped events.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $events = array();
|
||||
|
||||
/**
|
||||
* Method filters.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $filters = array();
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
/**
|
||||
* Dispatches an event.
|
||||
*
|
||||
* @param string $name Event name
|
||||
* @param array $params Callback parameters
|
||||
*/
|
||||
public function run($name, $params) {
|
||||
$output = '';
|
||||
|
||||
// Run pre-filters
|
||||
if (!empty($this->filters[$name]['before'])) {
|
||||
$this->filter($this->filters[$name]['before'], $params, $output);
|
||||
}
|
||||
|
||||
// Run requested method
|
||||
$output = $this->execute($this->get($name), $params);
|
||||
|
||||
// Run post-filters
|
||||
if (!empty($this->filters[$name]['after'])) {
|
||||
$this->filter($this->filters[$name]['after'], $params, $output);
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a callback to an event.
|
||||
*
|
||||
* @param string $name Event name
|
||||
* @param callback $callback Callback function
|
||||
*/
|
||||
public function set($name, $callback) {
|
||||
$this->events[$name] = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an assigned callback.
|
||||
*
|
||||
* @param string $name Event name
|
||||
* @param callback $callback Callback function
|
||||
*/
|
||||
public function get($name) {
|
||||
return $this->events[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Hooks a callback to an event.
|
||||
*
|
||||
* @param string $event Event name
|
||||
* @param string $type Filter type
|
||||
* @param callback $callback Callback function
|
||||
*/
|
||||
public function hook($name, $type, $callback) {
|
||||
$this->filters[$name][$type][] = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a chain of method filters.
|
||||
*
|
||||
* @param array $filters Chain of filters
|
||||
* @param reference $data Method parameters or method output
|
||||
*/
|
||||
public function filter($filters, &$params, &$output) {
|
||||
$args = array(&$params, &$output);
|
||||
foreach ($filters as $callback) {
|
||||
$continue = $this->execute($callback, $args);
|
||||
if ($continue === false) break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a callback function.
|
||||
*
|
||||
* @param callback $callback Callback function
|
||||
* @param array $params Function parameters
|
||||
* @return mixed Function results
|
||||
*/
|
||||
public static function execute($callback, array &$params = array()) {
|
||||
if (is_callable($callback)) {
|
||||
return is_array($callback) ?
|
||||
self::invokeMethod($callback, $params) :
|
||||
self::callFunction($callback, $params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls a function.
|
||||
*
|
||||
* @param string $func Name of function to call
|
||||
* @param array $params Function parameters
|
||||
*/
|
||||
public static function callFunction($func, array &$params = array()) {
|
||||
switch (count($params)) {
|
||||
case 0:
|
||||
return $func();
|
||||
case 1:
|
||||
return $func($params[0]);
|
||||
case 2:
|
||||
return $func($params[0], $params[1]);
|
||||
case 3:
|
||||
return $func($params[0], $params[1], $params[2]);
|
||||
case 4:
|
||||
return $func($params[0], $params[1], $params[2], $params[3]);
|
||||
case 5:
|
||||
return $func($params[0], $params[1], $params[2], $params[3], $params[4]);
|
||||
default:
|
||||
return call_user_func_array($func, $params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes a method.
|
||||
*
|
||||
* @param mixed $func Class method
|
||||
* @param array $params Class method parameters
|
||||
*/
|
||||
public static function invokeMethod($func, array &$params = array()) {
|
||||
list($class, $method) = $func;
|
||||
|
||||
switch (count($params)) {
|
||||
case 0:
|
||||
return $class::$method();
|
||||
case 1:
|
||||
return $class::$method($params[0]);
|
||||
case 2:
|
||||
return $class::$method($params[0], $params[1]);
|
||||
case 3:
|
||||
return $class::$method($params[0], $params[1], $params[2]);
|
||||
case 4:
|
||||
return $class::$method($params[0], $params[1], $params[2], $params[3]);
|
||||
case 5:
|
||||
return $class::$method($params[0], $params[1], $params[2], $params[3], $params[4]);
|
||||
default:
|
||||
return call_user_func_array($func, $params);
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
@ -0,0 +1,173 @@
|
||||
<?php
|
||||
/**
|
||||
* Flight: An extensible micro-framework.
|
||||
*
|
||||
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
|
||||
* @license http://www.opensource.org/licenses/mit-license.php
|
||||
*/
|
||||
|
||||
namespace flight\core;
|
||||
|
||||
/**
|
||||
* The Loader class is resonsible for loading objects. It maintains
|
||||
* a list of reusable class instances and can generate a new class
|
||||
* instances with custom initialization parameters. It also performs
|
||||
* class autoloading.
|
||||
*/
|
||||
class Loader {
|
||||
/**
|
||||
* Registered classes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $classes = array();
|
||||
|
||||
/**
|
||||
* Class instances.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $instances = array();
|
||||
|
||||
/**
|
||||
* Autoload directories.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dirs = array();
|
||||
|
||||
/**
|
||||
* Constructor. Enables autoloading.
|
||||
*/
|
||||
public function __construct() {
|
||||
spl_autoload_register(array(__CLASS__, 'autoload'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a class.
|
||||
*
|
||||
* @param string $name Registry name
|
||||
* @param string $class Class name
|
||||
* @param array $params Class initialization parameters
|
||||
* @param callback $callback Function to call after object instantiation
|
||||
*/
|
||||
public function register($name, $class, array $params = array(), $callback = null) {
|
||||
unset($this->instances[$class]);
|
||||
|
||||
$this->classes[$name] = array($class, $params, $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters a class.
|
||||
*
|
||||
* @param string $name Registry name
|
||||
*/
|
||||
public function unregister($name) {
|
||||
unset($this->classes[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a registered class.
|
||||
*
|
||||
* @param string $name Method name
|
||||
* @param bool $shared Shared instance
|
||||
*/
|
||||
public function load($name, $shared = true) {
|
||||
if (isset($this->classes[$name])) {
|
||||
list($class, $params, $callback) = $this->classes[$name];
|
||||
|
||||
$do_callback = ($callback && (!$shared || !isset($this->instances[$class])));
|
||||
|
||||
$obj = ($shared) ?
|
||||
$this->getInstance($class, $params) :
|
||||
$this->newInstance($class, $params);
|
||||
|
||||
if ($do_callback) {
|
||||
$ref = array(&$obj);
|
||||
call_user_func_array($callback, $ref);
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
$class = ucfirst($name);
|
||||
|
||||
return ($shared) ?
|
||||
$this->getInstance($class) :
|
||||
$this->newInstance($class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a single instance of a class.
|
||||
*
|
||||
* @param string $class Class name
|
||||
* @param array $params Class initialization parameters
|
||||
*/
|
||||
public function getInstance($class, array $params = array()) {
|
||||
if (!isset($this->instances[$class])) {
|
||||
$this->instances[$class] = $this->newInstance($class, $params);
|
||||
}
|
||||
|
||||
return $this->instances[$class];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a new instance of a class.
|
||||
*
|
||||
* @param string $class Class name
|
||||
* @param array $params Class initialization parameters
|
||||
*/
|
||||
public function newInstance($class, array $params = array()) {
|
||||
switch (count($params)) {
|
||||
case 0:
|
||||
return new $class();
|
||||
case 1:
|
||||
return new $class($params[0]);
|
||||
case 2:
|
||||
return new $class($params[0], $params[1]);
|
||||
case 3:
|
||||
return new $class($params[0], $params[1], $params[2]);
|
||||
case 4:
|
||||
return new $class($params[0], $params[1], $params[2], $params[3]);
|
||||
case 5:
|
||||
return new $class($params[0], $params[1], $params[2], $params[3], $params[4]);
|
||||
default:
|
||||
$refClass = new ReflectionClass($class);
|
||||
return $refClass->newInstanceArgs($params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a directory for autoloading classes.
|
||||
*
|
||||
* @param string $dir Directory path
|
||||
*/
|
||||
public function addDirectory($dir) {
|
||||
$this->dirs[] = $dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Autoloads classes.
|
||||
*
|
||||
* @param string $class Class name
|
||||
*/
|
||||
public function autoload($class) {
|
||||
$class_file = str_replace('\\', '/', str_replace('_', '/', $class)).'.php';
|
||||
$dirs = array_merge($this->dirs, array(__DIR__, '.'));
|
||||
|
||||
foreach ($dirs as $dir) {
|
||||
$file = $dir.'/'.$class_file;
|
||||
if (file_exists($file)) {
|
||||
require $file;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Allow other autoloaders to run before raising an error
|
||||
$loader = array_pop(spl_autoload_functions());
|
||||
if (is_array($loader) && $loader[0] == __CLASS__ && $loader[1] == __FUNCTION__) {
|
||||
throw new Exception('Unable to load file: '.$class_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Reference in new issue