Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 154 lines (140 sloc) 5.297 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\core;

use lithium\core\Libraries;
use lithium\util\collection\Filters;

/**
* Provides a base class for all static classes in the Lithium framework. Similar to its
* counterpart, the `Object` class, `StaticObject` defines some utility methods for working with
* the filters system, and methods useful for testing purposes.
*
* @see lithium\core\Object
*/
class StaticObject {

/**
* Stores the closures that represent the method filters. They are indexed by called class.
*
* @var array Method filters, indexed by `get_called_class()`.
*/
protected static $_methodFilters = array();

/**
* Keeps a cached list of each class' inheritance tree.
*
* @var array
*/
protected static $_parents = array();

/**
* Apply a closure to a method of the current static object.
*
* @see lithium\core\StaticObject::_filter()
* @see lithium\util\collection\Filters
* @param mixed $method The name of the method to apply the closure to. Can either be a single
* method name as a string, or an array of method names.
* @param closure $filter The closure that is used to filter the method.
* @return void
*/
public static function applyFilter($method, $filter = null) {
$class = get_called_class();
foreach ((array) $method as $m) {
if (!isset(static::$_methodFilters[$class][$m])) {
static::$_methodFilters[$class][$m] = array();
}
static::$_methodFilters[$class][$m][] = $filter;
}
}

/**
* Calls a method on this object with the given parameters. Provides an OO wrapper for
* `forward_static_call_array()`, and improves performance by using straight method calls
* in most cases.
*
* @param string $method Name of the method to call.
* @param array $params Parameter list to use when calling `$method`.
* @return mixed Returns the result of the method call.
*/
public static function invokeMethod($method, $params = array()) {
switch (count($params)) {
case 0:
return static::$method();
case 1:
return static::$method($params[0]);
case 2:
return static::$method($params[0], $params[1]);
case 3:
return static::$method($params[0], $params[1], $params[2]);
case 4:
return static::$method($params[0], $params[1], $params[2], $params[3]);
case 5:
return static::$method($params[0], $params[1], $params[2], $params[3], $params[4]);
default:
return forward_static_call_array(array(get_called_class(), $method), $params);
}
}

/**
* Returns an instance of a class with given `config`. The `name` could be a key from the
* `classes` array, a fully namespaced class name, or an object. Typically this method is used
* in `_init` to create the dependencies used in the current class.
*
* @param string|object $name A `classes` key or fully-namespaced class name.
* @param array $options The configuration passed to the constructor.
* @return object
*/
protected static function _instance($name, array $options = array()) {
if (is_string($name) && isset(static::$_classes[$name])) {
$name = static::$_classes[$name];
}
return Libraries::instance(null, $name, $options);
}

/**
* Executes a set of filters against a method by taking a method's main implementation as a
* callback, and iteratively wrapping the filters around it.
*
* @see lithium\util\collection\Filters
* @param string|array $method The name of the method being executed, or an array containing
* the name of the class that defined the method, and the method name.
* @param array $params An associative array containing all the parameters passed into
* the method.
* @param Closure $callback The method's implementation, wrapped in a closure.
* @param array $filters Additional filters to apply to the method for this call only.
* @return mixed
*/
protected static function _filter($method, $params, $callback, $filters = array()) {
$class = get_called_class();
$hasNoFilters = empty(static::$_methodFilters[$class][$method]);

if ($hasNoFilters && !$filters && !Filters::hasApplied($class, $method)) {
return $callback($class, $params, null);
}
if (!isset(static::$_methodFilters[$class][$method])) {
static::$_methodFilters += array($class => array());
static::$_methodFilters[$class][$method] = array();
}
$data = array_merge(static::$_methodFilters[$class][$method], $filters, array($callback));
return Filters::run($class, $params, compact('data', 'class', 'method'));
}

/**
* Gets and caches an array of the parent methods of a class.
*
* @return array Returns an array of parent classes for the current class.
*/
protected static function _parents() {
$class = get_called_class();

if (!isset(self::$_parents[$class])) {
self::$_parents[$class] = class_parents($class);
}
return self::$_parents[$class];
}

/**
* Exit immediately. Primarily used for overrides during testing.
*
* @param integer|string $status integer range 0 to 254, string printed on exit
* @return void
*/
protected static function _stop($status = 0) {
exit($status);
}
}

?>
Something went wrong with that request. Please try again.