|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CarbonFramework\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use CarbonFramework\Framework; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Represent a callable or a controller method to be executed in response to a request |
|
11
|
|
|
*/ |
|
12
|
|
|
class Handler { |
|
13
|
|
|
/** |
|
14
|
|
|
* Actual handler |
|
15
|
|
|
* |
|
16
|
|
|
* @var callable|array|null |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $handler = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor |
|
22
|
|
|
* |
|
23
|
|
|
* @param string|callable $handler |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct( $handler ) { |
|
26
|
|
|
$this->set( $handler ); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Parse a handler to a callable or a [class, method] array |
|
31
|
|
|
* |
|
32
|
|
|
* @param string|callable $handler |
|
33
|
|
|
* @return callable|array|null |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function parse( $handler ) { |
|
36
|
|
|
if ( $handler instanceof Closure ) { |
|
37
|
|
|
return $handler; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ( is_string( $handler ) ) { |
|
41
|
|
|
$handlerPieces = preg_split( '/@|::/', $handler, 2 ); |
|
42
|
|
|
if ( count( $handlerPieces ) === 1 ) { |
|
43
|
|
|
if ( is_callable( $handlerPieces[0] ) ) { |
|
44
|
|
|
return $handlerPieces[0]; |
|
45
|
|
|
} |
|
46
|
|
|
return null; |
|
47
|
|
|
} |
|
48
|
|
|
return array( |
|
49
|
|
|
'class' => $handlerPieces[0], |
|
50
|
|
|
'method' => $handlerPieces[1], |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Execute the handler returning raw result |
|
59
|
|
|
* |
|
60
|
|
|
* @return string|array|\Psr\Http\Message\ResponseInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function executeHandler() { |
|
63
|
|
|
$arguments = func_get_args(); |
|
64
|
|
|
if ( is_callable( $this->handler ) ) { |
|
65
|
|
|
return call_user_func_array( $this->handler, $arguments ); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$class = $this->handler['class']; |
|
69
|
|
|
$method = $this->handler['method']; |
|
70
|
|
|
|
|
71
|
|
|
$controller = Framework::instantiate( $class ); |
|
72
|
|
|
return call_user_func_array( [$controller, $method], $arguments ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set the handler |
|
77
|
|
|
* |
|
78
|
|
|
* @param string|callable $new_handler |
|
79
|
|
|
* @return null |
|
80
|
|
|
*/ |
|
81
|
|
|
public function set( $new_handler ) { |
|
82
|
|
|
$handler = $this->parse( $new_handler ); |
|
83
|
|
|
|
|
84
|
|
|
if ( $handler === null ) { |
|
85
|
|
|
throw new Exception( 'No or invalid handler provided.' ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->handler = $handler; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Execute the handler |
|
93
|
|
|
* |
|
94
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
|
|
|
|
|
95
|
|
|
*/ |
|
96
|
|
|
public function execute() { |
|
97
|
|
|
$response = call_user_func_array( [$this, 'executeHandler'], func_get_args() ); |
|
98
|
|
|
|
|
99
|
|
|
if ( is_string( $response ) ) { |
|
100
|
|
|
return cf_output( $response ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if ( is_array( $response ) ) { |
|
104
|
|
|
return cf_json( $response ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $response; |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.