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\Container\Container;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Arr;
8
 
9
class RouteCollection extends AbstractRouteCollection
10
{
11
    /**
12
     * An array of the routes keyed by method.
13
     *
14
     * @var array
15
     */
16
    protected $routes = [];
17
 
18
    /**
19
     * A flattened array of all of the routes.
20
     *
21
     * @var \Illuminate\Routing\Route[]
22
     */
23
    protected $allRoutes = [];
24
 
25
    /**
26
     * A look-up table of routes by their names.
27
     *
28
     * @var \Illuminate\Routing\Route[]
29
     */
30
    protected $nameList = [];
31
 
32
    /**
33
     * A look-up table of routes by controller action.
34
     *
35
     * @var \Illuminate\Routing\Route[]
36
     */
37
    protected $actionList = [];
38
 
39
    /**
40
     * Add a Route instance to the collection.
41
     *
42
     * @param  \Illuminate\Routing\Route  $route
43
     * @return \Illuminate\Routing\Route
44
     */
45
    public function add(Route $route)
46
    {
47
        $this->addToCollections($route);
48
 
49
        $this->addLookups($route);
50
 
51
        return $route;
52
    }
53
 
54
    /**
55
     * Add the given route to the arrays of routes.
56
     *
57
     * @param  \Illuminate\Routing\Route  $route
58
     * @return void
59
     */
60
    protected function addToCollections($route)
61
    {
62
        $domainAndUri = $route->getDomain().$route->uri();
63
 
64
        foreach ($route->methods() as $method) {
65
            $this->routes[$method][$domainAndUri] = $route;
66
        }
67
 
68
        $this->allRoutes[$method.$domainAndUri] = $route;
69
    }
70
 
71
    /**
72
     * Add the route to any look-up tables if necessary.
73
     *
74
     * @param  \Illuminate\Routing\Route  $route
75
     * @return void
76
     */
77
    protected function addLookups($route)
78
    {
79
        // If the route has a name, we will add it to the name look-up table so that we
80
        // will quickly be able to find any route associate with a name and not have
81
        // to iterate through every route every time we need to perform a look-up.
82
        if ($name = $route->getName()) {
83
            $this->nameList[$name] = $route;
84
        }
85
 
86
        // When the route is routing to a controller we will also store the action that
87
        // is used by the route. This will let us reverse route to controllers while
88
        // processing a request and easily generate URLs to the given controllers.
89
        $action = $route->getAction();
90
 
91
        if (isset($action['controller'])) {
92
            $this->addToActionList($action, $route);
93
        }
94
    }
95
 
96
    /**
97
     * Add a route to the controller action dictionary.
98
     *
99
     * @param  array  $action
100
     * @param  \Illuminate\Routing\Route  $route
101
     * @return void
102
     */
103
    protected function addToActionList($action, $route)
104
    {
105
        $this->actionList[trim($action['controller'], '\\')] = $route;
106
    }
107
 
108
    /**
109
     * Refresh the name look-up table.
110
     *
111
     * This is done in case any names are fluently defined or if routes are overwritten.
112
     *
113
     * @return void
114
     */
115
    public function refreshNameLookups()
116
    {
117
        $this->nameList = [];
118
 
119
        foreach ($this->allRoutes as $route) {
120
            if ($route->getName()) {
121
                $this->nameList[$route->getName()] = $route;
122
            }
123
        }
124
    }
125
 
126
    /**
127
     * Refresh the action look-up table.
128
     *
129
     * This is done in case any actions are overwritten with new controllers.
130
     *
131
     * @return void
132
     */
133
    public function refreshActionLookups()
134
    {
135
        $this->actionList = [];
136
 
137
        foreach ($this->allRoutes as $route) {
138
            if (isset($route->getAction()['controller'])) {
139
                $this->addToActionList($route->getAction(), $route);
140
            }
141
        }
142
    }
143
 
144
    /**
145
     * Find the first route matching a given request.
146
     *
147
     * @param  \Illuminate\Http\Request  $request
148
     * @return \Illuminate\Routing\Route
149
     *
150
     * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
151
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
152
     */
153
    public function match(Request $request)
154
    {
155
        $routes = $this->get($request->getMethod());
156
 
157
        // First, we will see if we can find a matching route for this current request
158
        // method. If we can, great, we can just return it so that it can be called
159
        // by the consumer. Otherwise we will check for routes with another verb.
160
        $route = $this->matchAgainstRoutes($routes, $request);
161
 
162
        return $this->handleMatchedRoute($request, $route);
163
    }
164
 
165
    /**
166
     * Get routes from the collection by method.
167
     *
168
     * @param  string|null  $method
169
     * @return \Illuminate\Routing\Route[]
170
     */
171
    public function get($method = null)
172
    {
173
        return is_null($method) ? $this->getRoutes() : Arr::get($this->routes, $method, []);
174
    }
175
 
176
    /**
177
     * Determine if the route collection contains a given named route.
178
     *
179
     * @param  string  $name
180
     * @return bool
181
     */
182
    public function hasNamedRoute($name)
183
    {
184
        return ! is_null($this->getByName($name));
185
    }
186
 
187
    /**
188
     * Get a route instance by its name.
189
     *
190
     * @param  string  $name
191
     * @return \Illuminate\Routing\Route|null
192
     */
193
    public function getByName($name)
194
    {
195
        return $this->nameList[$name] ?? null;
196
    }
197
 
198
    /**
199
     * Get a route instance by its controller action.
200
     *
201
     * @param  string  $action
202
     * @return \Illuminate\Routing\Route|null
203
     */
204
    public function getByAction($action)
205
    {
206
        return $this->actionList[$action] ?? null;
207
    }
208
 
209
    /**
210
     * Get all of the routes in the collection.
211
     *
212
     * @return \Illuminate\Routing\Route[]
213
     */
214
    public function getRoutes()
215
    {
216
        return array_values($this->allRoutes);
217
    }
218
 
219
    /**
220
     * Get all of the routes keyed by their HTTP verb / method.
221
     *
222
     * @return array
223
     */
224
    public function getRoutesByMethod()
225
    {
226
        return $this->routes;
227
    }
228
 
229
    /**
230
     * Get all of the routes keyed by their name.
231
     *
232
     * @return \Illuminate\Routing\Route[]
233
     */
234
    public function getRoutesByName()
235
    {
236
        return $this->nameList;
237
    }
238
 
239
    /**
240
     * Convert the collection to a Symfony RouteCollection instance.
241
     *
242
     * @return \Symfony\Component\Routing\RouteCollection
243
     */
244
    public function toSymfonyRouteCollection()
245
    {
246
        $symfonyRoutes = parent::toSymfonyRouteCollection();
247
 
248
        $this->refreshNameLookups();
249
 
250
        return $symfonyRoutes;
251
    }
252
 
253
    /**
254
     * Convert the collection to a CompiledRouteCollection instance.
255
     *
256
     * @param  \Illuminate\Routing\Router  $router
257
     * @param  \Illuminate\Container\Container  $container
258
     * @return \Illuminate\Routing\CompiledRouteCollection
259
     */
260
    public function toCompiledRouteCollection(Router $router, Container $container)
261
    {
262
        ['compiled' => $compiled, 'attributes' => $attributes] = $this->compile();
263
 
264
        return (new CompiledRouteCollection($compiled, $attributes))
265
            ->setRouter($router)
266
            ->setContainer($container);
267
    }
268
}