1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace League\Route\Middleware; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
8
|
|
|
|
9
|
|
|
trait MiddlewareAwareTrait |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $middleware = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
16 |
|
public function middleware(MiddlewareInterface $middleware) : MiddlewareAwareInterface |
20
|
|
|
{ |
21
|
16 |
|
$this->middleware[] = $middleware; |
22
|
|
|
|
23
|
16 |
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
2 |
|
public function middlewares(array $middlewares) : MiddlewareAwareInterface |
30
|
|
|
{ |
31
|
2 |
|
foreach ($middlewares as $middleware) { |
32
|
2 |
|
$this->middleware($middleware); |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
30 |
|
public function prependMiddleware(MiddlewareInterface $middleware) : MiddlewareAwareInterface |
42
|
|
|
{ |
43
|
30 |
|
array_unshift($this->middleware, $middleware); |
44
|
|
|
|
45
|
30 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add a middleware as a class name to the stack |
50
|
|
|
* |
51
|
|
|
* @param string $middleware |
52
|
|
|
* |
53
|
|
|
* @return static |
54
|
|
|
*/ |
55
|
2 |
|
public function lazyMiddleware(string $middleware) : MiddlewareAwareInterface |
56
|
|
|
{ |
57
|
2 |
|
$this->middleware[] = $middleware; |
58
|
|
|
|
59
|
2 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add multiple middlewares as class names to the stack |
64
|
|
|
* |
65
|
|
|
* @param string[] $middlewares |
66
|
|
|
* |
67
|
|
|
* @return static |
68
|
|
|
*/ |
69
|
|
|
public function lazyMiddlewares(array $middlewares) : MiddlewareAwareInterface |
70
|
|
|
{ |
71
|
|
|
foreach ($middlewares as $middleware) { |
72
|
|
|
$this->lazyMiddleware($middleware); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Prepend a middleware as a class name to the stack |
80
|
|
|
* |
81
|
|
|
* @param string $middleware |
82
|
|
|
* |
83
|
|
|
* @return static |
84
|
|
|
*/ |
85
|
|
|
public function lazyPrependMiddleware(string $middleware) : MiddlewareAwareInterface |
86
|
|
|
{ |
87
|
|
|
array_unshift($this->middleware, $middleware); |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
30 |
|
public function shiftMiddleware() : MiddlewareInterface |
96
|
|
|
{ |
97
|
30 |
|
return array_shift($this->middleware); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
32 |
|
public function getMiddlewareStack() : iterable |
104
|
|
|
{ |
105
|
32 |
|
return $this->middleware; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Resolve a middleware implementation, optionally from a container |
110
|
|
|
* |
111
|
|
|
* @param \Psr\Http\Server\MiddlewareInterface|string $middleware |
112
|
|
|
* @param \Psr\Container\ContainerInterface|null $container |
113
|
|
|
* |
114
|
|
|
* @return \Psr\Http\Server\MiddlewareInterface |
115
|
|
|
*/ |
116
|
2 |
|
protected function resolveMiddleware($middleware, ?ContainerInterface $container = null) : MiddlewareInterface |
117
|
|
|
{ |
118
|
2 |
|
if (is_null($container) && is_string($middleware) && class_exists($middleware)) { |
119
|
2 |
|
$middleware = new $middleware; |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
if (! is_null($container) && is_string($middleware) && $container->has($middleware)) { |
123
|
|
|
$middleware = $container->get($middleware); |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
if ($middleware instanceof MiddlewareInterface) { |
|
|
|
|
127
|
2 |
|
return $middleware; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
throw new InvalidArgumentException(sprintf('Could not resolve middleware class: %s', $middleware)); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.