StaticObject.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\core;
  9. use lithium\core\Libraries;
  10. use lithium\util\collection\Filters;
  11. use lithium\analysis\Inspector;
  12. /**
  13. * Provides a base class for all static classes in the Lithium framework. Similar to its
  14. * counterpart, the `Object` class, `StaticObject` defines some utility methods for working with
  15. * the filters system, and methods useful for testing purposes.
  16. *
  17. * @see lithium\core\Object
  18. */
  19. class StaticObject {
  20. /**
  21. * Stores the closures that represent the method filters. They are indexed by called class.
  22. *
  23. * @var array Method filters, indexed by `get_called_class()`.
  24. */
  25. protected static $_methodFilters = array();
  26. /**
  27. * Keeps a cached list of each class' inheritance tree.
  28. *
  29. * @var array
  30. */
  31. protected static $_parents = array();
  32. /**
  33. * Apply a closure to a method of the current static object.
  34. *
  35. * @see lithium\core\StaticObject::_filter()
  36. * @see lithium\util\collection\Filters
  37. * @param mixed $method The name of the method to apply the closure to. Can either be a single
  38. * method name as a string, or an array of method names. Can also be false to remove
  39. * all filters on the current object.
  40. * @param closure $filter The closure that is used to filter the method(s), can also be false
  41. * to remove all the current filters for the given method.
  42. * @return void
  43. */
  44. public static function applyFilter($method, $filter = null) {
  45. $class = get_called_class();
  46. if ($method === false) {
  47. static::$_methodFilters[$class] = array();
  48. return;
  49. }
  50. foreach ((array) $method as $m) {
  51. if (!isset(static::$_methodFilters[$class][$m]) || $filter === false) {
  52. static::$_methodFilters[$class][$m] = array();
  53. }
  54. if ($filter !== false) {
  55. static::$_methodFilters[$class][$m][] = $filter;
  56. }
  57. }
  58. }
  59. /**
  60. * Calls a method on this object with the given parameters. Provides an OO wrapper for
  61. * `forward_static_call_array()`, and improves performance by using straight method calls
  62. * in most cases.
  63. *
  64. * @param string $method Name of the method to call.
  65. * @param array $params Parameter list to use when calling `$method`.
  66. * @return mixed Returns the result of the method call.
  67. */
  68. public static function invokeMethod($method, $params = array()) {
  69. switch (count($params)) {
  70. case 0:
  71. return static::$method();
  72. case 1:
  73. return static::$method($params[0]);
  74. case 2:
  75. return static::$method($params[0], $params[1]);
  76. case 3:
  77. return static::$method($params[0], $params[1], $params[2]);
  78. case 4:
  79. return static::$method($params[0], $params[1], $params[2], $params[3]);
  80. case 5:
  81. return static::$method($params[0], $params[1], $params[2], $params[3], $params[4]);
  82. default:
  83. return forward_static_call_array(array(get_called_class(), $method), $params);
  84. }
  85. }
  86. /**
  87. * Will determine if a method can be called.
  88. *
  89. * @param string $method Method name.
  90. * @param bool $internal Interal call or not.
  91. * @return bool
  92. */
  93. public static function respondsTo($method, $internal = false) {
  94. return Inspector::isCallable(get_called_class(), $method, $internal);
  95. }
  96. /**
  97. * Returns an instance of a class with given `config`. The `name` could be a key from the
  98. * `classes` array, a fully namespaced class name, or an object. Typically this method is used
  99. * in `_init` to create the dependencies used in the current class.
  100. *
  101. * @param string|object $name A `classes` key or fully-namespaced class name.
  102. * @param array $options The configuration passed to the constructor.
  103. * @return object
  104. */
  105. protected static function _instance($name, array $options = array()) {
  106. if (is_string($name) && isset(static::$_classes[$name])) {
  107. $name = static::$_classes[$name];
  108. }
  109. return Libraries::instance(null, $name, $options);
  110. }
  111. /**
  112. * Executes a set of filters against a method by taking a method's main implementation as a
  113. * callback, and iteratively wrapping the filters around it.
  114. *
  115. * @see lithium\util\collection\Filters
  116. * @param string|array $method The name of the method being executed, or an array containing
  117. * the name of the class that defined the method, and the method name.
  118. * @param array $params An associative array containing all the parameters passed into
  119. * the method.
  120. * @param Closure $callback The method's implementation, wrapped in a closure.
  121. * @param array $filters Additional filters to apply to the method for this call only.
  122. * @return mixed
  123. */
  124. protected static function _filter($method, $params, $callback, $filters = array()) {
  125. $class = get_called_class();
  126. $hasNoFilters = empty(static::$_methodFilters[$class][$method]);
  127. if ($hasNoFilters && !$filters && !Filters::hasApplied($class, $method)) {
  128. return $callback($class, $params, null);
  129. }
  130. if (!isset(static::$_methodFilters[$class][$method])) {
  131. static::$_methodFilters += array($class => array());
  132. static::$_methodFilters[$class][$method] = array();
  133. }
  134. $data = array_merge(static::$_methodFilters[$class][$method], $filters, array($callback));
  135. return Filters::run($class, $params, compact('data', 'class', 'method'));
  136. }
  137. /**
  138. * Gets and caches an array of the parent methods of a class.
  139. *
  140. * @return array Returns an array of parent classes for the current class.
  141. */
  142. protected static function _parents() {
  143. $class = get_called_class();
  144. if (!isset(self::$_parents[$class])) {
  145. self::$_parents[$class] = class_parents($class);
  146. }
  147. return self::$_parents[$class];
  148. }
  149. /**
  150. * Exit immediately. Primarily used for overrides during testing.
  151. *
  152. * @param integer|string $status integer range 0 to 254, string printed on exit
  153. * @return void
  154. */
  155. protected static function _stop($status = 0) {
  156. exit($status);
  157. }
  158. }
  159. ?>