htmlburger /
wpemerge
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @package WPEmerge |
||
| 4 | * @author Atanas Angelov <[email protected]> |
||
| 5 | * @copyright 2018 Atanas Angelov |
||
| 6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
||
| 7 | * @link https://wpemerge.com/ |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace WPEmerge\Helpers; |
||
| 11 | |||
| 12 | use Closure; |
||
| 13 | use WPEmerge\Exceptions\Exception; |
||
| 14 | use WPEmerge\Facades\Framework; |
||
| 15 | use WPEmerge\Framework\ClassNotFoundException; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Represent a generic handler - a Closure or a class method to be resolved from the service container |
||
| 19 | */ |
||
| 20 | class Handler { |
||
| 21 | /** |
||
| 22 | * Parsed handler |
||
| 23 | * |
||
| 24 | * @var array|Closure|null |
||
| 25 | */ |
||
| 26 | protected $handler = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Constructor |
||
| 30 | * |
||
| 31 | * @throws Exception |
||
| 32 | * @param string|Closure $raw_handler |
||
| 33 | * @param string $default_method |
||
| 34 | 7 | * @param string $class_prefix |
|
| 35 | 7 | */ |
|
| 36 | public function __construct( $raw_handler, $default_method = '', $class_prefix = '' ) { |
||
| 37 | 7 | $handler = $this->parse( $raw_handler, $default_method, $class_prefix ); |
|
| 38 | 2 | ||
| 39 | if ( $handler === null ) { |
||
| 40 | throw new Exception( 'No or invalid handler provided.' ); |
||
| 41 | 5 | } |
|
| 42 | 5 | ||
| 43 | $this->handler = $handler; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Parse a raw handler to a Closure or a [class, method] array |
||
| 48 | * |
||
| 49 | * @param string|Closure $raw_handler |
||
| 50 | * @param string $default_method |
||
| 51 | 6 | * @param string $class_prefix |
|
| 52 | 6 | * @return array|Closure|null |
|
| 53 | 1 | */ |
|
| 54 | protected function parse( $raw_handler, $default_method, $class_prefix ) { |
||
| 55 | if ( $raw_handler instanceof Closure ) { |
||
| 56 | 5 | return $raw_handler; |
|
| 57 | } |
||
| 58 | |||
| 59 | return $this->parseFromString( $raw_handler, $default_method, $class_prefix ); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Parse a raw string handler to a [class, method] array |
||
| 64 | * |
||
| 65 | * @param string $raw_handler |
||
| 66 | 5 | * @param string $default_method |
|
| 67 | 5 | * @param string $class_prefix |
|
| 68 | * @return array|null |
||
| 69 | 5 | */ |
|
| 70 | protected function parseFromString( $raw_handler, $default_method, $class_prefix ) { |
||
| 71 | 1 | list( $class, $method ) = array_pad( preg_split( '/@|::/', $raw_handler, 2 ), 2, '' ); |
|
|
1 ignored issue
–
show
Bug
introduced
by
Loading history...
|
|||
| 72 | 1 | ||
| 73 | 1 | if ( empty( $method ) ) { |
|
| 74 | $method = $default_method; |
||
| 75 | } |
||
| 76 | 4 | ||
| 77 | if ( ! empty( $class ) && ! empty( $method ) ) { |
||
| 78 | 2 | return [ |
|
| 79 | 2 | 'class' => $class, |
|
| 80 | 2 | 'method' => $method, |
|
| 81 | 'class_prefix' => $class_prefix, |
||
| 82 | ]; |
||
| 83 | 2 | } |
|
| 84 | |||
| 85 | return null; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get the parsed handler |
||
| 90 | * |
||
| 91 | 1 | * @return array|Closure|null |
|
| 92 | 1 | */ |
|
| 93 | public function get() { |
||
| 94 | return $this->handler; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Execute the parsed handler with any provided arguments and return the result |
||
| 99 | * |
||
| 100 | * @param mixed ...$arguments |
||
| 101 | 3 | * @return mixed |
|
| 102 | 3 | */ |
|
| 103 | 3 | public function execute() { |
|
| 104 | 2 | $arguments = func_get_args(); |
|
| 105 | |||
| 106 | if ( $this->handler instanceof Closure ) { |
||
| 107 | 1 | return call_user_func_array( $this->handler, $arguments ); |
|
| 108 | 1 | } |
|
| 109 | |||
| 110 | 1 | $class_prefix = $this->handler['class_prefix']; |
|
| 111 | 1 | $class = $this->handler['class']; |
|
| 112 | $method = $this->handler['method']; |
||
| 113 | |||
| 114 | try { |
||
| 115 | $instance = Framework::instantiate( $class ); |
||
| 116 | } catch ( ClassNotFoundException $e ) { |
||
| 117 | try { |
||
| 118 | $instance = Framework::instantiate( $class_prefix . $class ); |
||
| 119 | } catch ( ClassNotFoundException $e ) { |
||
| 120 | throw new ClassNotFoundException( 'Class not found - tried: ' . $class . ', ' . $class_prefix . $class ); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | return call_user_func_array( [$instance, $method], $arguments ); |
||
| 125 | } |
||
| 126 | } |
||
| 127 |