|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Modulus\Support; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Modulus\Support\Exceptions\CannotAddMethodException; |
| 7 | +use Modulus\Support\Exceptions\CannotCallMethodException; |
| 8 | + |
| 9 | +trait Extendable |
| 10 | +{ |
| 11 | + /** |
| 12 | + * A list of custom functions |
| 13 | + * |
| 14 | + * @var array $functions |
| 15 | + */ |
| 16 | + public static $functions = []; |
| 17 | + |
| 18 | + /** |
| 19 | + * A list of custom static functions |
| 20 | + * |
| 21 | + * @var array $staticFunctions |
| 22 | + */ |
| 23 | + public static $staticFunctions = []; |
| 24 | + |
| 25 | + /** |
| 26 | + * Add custom function |
| 27 | + * |
| 28 | + * @param string $method |
| 29 | + * @param Closure $closure |
| 30 | + * @return mixed |
| 31 | + */ |
| 32 | + public static function bind(string $method, Closure $closure) |
| 33 | + { |
| 34 | + $trace = debug_backtrace()[1]; |
| 35 | + |
| 36 | + if ( |
| 37 | + $trace['class'] == 'Modulus\Framework\Upstart\Prototype' && |
| 38 | + $trace['function'] == 'bind' && |
| 39 | + count($trace['args']) == 3 |
| 40 | + ) { |
| 41 | + if (!array_key_exists($method, Self::$functions)) { |
| 42 | + return Self::$functions[$method] = [$method => $closure]; |
| 43 | + } |
| 44 | + |
| 45 | + throw new CannotAddMethodException; |
| 46 | + } |
| 47 | + |
| 48 | + throw new CannotCallMethodException('Call to ' . self::class . '::bind() is not allowed'); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Add custom static function |
| 53 | + * |
| 54 | + * @param string $method |
| 55 | + * @param Closure $closure |
| 56 | + * @return mixed |
| 57 | + */ |
| 58 | + public static function static(string $method, Closure $closure) |
| 59 | + { |
| 60 | + $trace = debug_backtrace()[1]; |
| 61 | + |
| 62 | + if ( |
| 63 | + $trace['class'] == 'Modulus\Framework\Upstart\Prototype' && |
| 64 | + $trace['function'] == 'static' && |
| 65 | + count($trace['args']) == 3 |
| 66 | + ) { |
| 67 | + if (!array_key_exists($method, Self::$staticFunctions)) { |
| 68 | + return Self::$staticFunctions[$method] = [$method => $closure]; |
| 69 | + } |
| 70 | + |
| 71 | + throw new CannotAddMethodException; |
| 72 | + } |
| 73 | + |
| 74 | + throw new CannotCallMethodException('Call to ' . self::class . '::static() is not allowed'); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Call custom function |
| 79 | + * |
| 80 | + * @param string $method |
| 81 | + * @param array $args |
| 82 | + * @return mixed |
| 83 | + */ |
| 84 | + public function __call(string $method, array $args) |
| 85 | + { |
| 86 | + if (array_key_exists($method, Self::$functions)) { |
| 87 | + return call_user_func_array(Self::$functions[$method][$method], array_merge([$this], $args)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Call custom static function |
| 93 | + * |
| 94 | + * @param string $method |
| 95 | + * @param array $args |
| 96 | + * @return mixed |
| 97 | + */ |
| 98 | + public static function __callStatic(string $method, array $args) |
| 99 | + { |
| 100 | + if (array_key_exists($method, Self::$staticFunctions)) { |
| 101 | + return call_user_func_array(Self::$staticFunctions[$method][$method], $args); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments