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\ConfigurationException; |
||
14 | use WPEmerge\Facades\Application; |
||
15 | use WPEmerge\Application\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 ConfigurationException |
||
32 | * @param string|Closure $raw_handler |
||
33 | * @param string $default_method |
||
34 | * @param string $class_prefix |
||
35 | */ |
||
36 | 8 | public function __construct( $raw_handler, $default_method = '', $class_prefix = '' ) { |
|
37 | 8 | $handler = $this->parse( $raw_handler, $default_method, $class_prefix ); |
|
38 | |||
39 | 8 | if ( $handler === null ) { |
|
40 | 2 | throw new ConfigurationException( 'No or invalid handler provided.' ); |
|
41 | } |
||
42 | |||
43 | 6 | $this->handler = $handler; |
|
44 | 6 | } |
|
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 | * @param string $class_prefix |
||
52 | * @return array|Closure|null |
||
53 | */ |
||
54 | 7 | protected function parse( $raw_handler, $default_method, $class_prefix ) { |
|
55 | 7 | if ( $raw_handler instanceof Closure ) { |
|
56 | 2 | return $raw_handler; |
|
57 | } |
||
58 | |||
59 | 5 | 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 | * @param string $default_method |
||
67 | * @param string $class_prefix |
||
68 | * @return array|null |
||
69 | */ |
||
70 | 5 | protected function parseFromString( $raw_handler, $default_method, $class_prefix ) { |
|
71 | 5 | list( $class, $method ) = array_pad( preg_split( '/@|::/', $raw_handler, 2 ), 2, '' ); |
|
72 | |||
73 | 5 | if ( empty( $method ) ) { |
|
74 | 3 | $method = $default_method; |
|
75 | } |
||
76 | |||
77 | 5 | if ( ! empty( $class ) && ! empty( $method ) ) { |
|
78 | return [ |
||
79 | 3 | 'class' => $class, |
|
80 | 3 | 'method' => $method, |
|
81 | 3 | 'class_prefix' => $class_prefix, |
|
82 | ]; |
||
83 | } |
||
84 | |||
85 | 2 | return null; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Get the parsed handler |
||
90 | * |
||
91 | * @return array|Closure|null |
||
92 | */ |
||
93 | 2 | public function get() { |
|
94 | 2 | 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 | * @return mixed |
||
102 | */ |
||
103 | 6 | public function execute() { |
|
104 | 6 | $arguments = func_get_args(); |
|
105 | |||
106 | 6 | if ( $this->handler instanceof Closure ) { |
|
107 | 2 | return call_user_func_array( $this->handler, $arguments ); |
|
108 | } |
||
109 | |||
110 | 4 | $class_prefix = $this->handler['class_prefix']; |
|
111 | 4 | $class = $this->handler['class']; |
|
112 | 4 | $method = $this->handler['method']; |
|
113 | |||
114 | try { |
||
115 | 4 | $instance = Application::instantiate( $class ); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
116 | 3 | } catch ( ClassNotFoundException $e ) { |
|
117 | try { |
||
118 | 3 | $instance = Application::instantiate( $class_prefix . $class ); |
|
119 | 2 | } catch ( ClassNotFoundException $e ) { |
|
120 | 2 | throw new ClassNotFoundException( 'Class not found - tried: ' . $class . ', ' . $class_prefix . $class ); |
|
121 | } |
||
122 | } |
||
123 | |||
124 | 2 | return call_user_func_array( [$instance, $method], $arguments ); |
|
125 | } |
||
126 | } |
||
127 |