|
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\Routing; |
|
11
|
|
|
|
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
use WPEmerge\Exceptions\Exception; |
|
14
|
|
|
use WPEmerge\Facades\Response; |
|
15
|
|
|
use WPEmerge\Helpers\Handler; |
|
16
|
|
|
use WPEmerge\Responses\ResponsableInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Represent a Closure or a controller method to be executed in response to a request |
|
20
|
|
|
*/ |
|
21
|
|
|
class PipelineHandler { |
|
22
|
|
|
/** |
|
23
|
|
|
* Actual handler |
|
24
|
|
|
* |
|
25
|
|
|
* @var Handler |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $handler = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor |
|
31
|
|
|
* |
|
32
|
|
|
* @param string|\Closure $handler |
|
33
|
|
|
* @throws Exception |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function __construct( $handler ) { |
|
36
|
1 |
|
$this->handler = new Handler( $handler, '', '\\App\\Controllers\\' ); |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get the handler |
|
41
|
|
|
* |
|
42
|
|
|
* @return Handler |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function get() { |
|
45
|
1 |
|
return $this->handler; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Convert a user returned response to a ResponseInterface instance if possible. |
|
50
|
|
|
* Return the original value if unsupported. |
|
51
|
|
|
* |
|
52
|
|
|
* @param mixed $response |
|
53
|
|
|
* @return mixed |
|
54
|
|
|
*/ |
|
55
|
4 |
|
protected function getResponse( $response ) { |
|
56
|
4 |
|
if ( is_string( $response ) ) { |
|
57
|
1 |
|
return Response::output( $response ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
if ( is_array( $response ) ) { |
|
61
|
1 |
|
return Response::json( $response ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
if ( $response instanceof ResponsableInterface ) { |
|
65
|
1 |
|
return $response->toResponse(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
return $response; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Execute the handler |
|
73
|
|
|
* |
|
74
|
|
|
* @throws Exception |
|
75
|
|
|
* @param mixed ...$arguments |
|
76
|
|
|
* @return ResponseInterface |
|
77
|
|
|
*/ |
|
78
|
5 |
|
public function execute() { |
|
79
|
5 |
|
$response = call_user_func_array( [$this->handler, 'execute'], func_get_args() ); |
|
80
|
5 |
|
$response = $this->getResponse( $response ); |
|
81
|
|
|
|
|
82
|
5 |
|
if ( ! $response instanceof ResponseInterface ) { |
|
83
|
1 |
|
throw new Exception( |
|
84
|
|
|
'Response returned by controller is not valid ' . |
|
85
|
1 |
|
'(expected ' . ResponseInterface::class . '; received ' . gettype( $response ) . ').' |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
return $response; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|