Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 154 lines (140 sloc) 5.297 kB
e7f3c31 @gwoo going lithium
gwoo authored
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
4f1a9c0 @nateabele Updating copyright year.
nateabele authored
5 * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org)
e7f3c31 @gwoo going lithium
gwoo authored
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
7 */
8
9 namespace lithium\core;
10
a12db0c @nateabele Making `\core\Libraries::instance()` filterable, and refactoring `\co…
nateabele authored
11 use lithium\core\Libraries;
6fb3f64 @daschl change _use lithium_ commands and remove the prefixed slash
daschl authored
12 use lithium\util\collection\Filters;
e7f3c31 @gwoo going lithium
gwoo authored
13
14 /**
00ad303 @nateabele Updating documentation for `\core\Object` and `\core\StaticObject`.
nateabele authored
15 * Provides a base class for all static classes in the Lithium framework. Similar to its
16 * counterpart, the `Object` class, `StaticObject` defines some utility methods for working with
17 * the filters system, and methods useful for testing purposes.
18 *
19 * @see lithium\core\Object
e7f3c31 @gwoo going lithium
gwoo authored
20 */
21 class StaticObject {
22
7e6b328 @jperras Adding missing doc block to core\StaticObject::.
jperras authored
23 /**
24 * Stores the closures that represent the method filters. They are indexed by called class.
25 *
00ad303 @nateabele Updating documentation for `\core\Object` and `\core\StaticObject`.
nateabele authored
26 * @var array Method filters, indexed by `get_called_class()`.
7e6b328 @jperras Adding missing doc block to core\StaticObject::.
jperras authored
27 */
e7f3c31 @gwoo going lithium
gwoo authored
28 protected static $_methodFilters = array();
29
30 /**
870024c @nateabele Implementing model configuration inheritance for any number of base c…
nateabele authored
31 * Keeps a cached list of each class' inheritance tree.
32 *
33 * @var array
34 */
35 protected static $_parents = array();
36
37 /**
e7f3c31 @gwoo going lithium
gwoo authored
38 * Apply a closure to a method of the current static object.
39 *
7e6b328 @jperras Adding missing doc block to core\StaticObject::.
jperras authored
40 * @see lithium\core\StaticObject::_filter()
41 * @see lithium\util\collection\Filters
e7f3c31 @gwoo going lithium
gwoo authored
42 * @param mixed $method The name of the method to apply the closure to. Can either be a single
2f0d2a9 @niel Updating dockblocks to reflect changes to documentation standards.
niel authored
43 * method name as a string, or an array of method names.
840f361 @nateabele Implementing `\util\collection\Filters::apply()` to enable applying f…
nateabele authored
44 * @param closure $filter The closure that is used to filter the method.
e7f3c31 @gwoo going lithium
gwoo authored
45 * @return void
46 */
840f361 @nateabele Implementing `\util\collection\Filters::apply()` to enable applying f…
nateabele authored
47 public static function applyFilter($method, $filter = null) {
f247ae2 @gwoo adding filters to console\Dispatcher, refactoring \console\Request to…
gwoo authored
48 $class = get_called_class();
9decac0 @alkemann insert space after type casting parenthesis
alkemann authored
49 foreach ((array) $method as $m) {
f247ae2 @gwoo adding filters to console\Dispatcher, refactoring \console\Request to…
gwoo authored
50 if (!isset(static::$_methodFilters[$class][$m])) {
51 static::$_methodFilters[$class][$m] = array();
e7f3c31 @gwoo going lithium
gwoo authored
52 }
840f361 @nateabele Implementing `\util\collection\Filters::apply()` to enable applying f…
nateabele authored
53 static::$_methodFilters[$class][$m][] = $filter;
e7f3c31 @gwoo going lithium
gwoo authored
54 }
55 }
56
57 /**
2f0d2a9 @niel Updating dockblocks to reflect changes to documentation standards.
niel authored
58 * Calls a method on this object with the given parameters. Provides an OO wrapper for
1281928 @daschl QA: Misc. Fixes
daschl authored
59 * `forward_static_call_array()`, and improves performance by using straight method calls
60 * in most cases.
e7f3c31 @gwoo going lithium
gwoo authored
61 *
7f43421 @nateabele Added documentation and fixed code formatting in `core\StaticObject`.
nateabele authored
62 * @param string $method Name of the method to call.
63 * @param array $params Parameter list to use when calling `$method`.
64 * @return mixed Returns the result of the method call.
e7f3c31 @gwoo going lithium
gwoo authored
65 */
66 public static function invokeMethod($method, $params = array()) {
67 switch (count($params)) {
68 case 0:
69 return static::$method();
70 case 1:
71 return static::$method($params[0]);
72 case 2:
73 return static::$method($params[0], $params[1]);
74 case 3:
75 return static::$method($params[0], $params[1], $params[2]);
76 case 4:
77 return static::$method($params[0], $params[1], $params[2], $params[3]);
78 case 5:
79 return static::$method($params[0], $params[1], $params[2], $params[3], $params[4]);
80 default:
2a6ccf7 @daschl replacing call_user_func_array with forward_static_call array so that…
daschl authored
81 return forward_static_call_array(array(get_called_class(), $method), $params);
e7f3c31 @gwoo going lithium
gwoo authored
82 }
83 }
84
85 /**
ce3ede1 @gwoo adding Object::_instance() and StaticObject::_instance()
gwoo authored
86 * Returns an instance of a class with given `config`. The `name` could be a key from the
87 * `classes` array, a fully namespaced class name, or an object. Typically this method is used
88 * in `_init` to create the dependencies used in the current class.
89 *
90 * @param string|object $name A `classes` key or fully-namespaced class name.
a12db0c @nateabele Making `\core\Libraries::instance()` filterable, and refactoring `\co…
nateabele authored
91 * @param array $options The configuration passed to the constructor.
92 * @return object
ce3ede1 @gwoo adding Object::_instance() and StaticObject::_instance()
gwoo authored
93 */
a12db0c @nateabele Making `\core\Libraries::instance()` filterable, and refactoring `\co…
nateabele authored
94 protected static function _instance($name, array $options = array()) {
95 if (is_string($name) && isset(static::$_classes[$name])) {
ce3ede1 @gwoo adding Object::_instance() and StaticObject::_instance()
gwoo authored
96 $name = static::$_classes[$name];
97 }
a12db0c @nateabele Making `\core\Libraries::instance()` filterable, and refactoring `\co…
nateabele authored
98 return Libraries::instance(null, $name, $options);
ce3ede1 @gwoo adding Object::_instance() and StaticObject::_instance()
gwoo authored
99 }
100
101 /**
e7f3c31 @gwoo going lithium
gwoo authored
102 * Executes a set of filters against a method by taking a method's main implementation as a
103 * callback, and iteratively wrapping the filters around it.
104 *
7e6b328 @jperras Adding missing doc block to core\StaticObject::.
jperras authored
105 * @see lithium\util\collection\Filters
e7f3c31 @gwoo going lithium
gwoo authored
106 * @param string|array $method The name of the method being executed, or an array containing
107 * the name of the class that defined the method, and the method name.
108 * @param array $params An associative array containing all the parameters passed into
109 * the method.
110 * @param Closure $callback The method's implementation, wrapped in a closure.
2f0d2a9 @niel Updating dockblocks to reflect changes to documentation standards.
niel authored
111 * @param array $filters Additional filters to apply to the method for this call only.
e7f3c31 @gwoo going lithium
gwoo authored
112 * @return mixed
113 */
114 protected static function _filter($method, $params, $callback, $filters = array()) {
d947cdc @nateabele Adding test coverage for `\core\StaticObject` and removing unused cod…
nateabele authored
115 $class = get_called_class();
840f361 @nateabele Implementing `\util\collection\Filters::apply()` to enable applying f…
nateabele authored
116 $hasNoFilters = empty(static::$_methodFilters[$class][$method]);
e7f3c31 @gwoo going lithium
gwoo authored
117
840f361 @nateabele Implementing `\util\collection\Filters::apply()` to enable applying f…
nateabele authored
118 if ($hasNoFilters && !$filters && !Filters::hasApplied($class, $method)) {
b124593 @nateabele Updating `\core\StaticObject` from filter refactor.
nateabele authored
119 return $callback($class, $params, null);
e7f3c31 @gwoo going lithium
gwoo authored
120 }
7f43421 @nateabele Added documentation and fixed code formatting in `core\StaticObject`.
nateabele authored
121 if (!isset(static::$_methodFilters[$class][$method])) {
122 static::$_methodFilters += array($class => array());
123 static::$_methodFilters[$class][$method] = array();
124 }
244646c @nateabele Adding test for `\data\Source`. Refactoring classes in `\data` into "…
nateabele authored
125 $data = array_merge(static::$_methodFilters[$class][$method], $filters, array($callback));
126 return Filters::run($class, $params, compact('data', 'class', 'method'));
e7f3c31 @gwoo going lithium
gwoo authored
127 }
128
129 /**
870024c @nateabele Implementing model configuration inheritance for any number of base c…
nateabele authored
130 * Gets and caches an array of the parent methods of a class.
131 *
132 * @return array Returns an array of parent classes for the current class.
133 */
134 protected static function _parents() {
135 $class = get_called_class();
136
137 if (!isset(self::$_parents[$class])) {
138 self::$_parents[$class] = class_parents($class);
139 }
140 return self::$_parents[$class];
141 }
142
143 /**
2f0d2a9 @niel Updating dockblocks to reflect changes to documentation standards.
niel authored
144 * Exit immediately. Primarily used for overrides during testing.
e7f3c31 @gwoo going lithium
gwoo authored
145 *
ce3ede1 @gwoo adding Object::_instance() and StaticObject::_instance()
gwoo authored
146 * @param integer|string $status integer range 0 to 254, string printed on exit
e7f3c31 @gwoo going lithium
gwoo authored
147 * @return void
148 */
ce3ede1 @gwoo adding Object::_instance() and StaticObject::_instance()
gwoo authored
149 protected static function _stop($status = 0) {
150 exit($status);
e7f3c31 @gwoo going lithium
gwoo authored
151 }
152 }
153
154 ?>
Something went wrong with that request. Please try again.