1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Obsidian\Routing; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use Obsidian\Framework; |
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|\Closure|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
|
|
|
* @param string|\Closure $new_handler |
79
|
|
|
* @return null |
80
|
|
|
*/ |
81
|
6 |
|
public function set( $new_handler ) { |
82
|
6 |
|
$handler = $this->parse( $new_handler ); |
83
|
|
|
|
84
|
6 |
|
if ( $handler === null ) { |
85
|
2 |
|
throw new Exception( 'No or invalid handler provided.' ); |
86
|
|
|
} |
87
|
|
|
|
88
|
6 |
|
$this->handler = $handler; |
89
|
6 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Execute the handler returning raw result |
93
|
|
|
* |
94
|
|
|
* @return string|array|\Psr\Http\Message\ResponseInterface |
95
|
|
|
*/ |
96
|
2 |
|
protected function executeHandler() { |
97
|
2 |
|
$arguments = func_get_args(); |
98
|
2 |
|
if ( is_a( $this->handler, Closure::class ) ) { |
99
|
1 |
|
return call_user_func_array( $this->handler, $arguments ); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$class = $this->handler['class']; |
103
|
1 |
|
$method = $this->handler['method']; |
104
|
|
|
|
105
|
1 |
|
$controller = Framework::instantiate( $class ); |
106
|
1 |
|
return call_user_func_array( [$controller, $method], $arguments ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Execute the handler |
111
|
|
|
* |
112
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
113
|
|
|
*/ |
114
|
5 |
|
public function execute() { |
115
|
5 |
|
$response = call_user_func_array( [$this, 'executeHandler'], func_get_args() ); |
116
|
|
|
|
117
|
5 |
|
if ( is_string( $response ) ) { |
118
|
1 |
|
return obs_output( $response ); |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
if ( is_array( $response ) ) { |
122
|
1 |
|
return obs_json( $response ); |
123
|
|
|
} |
124
|
|
|
|
125
|
3 |
|
return $response; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.