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