MiddlewareFactory::create()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the jade/jade package.
5
 *
6
 * (c) Slince <[email protected]>
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 Jade\Middleware;
13
14
use Psr\Http\Server\MiddlewareInterface;
15
use Zend\Stratigility\Middleware\CallableMiddlewareDecorator;
16
17
final class MiddlewareFactory
18
{
19
    /**
20
     * 创建middleware
21
     *
22
     * @param MiddlewareInterface|string|callable $middleware
23
     * @return MiddlewareInterface
24
     */
25
    public function create($middleware)
26
    {
27
        // middleware 实例直接返回
28
        if ($middleware instanceof MiddlewareInterface) {
29
            return $middleware;
30
        }
31
        // middleware 类
32
        if (is_subclass_of($middleware, MiddlewareInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Psr\Http\Server\MiddlewareInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
33
            return new $middleware;
34
        }
35
        if (is_callable($middleware)) {
36
            return new CallableMiddlewareDecorator($middleware);
37
        }
38
        throw new \InvalidArgumentException('Invalid middleware format');
39
    }
40
}