|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * MIT License |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Samuel CHEMLA |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in all |
| 16 | + * copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +namespace PhpBg\Rtsp\Middleware; |
| 28 | + |
| 29 | +use PhpBg\Rtsp\Message\Request; |
| 30 | +use PhpBg\Rtsp\Message\Response; |
| 31 | +use React\Promise\PromiseInterface; |
| 32 | +use React\Socket\ConnectionInterface; |
| 33 | + |
| 34 | +/** |
| 35 | + * Middleware that run a stack of middlewares |
| 36 | + */ |
| 37 | +class MiddlewareStack |
| 38 | +{ |
| 39 | + /** |
| 40 | + * @var callable[] |
| 41 | + */ |
| 42 | + protected $middlewares; |
| 43 | + |
| 44 | + /** |
| 45 | + * @param callable[] $middlewares Stack of middlewares you want to run |
| 46 | + */ |
| 47 | + public function __construct(array $middlewares) |
| 48 | + { |
| 49 | + if (empty($middlewares)) { |
| 50 | + throw new \RuntimeException('No middleware to run'); |
| 51 | + } |
| 52 | + $this->middlewares = array_values($middlewares); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * This middleware just looks like a standard middleware: invokable, receive request and return response |
| 57 | + * It has no $next handler because it is meant to be the last middleware in stack |
| 58 | + * |
| 59 | + * @param Request $request |
| 60 | + * @param ConnectionInterface $connection |
| 61 | + * @return Response|PromiseInterface |
| 62 | + */ |
| 63 | + public function __invoke(Request $request, ConnectionInterface $connection) |
| 64 | + { |
| 65 | + return $this->call($request, $connection, 0); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Recursively executes middlewares |
| 70 | + * |
| 71 | + * @param Request $request |
| 72 | + * @param ConnectionInterface $connection |
| 73 | + * @param int $position |
| 74 | + * @return mixed |
| 75 | + */ |
| 76 | + protected function call(Request $request, ConnectionInterface $connection, int $position) |
| 77 | + { |
| 78 | + // final request handler will be invoked without a next handler |
| 79 | + if (!isset($this->middlewares[$position + 1])) { |
| 80 | + $handler = $this->middlewares[$position]; |
| 81 | + return $handler($request, $connection); |
| 82 | + } |
| 83 | + |
| 84 | + $next = function (Request $request, ConnectionInterface $connection) use ($position) { |
| 85 | + return $this->call($request, $connection, $position + 1); |
| 86 | + }; |
| 87 | + |
| 88 | + // invoke middleware request handler with next handler |
| 89 | + $handler = $this->middlewares[$position]; |
| 90 | + return $handler($request, $connection, $next); |
| 91 | + } |
| 92 | +} |
0 commit comments