123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
- if (!interface_exists('FacesExtenderInterface', FALSE)) {
-
- interface FacesExtenderInterface {
-
- function __construct(FacesExtendable $object);
-
- public function getExtendable();
- }
-
- class FacesExtendableException extends ErrorException {}
- }
- if (!class_exists('FacesExtender', FALSE)) {
-
- abstract class FacesExtender implements FacesExtenderInterface {
-
- protected $object;
- function __construct(FacesExtendable $object) {
- $this->object = $object;
- }
-
- public function getExtendable() {
- return $this->object;
- }
-
- protected function &property($name) {
- $var =& $this->object->property($name);
- return $var;
- }
-
- protected function call($name, array $args = array()) {
- return $this->object->call($name, $args);
- }
- }
- }
- if (!class_exists('FacesExtendable', FALSE)) {
-
- abstract class FacesExtendable {
- protected $facesMethods = array();
- protected $faces = array();
- protected $facesIncludes = array();
- protected $facesClassInstances = array();
- static protected $facesIncluded = array();
-
- protected static function load_include($args) {
- $args += array('type' => 'inc', 'module' => '', 'name' => NULL);
- $key = implode(':', $args);
- if (!isset(self::$facesIncluded[$key])) {
- self::$facesIncluded[$key] = TRUE;
- module_load_include($args['type'], $args['module'], $args['name']);
- }
- }
-
- function __call($name, $arguments = array()) {
- if (isset($this->facesMethods[$name])) {
- $method = $this->facesMethods[$name];
-
- if (isset($this->facesIncludes[$name])) {
- self::load_include($this->facesIncludes[$name]);
- $this->facesIncludes[$name] = NULL;
- }
- if (isset($method[0])) {
-
- $arguments[] = $this;
- $arguments[] = $name;
- return call_user_func_array($method[0], $arguments);
- }
-
-
- if (!isset($this->facesClassInstances[$method[1]])) {
- $this->facesClassInstances[$method[1]] = new $method[1]($this);
- }
- return call_user_func_array(array($this->facesClassInstances[$method[1]], $name), $arguments);
- }
- $class = check_plain(get_class($this));
- throw new FacesExtendableException("There is no method $name for this instance of the class $class.");
- }
-
- public function extender($class) {
- if (!isset($this->facesClassInstances[$class])) {
- $this->facesClassInstances[$class] = new $class($this);
- }
- return $this->facesClassInstances[$class];
- }
-
- public function facesAs($interface = NULL) {
- if (!isset($interface)) {
- return array_values($this->faces);
- }
- return in_array($interface, $this->faces) || $this instanceof $interface;
- }
-
- public function extendByClass($interface, $className, array $includes = array()) {
- $parents = class_implements($className);
- if (!in_array('FacesExtenderInterface', $parents)) {
- throw new FacesExtendableException("The class " . check_plain($className) . " doesn't implement the FacesExtenderInterface.");
- }
- $interfaces = is_array($interface) ? $interface : array($interface);
- foreach ($interfaces as $interface) {
- if (!in_array($interface, $parents)) {
- throw new FacesExtendableException("The class " . check_plain($className) . " doesn't implement the interface " . check_plain($interface) . ".");
- }
- $this->faces[$interface] = $interface;
- $this->faces += class_implements($interface);
- $face_methods = get_class_methods($interface);
- $this->addIncludes($face_methods, $includes);
- foreach ($face_methods as $method) {
- $this->facesMethods[$method] = array(1 => $className);
- }
- }
- }
-
- public function extend($interface, array $callbacks = array(), array $includes = array()) {
- $face_methods = $interface ? get_class_methods($interface) : array_keys($callbacks);
- if ($interface) {
- if (array_diff($face_methods, array_keys($callbacks))) {
- throw new FacesExtendableException("Missing methods for implementing the interface " . check_plain($interface) . ".");
- }
- $this->faces[$interface] = $interface;
- $this->faces += class_implements($interface);
- }
- $this->addIncludes($face_methods, $includes);
- foreach ($face_methods as $method) {
- $this->facesMethods[$method] = array(0 => $callbacks[$method]);
- }
- }
-
- public function override(array $callbacks = array(), array $includes = array()) {
- if (array_diff_key($callbacks, $this->facesMethods)) {
- throw new FacesExtendableException("A not implemented method is to be overridden.");
- }
- $this->addIncludes(array_keys($callbacks), $includes);
- foreach ($callbacks as $method => $callback) {
- $this->facesMethods[$method] = array(0 => $callback);
- }
- }
-
- protected function addIncludes($methods, $includes) {
- $includes = isset($includes['module']) && is_string($includes['module']) ? array_fill_keys($methods, $includes) : $includes;
- $this->facesIncludes = $includes + array_diff_key($this->facesIncludes, array_flip($methods));
- }
-
- public function __sleep() {
- return array('facesMethods', 'faces', 'facesIncludes');
- }
-
- public function destroy() {
-
- $this->facesClassInstances = array();
- }
-
- public function &property($name) {
- if (property_exists($this, $name)) {
- return $this->$name;
- }
- }
-
- public function call($name, array $args = array()) {
- if (method_exists($this, $name)) {
- return call_user_func_array(array($this, $name), $args);
- }
- return $this->__call($name, $args);
- }
- }
- }
|