woocommerce-multilingual
/
vendor
/
otgs
/
installer
/
includes
/
utilities
/
Collect
/
Support
/
Macroable.php
woocommerce-multilingual
/
vendor
/
otgs
/
installer
/
includes
/
utilities
/
Collect
/
Support
Last commit date
Macroable.php
1 month ago
Macroable.php
42 lines
| 1 | <?php |
| 2 | |
| 3 | namespace OTGS\Installer\Collect\Support; |
| 4 | |
| 5 | use Closure; |
| 6 | use BadMethodCallException; |
| 7 | |
| 8 | trait Macroable { |
| 9 | protected static $macros = []; |
| 10 | |
| 11 | public static function macro( $name, callable $macro ) { |
| 12 | static::$macros[ $name ] = $macro; |
| 13 | } |
| 14 | |
| 15 | public static function hasMacro( $name ) { |
| 16 | return isset( static::$macros[ $name ] ); |
| 17 | } |
| 18 | |
| 19 | public static function __callStatic( $method, $parameters ) { |
| 20 | if ( ! static::hasMacro( $method ) ) { |
| 21 | throw new BadMethodCallException( "Method {$method} does not exist." ); |
| 22 | } |
| 23 | |
| 24 | if ( static::$macros[ $method ] instanceof Closure ) { |
| 25 | return call_user_func_array( Closure::bind( static::$macros[ $method ], null, static::class ), $parameters ); |
| 26 | } |
| 27 | |
| 28 | return call_user_func_array( static::$macros[ $method ], $parameters ); |
| 29 | } |
| 30 | |
| 31 | public function __call( $method, $parameters ) { |
| 32 | if ( ! static::hasMacro( $method ) ) { |
| 33 | throw new BadMethodCallException( "Method {$method} does not exist." ); |
| 34 | } |
| 35 | |
| 36 | if ( static::$macros[ $method ] instanceof Closure ) { |
| 37 | return call_user_func_array( static::$macros[ $method ]->bindTo( $this, static::class ), $parameters ); |
| 38 | } |
| 39 | |
| 40 | return call_user_func_array( static::$macros[ $method ], $parameters ); |
| 41 | } |
| 42 | } |