Completed
Push — master ( 2b82a4...1acfba )
by Oscar
02:59
created

Middleware::getAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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