Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Decorator.php
1 <?php
2
3 namespace Drupal\webprofiler;
4
5 /**
6  * Generic class Decorator.
7  */
8 class Decorator {
9
10   /**
11    * @var
12    */
13   protected $object;
14
15   /**
16    * Class constructor.
17    *
18    * @param object $object
19    *   The object to decorate.
20    */
21   public function __construct($object) {
22     $this->object = $object;
23   }
24
25   /**
26    * Return the original (i.e. non decorated) object.
27    *
28    * @return mixed
29    *   The original object.
30    */
31   public function getOriginalObject() {
32     $object = $this->object;
33     while ($object instanceof Decorator) {
34       $object = $object->getOriginalObject();
35     }
36     return $object;
37   }
38
39   /**
40    * Returns true if $method is a PHP callable.
41    *
42    * @param string $method
43    *   The method name.
44    * @param bool $checkSelf
45    *
46    * @return bool|mixed
47    */
48   public function isCallable($method, $checkSelf = FALSE) {
49     //Check the original object
50     $object = $this->getOriginalObject();
51     if (is_callable([$object, $method])) {
52       return $object;
53     }
54     // Check Decorators.
55     $object = $checkSelf ? $this : $this->object;
56     while ($object instanceof Decorator) {
57       if (is_callable([$object, $method])) {
58         return $object;
59       }
60       $object = $this->object;
61     }
62     return FALSE;
63   }
64
65   /**
66    * @param $method
67    * @param $args
68    *
69    * @return mixed
70    *
71    * @throws \Exception
72    */
73   public function __call($method, $args) {
74     if ($object = $this->isCallable($method)) {
75       return call_user_func_array([$object, $method], $args);
76     }
77     throw new \Exception(
78       'Undefined method - ' . get_class($this->getOriginalObject()) . '::' . $method
79     );
80   }
81
82   /**
83    * @param $property
84    *
85    * @return null
86    */
87   public function __get($property) {
88     $object = $this->getOriginalObject();
89     if (property_exists($object, $property)) {
90       return $object->$property;
91     }
92     return NULL;
93   }
94 }