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 BadMethodCallException;
6
use Closure;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Reflector;
9
use InvalidArgumentException;
10
 
11
/**
12
 * @method \Illuminate\Routing\Route any(string $uri, \Closure|array|string|null $action = null)
13
 * @method \Illuminate\Routing\Route delete(string $uri, \Closure|array|string|null $action = null)
14
 * @method \Illuminate\Routing\Route get(string $uri, \Closure|array|string|null $action = null)
15
 * @method \Illuminate\Routing\Route options(string $uri, \Closure|array|string|null $action = null)
16
 * @method \Illuminate\Routing\Route patch(string $uri, \Closure|array|string|null $action = null)
17
 * @method \Illuminate\Routing\Route post(string $uri, \Closure|array|string|null $action = null)
18
 * @method \Illuminate\Routing\Route put(string $uri, \Closure|array|string|null $action = null)
19
 * @method \Illuminate\Routing\RouteRegistrar as(string $value)
20
 * @method \Illuminate\Routing\RouteRegistrar controller(string $controller)
21
 * @method \Illuminate\Routing\RouteRegistrar domain(string $value)
22
 * @method \Illuminate\Routing\RouteRegistrar middleware(array|string|null $middleware)
23
 * @method \Illuminate\Routing\RouteRegistrar name(string $value)
24
 * @method \Illuminate\Routing\RouteRegistrar namespace(string|null $value)
25
 * @method \Illuminate\Routing\RouteRegistrar prefix(string $prefix)
26
 * @method \Illuminate\Routing\RouteRegistrar scopeBindings()
27
 * @method \Illuminate\Routing\RouteRegistrar where(array $where)
28
 * @method \Illuminate\Routing\RouteRegistrar withoutMiddleware(array|string $middleware)
29
 * @method \Illuminate\Routing\RouteRegistrar withoutScopedBindings()
30
 */
31
class RouteRegistrar
32
{
33
    use CreatesRegularExpressionRouteConstraints;
34
 
35
    /**
36
     * The router instance.
37
     *
38
     * @var \Illuminate\Routing\Router
39
     */
40
    protected $router;
41
 
42
    /**
43
     * The attributes to pass on to the router.
44
     *
45
     * @var array
46
     */
47
    protected $attributes = [];
48
 
49
    /**
50
     * The methods to dynamically pass through to the router.
51
     *
52
     * @var string[]
53
     */
54
    protected $passthru = [
55
        'get', 'post', 'put', 'patch', 'delete', 'options', 'any',
56
    ];
57
 
58
    /**
59
     * The attributes that can be set through this class.
60
     *
61
     * @var string[]
62
     */
63
    protected $allowedAttributes = [
64
        'as',
65
        'controller',
66
        'domain',
67
        'middleware',
68
        'name',
69
        'namespace',
70
        'prefix',
71
        'scopeBindings',
72
        'where',
73
        'withoutMiddleware',
74
    ];
75
 
76
    /**
77
     * The attributes that are aliased.
78
     *
79
     * @var array
80
     */
81
    protected $aliases = [
82
        'name' => 'as',
83
        'scopeBindings' => 'scope_bindings',
84
        'withoutMiddleware' => 'excluded_middleware',
85
    ];
86
 
87
    /**
88
     * Create a new route registrar instance.
89
     *
90
     * @param  \Illuminate\Routing\Router  $router
91
     * @return void
92
     */
93
    public function __construct(Router $router)
94
    {
95
        $this->router = $router;
96
    }
97
 
98
    /**
99
     * Set the value for a given attribute.
100
     *
101
     * @param  string  $key
102
     * @param  mixed  $value
103
     * @return $this
104
     *
105
     * @throws \InvalidArgumentException
106
     */
107
    public function attribute($key, $value)
108
    {
109
        if (! in_array($key, $this->allowedAttributes)) {
110
            throw new InvalidArgumentException("Attribute [{$key}] does not exist.");
111
        }
112
 
113
        if ($key === 'middleware') {
114
            foreach ($value as $index => $middleware) {
115
                $value[$index] = (string) $middleware;
116
            }
117
        }
118
 
119
        $attributeKey = Arr::get($this->aliases, $key, $key);
120
 
121
        if ($key === 'withoutMiddleware') {
122
            $value = array_merge(
123
                (array) ($this->attributes[$attributeKey] ?? []), Arr::wrap($value)
124
            );
125
        }
126
 
127
        $this->attributes[$attributeKey] = $value;
128
 
129
        return $this;
130
    }
131
 
132
    /**
133
     * Route a resource to a controller.
134
     *
135
     * @param  string  $name
136
     * @param  string  $controller
137
     * @param  array  $options
138
     * @return \Illuminate\Routing\PendingResourceRegistration
139
     */
140
    public function resource($name, $controller, array $options = [])
141
    {
142
        return $this->router->resource($name, $controller, $this->attributes + $options);
143
    }
144
 
145
    /**
146
     * Route an API resource to a controller.
147
     *
148
     * @param  string  $name
149
     * @param  string  $controller
150
     * @param  array  $options
151
     * @return \Illuminate\Routing\PendingResourceRegistration
152
     */
153
    public function apiResource($name, $controller, array $options = [])
154
    {
155
        return $this->router->apiResource($name, $controller, $this->attributes + $options);
156
    }
157
 
158
    /**
159
     * Route a singleton resource to a controller.
160
     *
161
     * @param  string  $name
162
     * @param  string  $controller
163
     * @param  array  $options
164
     * @return \Illuminate\Routing\PendingSingletonResourceRegistration
165
     */
166
    public function singleton($name, $controller, array $options = [])
167
    {
168
        return $this->router->singleton($name, $controller, $this->attributes + $options);
169
    }
170
 
171
    /**
172
     * Route an API singleton resource to a controller.
173
     *
174
     * @param  string  $name
175
     * @param  string  $controller
176
     * @param  array  $options
177
     * @return \Illuminate\Routing\PendingSingletonResourceRegistration
178
     */
179
    public function apiSingleton($name, $controller, array $options = [])
180
    {
181
        return $this->router->apiSingleton($name, $controller, $this->attributes + $options);
182
    }
183
 
184
    /**
185
     * Create a route group with shared attributes.
186
     *
187
     * @param  \Closure|array|string  $callback
188
     * @return $this
189
     */
190
    public function group($callback)
191
    {
192
        $this->router->group($this->attributes, $callback);
193
 
194
        return $this;
195
    }
196
 
197
    /**
198
     * Register a new route with the given verbs.
199
     *
200
     * @param  array|string  $methods
201
     * @param  string  $uri
202
     * @param  \Closure|array|string|null  $action
203
     * @return \Illuminate\Routing\Route
204
     */
205
    public function match($methods, $uri, $action = null)
206
    {
207
        return $this->router->match($methods, $uri, $this->compileAction($action));
208
    }
209
 
210
    /**
211
     * Register a new route with the router.
212
     *
213
     * @param  string  $method
214
     * @param  string  $uri
215
     * @param  \Closure|array|string|null  $action
216
     * @return \Illuminate\Routing\Route
217
     */
218
    protected function registerRoute($method, $uri, $action = null)
219
    {
220
        if (! is_array($action)) {
221
            $action = array_merge($this->attributes, $action ? ['uses' => $action] : []);
222
        }
223
 
224
        return $this->router->{$method}($uri, $this->compileAction($action));
225
    }
226
 
227
    /**
228
     * Compile the action into an array including the attributes.
229
     *
230
     * @param  \Closure|array|string|null  $action
231
     * @return array
232
     */
233
    protected function compileAction($action)
234
    {
235
        if (is_null($action)) {
236
            return $this->attributes;
237
        }
238
 
239
        if (is_string($action) || $action instanceof Closure) {
240
            $action = ['uses' => $action];
241
        }
242
 
243
        if (is_array($action) &&
244
            ! Arr::isAssoc($action) &&
245
            Reflector::isCallable($action)) {
246
            if (strncmp($action[0], '\\', 1)) {
247
                $action[0] = '\\'.$action[0];
248
            }
249
            $action = [
250
                'uses' => $action[0].'@'.$action[1],
251
                'controller' => $action[0].'@'.$action[1],
252
            ];
253
        }
254
 
255
        return array_merge($this->attributes, $action);
256
    }
257
 
258
    /**
259
     * Dynamically handle calls into the route registrar.
260
     *
261
     * @param  string  $method
262
     * @param  array  $parameters
263
     * @return \Illuminate\Routing\Route|$this
264
     *
265
     * @throws \BadMethodCallException
266
     */
267
    public function __call($method, $parameters)
268
    {
269
        if (in_array($method, $this->passthru)) {
270
            return $this->registerRoute($method, ...$parameters);
271
        }
272
 
273
        if (in_array($method, $this->allowedAttributes)) {
274
            if ($method === 'middleware') {
275
                return $this->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
276
            }
277
 
278
            return $this->attribute($method, array_key_exists(0, $parameters) ? $parameters[0] : true);
279
        }
280
 
281
        throw new BadMethodCallException(sprintf(
282
            'Method %s::%s does not exist.', static::class, $method
283
        ));
284
    }
285
}