1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shoot\Shoot; |
5
|
|
|
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use Shoot\Shoot\Middleware\SuppressionMiddleware; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The processing pipeline of Shoot. Holds the middleware that enables Shoot's functionality. It's called from the Twig |
11
|
|
|
* extension. |
12
|
|
|
*/ |
13
|
|
|
final class Pipeline |
14
|
|
|
{ |
15
|
|
|
/** @var callable */ |
16
|
|
|
private $middleware; |
17
|
|
|
|
18
|
|
|
/** @var ServerRequestInterface|null */ |
19
|
|
|
private $request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructs an instance of Pipeline. Takes the middleware that enables Shoot's functionality. Middleware is |
23
|
|
|
* executed in the same order as given. |
24
|
|
|
* |
25
|
|
|
* @param MiddlewareInterface[] $middleware |
26
|
|
|
*/ |
27
|
20 |
|
public function __construct(array $middleware = []) |
28
|
|
|
{ |
29
|
20 |
|
$this->middleware = $this->chainMiddleware($middleware); |
30
|
20 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets the HTTP request context while executing the given callback. Any templates should be rendered within this |
34
|
|
|
* callback. Returns the result returned by the callback (if any). |
35
|
|
|
* |
36
|
|
|
* @param ServerRequestInterface $request |
37
|
|
|
* @param callable $callback |
38
|
|
|
* |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
17 |
|
public function withRequest(ServerRequestInterface $request, callable $callback) |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
17 |
|
$this->request = $request; |
45
|
|
|
|
46
|
17 |
|
return $callback(); |
47
|
|
|
} finally { |
48
|
17 |
|
$this->request = null; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param View $view |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
* |
57
|
|
|
* @internal |
58
|
|
|
*/ |
59
|
17 |
|
public function process(View $view): void |
60
|
|
|
{ |
61
|
17 |
|
if ($this->request === null) { |
62
|
1 |
|
throw new MissingRequestException('Cannot process a view without a request set. This method should be called from the callback passed to Pipeline::withRequest'); |
63
|
|
|
} |
64
|
|
|
|
65
|
16 |
|
call_user_func($this->middleware, $view); |
66
|
14 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param MiddlewareInterface[] $middleware |
70
|
|
|
* |
71
|
|
|
* @return callable |
72
|
|
|
*/ |
73
|
20 |
|
private function chainMiddleware(array $middleware): callable |
74
|
|
|
{ |
75
|
20 |
|
$middleware = array_reverse($middleware); |
76
|
|
|
|
77
|
|
|
return array_reduce($middleware, function (callable $next, MiddlewareInterface $middleware): callable { |
78
|
|
|
return function (View $view) use ($middleware, $next): View { |
79
|
15 |
|
return $middleware->process($view, $this->request, $next); |
|
|
|
|
80
|
16 |
|
}; |
81
|
|
|
}, function (View $view): View { |
82
|
12 |
|
$view->render(); |
83
|
|
|
|
84
|
10 |
|
return $view; |
85
|
20 |
|
}); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|