|
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 WPEmerge\Middleware\ExecutesMiddlewareTrait; |
|
13
|
|
|
use WPEmerge\Middleware\HasMiddlewareInterface; |
|
14
|
|
|
use WPEmerge\Middleware\HasMiddlewareTrait; |
|
15
|
|
|
use WPEmerge\Requests\RequestInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Represent middleware that envelops a handler. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Pipeline implements HasMiddlewareInterface { |
|
21
|
|
|
use HasMiddlewareTrait; |
|
22
|
|
|
use ExecutesMiddlewareTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Pipeline handler. |
|
26
|
|
|
* |
|
27
|
|
|
* @var PipelineHandler |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $handler = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get handler. |
|
33
|
|
|
* |
|
34
|
|
|
* @return PipelineHandler |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function getHandler() { |
|
37
|
1 |
|
return $this->handler; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Set handler. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string|\Closure $handler |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function setHandler( $handler ) { |
|
47
|
1 |
|
$this->handler = new PipelineHandler( $handler ); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Fluent alias for setHandler(). |
|
52
|
|
|
* |
|
53
|
|
|
* @param string|\Closure $handler |
|
54
|
|
|
* @return self $this |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function to( $handler ) { |
|
57
|
1 |
|
call_user_func_array( [$this, 'setHandler'], func_get_args() ); |
|
58
|
|
|
|
|
59
|
1 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get a response for the given request. |
|
64
|
|
|
* |
|
65
|
|
|
* @param RequestInterface $request |
|
66
|
|
|
* @param array $arguments |
|
67
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
public function run( RequestInterface $request, $arguments ) { |
|
70
|
1 |
|
return $this->executeMiddleware( $this->getMiddleware(), $request, function () use ( $arguments ) { |
|
71
|
1 |
|
return call_user_func_array( [$this->getHandler(), 'execute'], $arguments ); |
|
72
|
1 |
|
} ); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|