jclaveau /
php-deferred-callchain
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * DeferredCallChain |
||
| 5 | * |
||
| 6 | * @package php-deferred-callchain |
||
| 7 | * @author Jean Claveau |
||
| 8 | */ |
||
| 9 | namespace JClaveau\Async; |
||
| 10 | |||
| 11 | use JClaveau\Async\Exceptions\BadTargetClassException; |
||
| 12 | use JClaveau\Async\Exceptions\BadTargetTypeException; |
||
| 13 | use JClaveau\Async\Exceptions\UndefinedTargetClassException; |
||
| 14 | use JClaveau\Async\Exceptions\BadTargetInterfaceException; |
||
| 15 | use JClaveau\Async\Exceptions\TargetAlreadyDefinedException; |
||
| 16 | use JClaveau\VisibilityViolator\VisibilityViolator; |
||
| 17 | use BadMethodCallException; |
||
| 18 | /** |
||
| 19 | * This class stores an arbitrary stack of calls (methods or array entries access) |
||
| 20 | * that will be callable on any future variable. |
||
| 21 | */ |
||
| 22 | class DeferredCallChain implements \JsonSerializable, \ArrayAccess |
||
| 23 | { |
||
| 24 | use \JClaveau\Traits\Fluent\New_; |
||
| 25 | use FunctionCallTrait; |
||
| 26 | use ArrayAccessTrait; |
||
| 27 | use ExportTrait; |
||
| 28 | /** @var array $stack The stack of deferred calls */ |
||
| 29 | protected $stack = []; |
||
| 30 | /** @var mixed $expectedTarget The stack of deferred calls */ |
||
| 31 | protected $expectedTarget; |
||
| 32 | /** |
||
| 33 | * Constructor |
||
| 34 | * |
||
| 35 | * @param string $class_type_interface_or_instance The expected target class/type/interface/instance |
||
| 36 | */ |
||
| 37 | public function __construct($class_type_interface_or_instance = null) |
||
| 38 | { |
||
| 39 | if ($class_type_interface_or_instance) { |
||
| 40 | $this->expectedTarget = $class_type_interface_or_instance; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | /** |
||
| 44 | * Stores any call in the the stack. |
||
| 45 | * |
||
| 46 | * @param string $method |
||
| 47 | * @param array $arguments |
||
| 48 | * |
||
| 49 | * @return $this |
||
| 50 | */ |
||
| 51 | public final function __call($method, array $arguments) |
||
| 52 | { |
||
| 53 | $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; |
||
| 54 | $this->stack[] = ['method' => $method, 'arguments' => $arguments, 'file' => isset($caller['file']) ? $caller['file'] : null, 'line' => isset($caller['line']) ? $caller['line'] : null]; |
||
| 55 | return $this; |
||
| 56 | } |
||
| 57 | /** |
||
| 58 | * ArrayAccess interface |
||
| 59 | * |
||
| 60 | * @param string $key The entry to acces |
||
| 61 | */ |
||
| 62 | public function &offsetGet($key) |
||
| 63 | { |
||
| 64 | $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; |
||
| 65 | $this->stack[] = ['entry' => $key, 'file' => isset($caller['file']) ? $caller['file'] : null, 'line' => isset($caller['line']) ? $caller['line'] : null]; |
||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | /** |
||
| 69 | * Checks that the provided target matches the type/class/interface |
||
| 70 | * given during construction. |
||
| 71 | * |
||
| 72 | * @param mixed $target |
||
| 73 | * @return mixed $target Checked |
||
| 74 | */ |
||
| 75 | protected function checkTarget($target) |
||
| 76 | { |
||
| 77 | if (is_object($this->expectedTarget)) { |
||
| 78 | if ($target) { |
||
| 79 | throw new TargetAlreadyDefinedException($this, $this->expectedTarget, $target); |
||
| 80 | } |
||
| 81 | $out = $this->expectedTarget; |
||
| 82 | } elseif (is_string($this->expectedTarget)) { |
||
| 83 | if (class_exists($this->expectedTarget)) { |
||
| 84 | if (!$target instanceof $this->expectedTarget) { |
||
| 85 | throw new BadTargetClassException($this, $this->expectedTarget, $target); |
||
| 86 | } |
||
| 87 | } elseif (interface_exists($this->expectedTarget)) { |
||
| 88 | if (!$target instanceof $this->expectedTarget) { |
||
| 89 | throw new BadTargetInterfaceException($this, $this->expectedTarget, $target); |
||
| 90 | } |
||
| 91 | } elseif (type_exists($this->expectedTarget)) { |
||
| 92 | if (gettype($target) != $this->expectedTarget) { |
||
| 93 | throw new BadTargetTypeException($this, $this->expectedTarget, $target); |
||
| 94 | } |
||
| 95 | } else { |
||
| 96 | throw new UndefinedTargetClassException($this, $this->expectedTarget); |
||
| 97 | } |
||
| 98 | $out = $target; |
||
| 99 | } else { |
||
| 100 | $out = $target; |
||
| 101 | } |
||
| 102 | return $out; |
||
| 103 | } |
||
| 104 | /** |
||
| 105 | * Calling a method coded inside a magic __call can produce a |
||
| 106 | * BadMethodCallException and thus not be a callable. |
||
| 107 | * |
||
| 108 | * @param string $method_type '->' or '::' |
||
| 109 | * @param mixed $current_chained_subject |
||
| 110 | * @param string $method_name |
||
| 111 | * @param array $arguments |
||
| 112 | * |
||
| 113 | * @return bool $is_called |
||
| 114 | */ |
||
| 115 | protected function checkMethodIsReallyCallable($method_type, &$current_chained_subject, $method_name, $arguments) |
||
| 116 | { |
||
| 117 | $is_called = true; |
||
| 118 | try { |
||
| 119 | if ($method_type == '->') { |
||
| 120 | $callable = [$current_chained_subject, $method_name]; |
||
| 121 | } elseif ($method_type == '::') { |
||
| 122 | if (is_object($current_chained_subject)) { |
||
| 123 | $class = get_class($current_chained_subject); |
||
| 124 | } elseif (is_string($current_chained_subject)) { |
||
| 125 | $class = $current_chained_subject; |
||
| 126 | } |
||
| 127 | $callable = $class . '::' . $method_name; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 128 | } |
||
| 129 | $current_chained_subject = call_user_func_array($callable, $arguments); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 130 | } catch (\BadMethodCallException $e) { |
||
| 131 | if ($this->exceptionTrownFromMagicCall($e->getTrace(), $current_chained_subject, $method_name)) { |
||
| 132 | $is_called = false; |
||
| 133 | } else { |
||
| 134 | throw $e; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | return $is_called; |
||
| 138 | } |
||
| 139 | /** |
||
| 140 | * Checks if the exception having $trace is thrown from à __call |
||
| 141 | * magic method. |
||
| 142 | * |
||
| 143 | * @param array $trace |
||
| 144 | * @param object $current_chained_subject |
||
| 145 | * @param string $method_name |
||
| 146 | * |
||
| 147 | * @return bool Whether or not the exception having the $trace has been |
||
| 148 | * thrown from a __call() method. |
||
| 149 | */ |
||
| 150 | protected function exceptionTrownFromMagicCall($trace, $current_chained_subject, $method_name) |
||
| 151 | { |
||
| 152 | // Before PHP 7, there is a raw for the non existing method called |
||
| 153 | $call_user_func_array_position = PHP_VERSION_ID < 70000 ? 2 : 1; |
||
| 154 | return ($trace[0]['function'] == '__call' || $trace[0]['function'] == '__callStatic') && $trace[0]['class'] == (is_string($current_chained_subject) ? $current_chained_subject : get_class($current_chained_subject)) && $trace[0]['args'][0] == $method_name && ($trace[$call_user_func_array_position]['file'] == __FILE__ && $trace[$call_user_func_array_position]['function'] == 'call_user_func_array'); |
||
|
0 ignored issues
–
show
|
|||
| 155 | } |
||
| 156 | /** |
||
| 157 | * Invoking the instance produces the call of the stack |
||
| 158 | * |
||
| 159 | * @param mixed $target The target to apply the callchain on |
||
| 160 | * @return mixed The value returned once the call chain is called uppon $target |
||
| 161 | */ |
||
| 162 | public function __invoke($target = null) |
||
| 163 | { |
||
| 164 | $out = $this->checkTarget($target); |
||
| 165 | foreach ($this->stack as $i => $call) { |
||
| 166 | $is_called = false; |
||
| 167 | try { |
||
| 168 | if (isset($call['method'])) { |
||
| 169 | if (is_callable([$out, $call['method']])) { |
||
| 170 | $is_called = $this->checkMethodIsReallyCallable('->', $out, $call['method'], $call['arguments']); |
||
| 171 | } |
||
| 172 | if (!$is_called && (is_string($out) && is_callable($out . '::' . $call['method']) || is_object($out) && is_callable(get_class($out) . '::' . $call['method']))) { |
||
| 173 | $is_called = $this->checkMethodIsReallyCallable('::', $out, $call['method'], $call['arguments']); |
||
| 174 | } |
||
| 175 | if (!$is_called && is_callable($call['method'])) { |
||
| 176 | $arguments = $this->prepareArgs($call['arguments'], $out); |
||
| 177 | $out = call_user_func_array($call['method'], $arguments); |
||
| 178 | $is_called = true; |
||
| 179 | } |
||
| 180 | if (!$is_called) { |
||
| 181 | throw new \BadMethodCallException($call['method'] . "() is neither a method of " . (is_string($out) ? $out : get_class($out)) . " nor a function"); |
||
| 182 | } |
||
| 183 | } else { |
||
| 184 | $out = $out[$call['entry']]; |
||
| 185 | } |
||
| 186 | } catch (\Exception $e) { |
||
| 187 | $callchain_description = $this->toString(['target' => $target, 'limit' => $i]); |
||
| 188 | VisibilityViolator::setHiddenProperty($e, 'message', $e->getMessage() . "\nWhen applying {$callchain_description} defined at " . $call['file'] . ':' . $call['line']); |
||
| 189 | // Throw $e with the good stack (usage exception) |
||
| 190 | throw $e; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | return $out; |
||
| 194 | } |
||
| 195 | } |