Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Routing;
4
 
5
use Illuminate\Support\Collection;
6
 
7
class SortedMiddleware extends Collection
8
{
9
    /**
10
     * Create a new Sorted Middleware container.
11
     *
12
     * @param  array  $priorityMap
13
     * @param  \Illuminate\Support\Collection|array  $middlewares
14
     * @return void
15
     */
16
    public function __construct(array $priorityMap, $middlewares)
17
    {
18
        if ($middlewares instanceof Collection) {
19
            $middlewares = $middlewares->all();
20
        }
21
 
22
        $this->items = $this->sortMiddleware($priorityMap, $middlewares);
23
    }
24
 
25
    /**
26
     * Sort the middlewares by the given priority map.
27
     *
28
     * Each call to this method makes one discrete middleware movement if necessary.
29
     *
30
     * @param  array  $priorityMap
31
     * @param  array  $middlewares
32
     * @return array
33
     */
34
    protected function sortMiddleware($priorityMap, $middlewares)
35
    {
36
        $lastIndex = 0;
37
 
38
        foreach ($middlewares as $index => $middleware) {
39
            if (! is_string($middleware)) {
40
                continue;
41
            }
42
 
43
            $priorityIndex = $this->priorityMapIndex($priorityMap, $middleware);
44
 
45
            if (! is_null($priorityIndex)) {
46
                // This middleware is in the priority map. If we have encountered another middleware
47
                // that was also in the priority map and was at a lower priority than the current
48
                // middleware, we will move this middleware to be above the previous encounter.
49
                if (isset($lastPriorityIndex) && $priorityIndex < $lastPriorityIndex) {
50
                    return $this->sortMiddleware(
51
                        $priorityMap, array_values($this->moveMiddleware($middlewares, $index, $lastIndex))
52
                    );
53
                }
54
 
55
                // This middleware is in the priority map; but, this is the first middleware we have
56
                // encountered from the map thus far. We'll save its current index plus its index
57
                // from the priority map so we can compare against them on the next iterations.
58
                $lastIndex = $index;
59
 
60
                $lastPriorityIndex = $priorityIndex;
61
            }
62
        }
63
 
64
        return Router::uniqueMiddleware($middlewares);
65
    }
66
 
67
    /**
68
     * Calculate the priority map index of the middleware.
69
     *
70
     * @param  array  $priorityMap
71
     * @param  string  $middleware
72
     * @return int|null
73
     */
74
    protected function priorityMapIndex($priorityMap, $middleware)
75
    {
76
        foreach ($this->middlewareNames($middleware) as $name) {
77
            $priorityIndex = array_search($name, $priorityMap);
78
 
79
            if ($priorityIndex !== false) {
80
                return $priorityIndex;
81
            }
82
        }
83
    }
84
 
85
    /**
86
     * Resolve the middleware names to look for in the priority array.
87
     *
88
     * @param  string  $middleware
89
     * @return \Generator
90
     */
91
    protected function middlewareNames($middleware)
92
    {
93
        $stripped = head(explode(':', $middleware));
94
 
95
        yield $stripped;
96
 
97
        $interfaces = @class_implements($stripped);
98
 
99
        if ($interfaces !== false) {
100
            foreach ($interfaces as $interface) {
101
                yield $interface;
102
            }
103
        }
104
    }
105
 
106
    /**
107
     * Splice a middleware into a new position and remove the old entry.
108
     *
109
     * @param  array  $middlewares
110
     * @param  int  $from
111
     * @param  int  $to
112
     * @return array
113
     */
114
    protected function moveMiddleware($middlewares, $from, $to)
115
    {
116
        array_splice($middlewares, $to, 0, $middlewares[$from]);
117
 
118
        unset($middlewares[$from + 1]);
119
 
120
        return $middlewares;
121
    }
122
}