|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WPEmerge\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use WPEmerge; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Represent a Closure or a controller method to be executed in response to a request |
|
11
|
|
|
*/ |
|
12
|
|
|
class Handler { |
|
13
|
|
|
/** |
|
14
|
|
|
* Actual handler |
|
15
|
|
|
* |
|
16
|
|
|
* @var array|Closure|null |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $handler = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor |
|
22
|
|
|
* |
|
23
|
|
|
* @param string|Closure $handler |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function __construct( $handler ) { |
|
26
|
1 |
|
$this->set( $handler ); |
|
27
|
1 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Parse a handler to a Closure or a [class, method] array |
|
31
|
|
|
* |
|
32
|
|
|
* @param string|Closure $handler |
|
33
|
|
|
* @return array|Closure|null |
|
34
|
|
|
*/ |
|
35
|
5 |
|
protected function parse( $handler ) { |
|
36
|
5 |
|
if ( $handler instanceof Closure ) { |
|
37
|
5 |
|
return $handler; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
4 |
|
if ( is_string( $handler ) ) { |
|
41
|
3 |
|
return $this->parseFromString( $handler ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
return null; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Parse a string handler to a [class, method] array |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $handler |
|
51
|
|
|
* @return array|null |
|
52
|
|
|
*/ |
|
53
|
3 |
|
protected function parseFromString( $handler ) { |
|
54
|
3 |
|
$handlerPieces = preg_split( '/@|::/', $handler, 2 ); |
|
55
|
|
|
|
|
56
|
3 |
|
if ( count( $handlerPieces ) === 2 ) { |
|
57
|
|
|
return array( |
|
58
|
2 |
|
'class' => $handlerPieces[0], |
|
59
|
2 |
|
'method' => $handlerPieces[1], |
|
60
|
2 |
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the handler |
|
68
|
|
|
* |
|
69
|
|
|
* @return array|Closure|null |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function get() { |
|
72
|
1 |
|
return $this->handler; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set the handler |
|
77
|
|
|
* |
|
78
|
|
|
* @throws Exception |
|
79
|
|
|
* @param string|Closure $new_handler |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
6 |
|
public function set( $new_handler ) { |
|
83
|
6 |
|
$handler = $this->parse( $new_handler ); |
|
84
|
|
|
|
|
85
|
6 |
|
if ( $handler === null ) { |
|
86
|
2 |
|
throw new Exception( 'No or invalid handler provided.' ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
6 |
|
$this->handler = $handler; |
|
90
|
6 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Execute the handler returning raw result |
|
94
|
|
|
* |
|
95
|
|
|
* @return string|array|\Psr\Http\Message\ResponseInterface |
|
96
|
|
|
*/ |
|
97
|
2 |
|
protected function executeHandler() { |
|
98
|
2 |
|
$arguments = func_get_args(); |
|
99
|
2 |
|
if ( is_a( $this->handler, Closure::class ) ) { |
|
100
|
1 |
|
return call_user_func_array( $this->handler, $arguments ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
$class = $this->handler['class']; |
|
104
|
1 |
|
$method = $this->handler['method']; |
|
105
|
|
|
|
|
106
|
1 |
|
$controller = WPEmerge::instantiate( $class ); |
|
107
|
1 |
|
return call_user_func_array( [$controller, $method], $arguments ); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Execute the handler |
|
112
|
|
|
* |
|
113
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
114
|
|
|
*/ |
|
115
|
5 |
|
public function execute() { |
|
116
|
5 |
|
$response = call_user_func_array( [$this, 'executeHandler'], func_get_args() ); |
|
117
|
|
|
|
|
118
|
5 |
|
if ( is_string( $response ) ) { |
|
119
|
1 |
|
return wpm_output( $response ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
4 |
|
if ( is_array( $response ) ) { |
|
123
|
1 |
|
return wpm_json( $response ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
3 |
|
return $response; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.