Issues (14)

src/Server/MiddlewareStack.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of slick/http
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slick\Http\Server;
13
14
use Psr\Http\Server\MiddlewareInterface;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Slick\Http\Server\Exception\UnexpectedValueException;
18
use Slick\Http\Server\Middleware\CallableMiddleware;
19
20
/**
21
 * MiddlewareStack
22
 *
23
 * @package Slick\Http\Server
24
 */
25
class MiddlewareStack
26
{
27
    /**
28
     * @var list<MiddlewareInterface|callable>|MiddlewareInterface[]
0 ignored issues
show
The type Slick\Http\Server\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
     */
30
    private array $middlewareStack = [];
31
32
    /**
33
     * Creates a Middleware Stack
34
     *
35
     * @param list<MiddlewareInterface|callable>|MiddlewareInterface[]|callable[] $middlewareStack
36
     */
37
    public function __construct(array $middlewareStack)
38
    {
39
        foreach ($middlewareStack as $middleware) {
40
            $this->push($middleware);
41
        }
42
    }
43
44
    /**
45
     * Pushes a middleware to the stack
46
     *
47
     * @param MiddlewareInterface|callable $middleware
48
     *
49
     * @return MiddlewareStack
50
     */
51
    public function push(MiddlewareInterface|callable $middleware)
52
    {
53
        array_push($this->middlewareStack, $middleware);
54
        return $this;
55
    }
56
57
    /**
58
     * Processes all the middleware stack
59
     *
60
     * @param ServerRequestInterface $request
61
     *
62
     * @return ResponseInterface
63
     */
64
    public function process(ServerRequestInterface $request)
65
    {
66
        $handler = $this->resolve(0);
67
        return $handler->handle($request);
68
    }
69
70
    /**
71
     * Creates a request handler for middleware at the position defined by $index
72
     *
73
     * @param int $index
74
     *
75
     * @return RequestHandler
76
     *
77
     * @throws UnexpectedValueException If the return form a middleware is not a ResponseInterface
78
     */
79
    private function resolve($index)
80
    {
81
        return new RequestHandler(function (ServerRequestInterface $request) use ($index) {
82
83
            $middleware = isset($this->middlewareStack[$index])
84
                ? $this->middlewareStack[$index]
85
                : new CallableMiddleware(function () {
86
                });
87
88
            if ($middleware instanceof \Closure) {
89
                $middleware = new CallableMiddleware($middleware);
90
            }
91
92
            $nextHandler = $this->resolve($index + 1);
93
            $response = match (true) {
94
                $middleware instanceof MiddlewareInterface => $middleware->process($request, $nextHandler),
95
                default => $middleware($request, $nextHandler),
96
            };
97
98
99
            return $response;
100
        });
101
    }
102
}
103