Middleware::setStreamFactory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Psr7Middlewares;
4
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use RuntimeException;
8
use InvalidArgumentException;
9
10
class Middleware
11
{
12
    const KEY = 'Psr7Middlewares\\Middleware';
13
    const STORAGE_KEY = 'STORAGE_KEY';
14
15
    private static $streamFactory;
16
    private static $namespaces = ['Psr7Middlewares\\Middleware\\'];
17
18
    /**
19
     * Register a new namespace.
20
     *
21
     * @param string $namespace
22
     * @param bool   $prepend
23
     */
24
    public static function registerNamespace($namespace, $prepend = false)
25
    {
26
        if (false === $prepend) {
27
            self::$namespaces[] = $namespace;
28
        } else {
29
            array_unshift(self::$namespaces, $namespace);
30
        }
31
    }
32
33
    /**
34
     * Set the stream factory used by some middlewares.
35
     *
36
     * @param callable $streamFactory
37
     */
38
    public static function setStreamFactory(callable $streamFactory)
39
    {
40
        self::$streamFactory = $streamFactory;
41
    }
42
43
    /**
44
     * Set the stream factory used by some middlewares.
45
     *
46
     * @param callable|null
47
     */
48
    public static function getStreamFactory()
49
    {
50
        return self::$streamFactory;
51
    }
52
53
    /**
54
     * Create instances of the middlewares.
55
     *
56
     * @param string $name
57
     * @param array  $args
58
     */
59
    public static function __callStatic($name, $args)
60
    {
61
        foreach (self::$namespaces as $namespace) {
62
            $class = $namespace.ucfirst($name);
63
64
            if (class_exists($class)) {
65
                switch (count($args)) {
66
                    case 0:
67
                        return new $class();
68
69
                    case 1:
70
                        return new $class($args[0]);
71
72
                    default:
73
                        return (new \ReflectionClass($class))->newInstanceArgs($args);
74
                }
75
            }
76
        }
77
78
        throw new RuntimeException("The middleware {$name} does not exist");
79
    }
80
81
    /**
82
     * Create a middleware callable that acts as a "proxy" to a real middleware that must be returned by the given callback.
83
     *
84
     * @param callable|string $basePath The base path in which the middleware is created (optional)
85
     * @param callable        $factory  Takes no argument and MUST return a middleware callable or false
86
     *
87
     * @return callable
88
     */
89
    public static function create($basePath, callable $factory = null)
90
    {
91
        if ($factory === null) {
92
            $factory = $basePath;
93
            $basePath = '';
94
        }
95
96
        if (!is_callable($factory)) {
97
            throw new InvalidArgumentException('Invalid callable provided');
98
        }
99
100
        return function (RequestInterface $request, ResponseInterface $response, callable $next) use ($basePath, $factory) {
101
            $path = rtrim($request->getUri()->getPath(), '/');
102
            $basePath = rtrim($basePath, '/');
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $basePath, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
103
104
            if ($path === $basePath || strpos($path, "{$basePath}/") === 0) {
105
                $middleware = $factory($request, $response);
106
            } else {
107
                $middleware = false;
108
            }
109
110
            if ($middleware === false) {
111
                return $next($request, $response);
112
            }
113
114
            if (!is_callable($middleware)) {
115
                throw new RuntimeException(sprintf('Factory returned "%s" instead of a callable or FALSE.', gettype($middleware)));
116
            }
117
118
            return $middleware($request, $response, $next);
119
        };
120
    }
121
}
122