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 */ |
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
|
14 |
|
public function __construct(array $middleware = []) |
28
|
|
|
{ |
29
|
14 |
|
$middleware = $this->addSuppressionMiddleware($middleware); |
|
|
|
|
30
|
|
|
|
31
|
14 |
|
$this->middleware = $this->chainMiddleware($middleware); |
32
|
14 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sets the HTTP request context while executing the given callback. Any templates should be rendered within this |
36
|
|
|
* callback. Returns the result returned by the callback (if any). |
37
|
|
|
* |
38
|
|
|
* @param ServerRequestInterface $request |
39
|
|
|
* @param callable $callback |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
11 |
|
public function withRequest(ServerRequestInterface $request, callable $callback) |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
11 |
|
$this->request = $request; |
47
|
|
|
|
48
|
11 |
|
return $callback(); |
49
|
|
|
} finally { |
50
|
11 |
|
$this->request = null; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param View $view |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
* |
59
|
|
|
* @internal |
60
|
|
|
*/ |
61
|
11 |
|
public function process(View $view) |
62
|
|
|
{ |
63
|
11 |
|
if ($this->request === null) { |
64
|
1 |
|
throw new MissingRequestException('Cannot process a view without a request set. This method should be called from the callback passed to Pipeline::withRequest'); |
65
|
|
|
} |
66
|
|
|
|
67
|
10 |
|
call_user_func($this->middleware, $view); |
68
|
9 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param MiddlewareInterface[] $middleware |
72
|
|
|
* |
73
|
|
|
* @return MiddlewareInterface[] |
74
|
|
|
* |
75
|
|
|
* @deprecated 2.0.0 Should not have been default behaviour. Will be removed as of the next major release. |
76
|
|
|
*/ |
77
|
14 |
|
private function addSuppressionMiddleware(array $middleware): array |
78
|
|
|
{ |
79
|
14 |
|
foreach ($middleware as $instance) { |
80
|
10 |
|
if ($instance instanceof SuppressionMiddleware) { |
81
|
10 |
|
return $middleware; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
14 |
|
$middleware[] = new SuppressionMiddleware(); |
86
|
|
|
|
87
|
14 |
|
return $middleware; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param MiddlewareInterface[] $middleware |
92
|
|
|
* |
93
|
|
|
* @return callable |
94
|
|
|
*/ |
95
|
14 |
|
private function chainMiddleware(array $middleware): callable |
96
|
|
|
{ |
97
|
14 |
|
$middleware = array_reverse($middleware); |
98
|
|
|
|
99
|
|
|
return array_reduce($middleware, function (callable $next, MiddlewareInterface $middleware) { |
100
|
14 |
|
return function (View $view) use ($middleware, $next): View { |
101
|
10 |
|
return $middleware->process($view, $this->request, $next); |
102
|
14 |
|
}; |
103
|
|
|
}, function (View $view): View { |
104
|
7 |
|
$view->render(); |
105
|
|
|
|
106
|
4 |
|
return $view; |
107
|
14 |
|
}); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.