faces.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * @file Extendable Object Faces API. Provided by the faces module.
  4. */
  5. if (!interface_exists('FacesExtenderInterface', FALSE)) {
  6. /**
  7. * Interface for extenders.
  8. */
  9. interface FacesExtenderInterface {
  10. /**
  11. * Constructs an instance of the extender.
  12. */
  13. function __construct(FacesExtendable $object);
  14. /**
  15. * Returns the extended object.
  16. */
  17. public function getExtendable();
  18. }
  19. /**
  20. * The Exception thrown by the FacesExtendable.
  21. */
  22. class FacesExtendableException extends ErrorException {}
  23. }
  24. if (!class_exists('FacesExtender', FALSE)) {
  25. /**
  26. * A common base class for FacesExtenders. Extenders may access protected
  27. * methods and properties of the extendable using the property() and call()
  28. * methods.
  29. */
  30. abstract class FacesExtender implements FacesExtenderInterface {
  31. /**
  32. * @var FacesExtendable
  33. */
  34. protected $object;
  35. function __construct(FacesExtendable $object) {
  36. $this->object = $object;
  37. }
  38. /**
  39. * Returns the extended object.
  40. */
  41. public function getExtendable() {
  42. return $this->object;
  43. }
  44. /**
  45. * Makes protected properties of the extendable accessible.
  46. */
  47. protected function &property($name) {
  48. $var =& $this->object->property($name);
  49. return $var;
  50. }
  51. /**
  52. * Invokes any method on the extended object. May be used to invoke
  53. * protected methods.
  54. *
  55. * @param $name
  56. * The method name.
  57. * @param $arguments
  58. * An array of arguments to pass to the method.
  59. */
  60. protected function call($name, array $args = array()) {
  61. return $this->object->call($name, $args);
  62. }
  63. }
  64. }
  65. if (!class_exists('FacesExtendable', FALSE)) {
  66. /**
  67. * An extendable base class.
  68. */
  69. abstract class FacesExtendable {
  70. protected $facesMethods = array();
  71. protected $faces = array();
  72. protected $facesIncludes = array();
  73. protected $facesClassInstances = array();
  74. static protected $facesIncluded = array();
  75. /**
  76. * Wraps calls to module_load_include() to prevent multiple inclusions.
  77. *
  78. * @see module_load_include()
  79. */
  80. protected static function load_include($args) {
  81. $args += array('type' => 'inc', 'module' => '', 'name' => NULL);
  82. $key = implode(':', $args);
  83. if (!isset(self::$facesIncluded[$key])) {
  84. self::$facesIncluded[$key] = TRUE;
  85. module_load_include($args['type'], $args['module'], $args['name']);
  86. }
  87. }
  88. /**
  89. * Magic method: Invoke the dynamically implemented methods.
  90. */
  91. function __call($name, $arguments = array()) {
  92. if (isset($this->facesMethods[$name])) {
  93. $method = $this->facesMethods[$name];
  94. // Include code, if necessary.
  95. if (isset($this->facesIncludes[$name])) {
  96. self::load_include($this->facesIncludes[$name]);
  97. $this->facesIncludes[$name] = NULL;
  98. }
  99. if (isset($method[0])) {
  100. // We always pass the object reference and the name of the method.
  101. $arguments[] = $this;
  102. $arguments[] = $name;
  103. return call_user_func_array($method[0], $arguments);
  104. }
  105. // Call the method on the extender object, but don't use extender()
  106. // for performance reasons.
  107. if (!isset($this->facesClassInstances[$method[1]])) {
  108. $this->facesClassInstances[$method[1]] = new $method[1]($this);
  109. }
  110. return call_user_func_array(array($this->facesClassInstances[$method[1]], $name), $arguments);
  111. }
  112. $class = check_plain(get_class($this));
  113. throw new FacesExtendableException("There is no method $name for this instance of the class $class.");
  114. }
  115. /**
  116. * Returns the extender object for the given class. May be used to
  117. * explicitly invoke a specific extender, e.g. a function overriding a
  118. * method may use that to explicitly invoke the original extender.
  119. */
  120. public function extender($class) {
  121. if (!isset($this->facesClassInstances[$class])) {
  122. $this->facesClassInstances[$class] = new $class($this);
  123. }
  124. return $this->facesClassInstances[$class];
  125. }
  126. /**
  127. * Returns whether the object can face as the given interface, thus it
  128. * returns TRUE if this oject has been extended by an appropriate
  129. * implementation.
  130. *
  131. * @param $interface
  132. * Optional. A interface to test for. If it's omitted, all interfaces that
  133. * the object can be faced as are returned.
  134. * @return
  135. * Whether the object can face as the interface or an array of interface
  136. * names.
  137. */
  138. public function facesAs($interface = NULL) {
  139. if (!isset($interface)) {
  140. return array_values($this->faces);
  141. }
  142. return in_array($interface, $this->faces) || $this instanceof $interface;
  143. }
  144. /**
  145. * Extend the object by a class to implement the given interfaces.
  146. *
  147. * @param $interface
  148. * The interface name or an array of interface names.
  149. * @param $class
  150. * The extender class, which has to implement the FacesExtenderInterface.
  151. * @param $include
  152. * An optional array describing the file to include before invoking the
  153. * class. The array entries known are 'type', 'module', and 'name'
  154. * matching the parameters of module_load_include(). Only 'module' is
  155. * required as 'type' defaults to 'inc' and 'name' to NULL.
  156. */
  157. public function extendByClass($interface, $className, array $includes = array()) {
  158. $parents = class_implements($className);
  159. if (!in_array('FacesExtenderInterface', $parents)) {
  160. throw new FacesExtendableException("The class " . check_plain($className) . " doesn't implement the FacesExtenderInterface.");
  161. }
  162. $interfaces = is_array($interface) ? $interface : array($interface);
  163. foreach ($interfaces as $interface) {
  164. if (!in_array($interface, $parents)) {
  165. throw new FacesExtendableException("The class " . check_plain($className) . " doesn't implement the interface " . check_plain($interface) . ".");
  166. }
  167. $this->faces[$interface] = $interface;
  168. $this->faces += class_implements($interface);
  169. $face_methods = get_class_methods($interface);
  170. $this->addIncludes($face_methods, $includes);
  171. foreach ($face_methods as $method) {
  172. $this->facesMethods[$method] = array(1 => $className);
  173. }
  174. }
  175. }
  176. /**
  177. * Extend the object by the given functions to implement the given
  178. * interface. There has to be an implementation function for each method of
  179. * the interface.
  180. *
  181. * @param $interface
  182. * The interface name or FALSE to extend the object without a given
  183. * interface.
  184. * @param $methods
  185. * An array, where the keys are methods of the given interface and the
  186. * values the callback functions to use.
  187. * @param $includes
  188. * An optional array to describe files to include before invoking the
  189. * callbacks. You may pass a single array describing one include for all
  190. * callbacks or an array of arrays, keyed by the method names. Look at the
  191. * extendByClass() $include parameter for more details about how to
  192. * describe a single file.
  193. */
  194. public function extend($interface, array $callbacks = array(), array $includes = array()) {
  195. $face_methods = $interface ? get_class_methods($interface) : array_keys($callbacks);
  196. if ($interface) {
  197. if (array_diff($face_methods, array_keys($callbacks))) {
  198. throw new FacesExtendableException("Missing methods for implementing the interface " . check_plain($interface) . ".");
  199. }
  200. $this->faces[$interface] = $interface;
  201. $this->faces += class_implements($interface);
  202. }
  203. $this->addIncludes($face_methods, $includes);
  204. foreach ($face_methods as $method) {
  205. $this->facesMethods[$method] = array(0 => $callbacks[$method]);
  206. }
  207. }
  208. /**
  209. * Override the implementation of an extended method.
  210. *
  211. * @param $methods
  212. * An array of methods of the interface, that should be overriden, where
  213. * the keys are methods to override and the values the callback functions
  214. * to use.
  215. * @param $includes
  216. * An optional array to describe files to include before invoking the
  217. * callbacks. You may pass a single array describing one include for all
  218. * callbacks or an array of arrays, keyed by the method names. Look at the
  219. * extendByClass() $include parameter for more details about how to
  220. * describe a single file.
  221. */
  222. public function override(array $callbacks = array(), array $includes = array()) {
  223. if (array_diff_key($callbacks, $this->facesMethods)) {
  224. throw new FacesExtendableException("A not implemented method is to be overridden.");
  225. }
  226. $this->addIncludes(array_keys($callbacks), $includes);
  227. foreach ($callbacks as $method => $callback) {
  228. $this->facesMethods[$method] = array(0 => $callback);
  229. }
  230. }
  231. /**
  232. * Adds in include files for the given methods while removing any old files.
  233. * If a single include file is described, it's added for all methods.
  234. */
  235. protected function addIncludes($methods, $includes) {
  236. $includes = isset($includes['module']) && is_string($includes['module']) ? array_fill_keys($methods, $includes) : $includes;
  237. $this->facesIncludes = $includes + array_diff_key($this->facesIncludes, array_flip($methods));
  238. }
  239. /**
  240. * Only serialize what is really necessary.
  241. */
  242. public function __sleep() {
  243. return array('facesMethods', 'faces', 'facesIncludes');
  244. }
  245. /**
  246. * Destroys all references to created instances so that PHP's garbage
  247. * collection can do its work. This is needed as PHP's gc has troubles with
  248. * circular references until PHP < 5.3.
  249. */
  250. public function destroy() {
  251. // Avoid circular references.
  252. $this->facesClassInstances = array();
  253. }
  254. /**
  255. * Makes protected properties accessible.
  256. */
  257. public function &property($name) {
  258. if (property_exists($this, $name)) {
  259. return $this->$name;
  260. }
  261. }
  262. /**
  263. * Invokes any method.
  264. *
  265. * This also allows to pass arguments by reference, so it may be used to
  266. * pass arguments by reference to dynamically extended methods.
  267. *
  268. * @param $name
  269. * The method name.
  270. * @param $arguments
  271. * An array of arguments to pass to the method.
  272. */
  273. public function call($name, array $args = array()) {
  274. if (method_exists($this, $name)) {
  275. return call_user_func_array(array($this, $name), $args);
  276. }
  277. return $this->__call($name, $args);
  278. }
  279. }
  280. }