GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — 3.x (#1963)
by Glenn
03:10
created

Route::setCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Slim Framework (http://slimframework.com)
4
 *
5
 * @link      https://github.com/slimphp/Slim
6
 * @copyright Copyright (c) 2011-2016 Josh Lockhart
7
 * @license   https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
8
 */
9
namespace Slim;
10
11
use Exception;
12
use InvalidArgumentException;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
use Slim\Exception\SlimException;
16
use Slim\Handlers\Strategies\RequestResponse;
17
use Slim\Interfaces\InvocationStrategyInterface;
18
use Slim\Interfaces\RouteInterface;
19
20
/**
21
 * Route
22
 */
23
class Route extends Routable implements RouteInterface
24
{
25
    use MiddlewareAwareTrait;
26
27
    /**
28
     * HTTP methods supported by this route
29
     *
30
     * @var string[]
31
     */
32
    protected $methods = [];
33
34
    /**
35
     * Route identifier
36
     *
37
     * @var string
38
     */
39
    protected $identifier;
40
41
    /**
42
     * Route name
43
     *
44
     * @var null|string
45
     */
46
    protected $name;
47
48
    /**
49
     * Parent route groups
50
     *
51
     * @var RouteGroup[]
52
     */
53
    protected $groups;
54
55
    private $finalized = false;
56
57
    /**
58
     * Output buffering mode
59
     *
60
     * One of: false, 'prepend' or 'append'
61
     *
62
     * @var boolean|string
63
     */
64
    protected $outputBuffering = 'append';
65
66
    /**
67
     * Route parameters
68
     *
69
     * @var array
70
     */
71
    protected $arguments = [];
72
73
    /**
74
     * Create new route
75
     *
76
     * @param string|string[]   $methods The route HTTP methods
77
     * @param string            $pattern The route pattern
78
     * @param callable          $callable The route callable
79
     * @param RouteGroup[]      $groups The parent route groups
80
     * @param int               $identifier The route identifier
81
     */
82
    public function __construct($methods, $pattern, $callable, $groups = [], $identifier = 0)
83
    {
84
        $this->methods  = is_string($methods) ? [$methods] : $methods;
85
        $this->pattern  = $pattern;
86
        $this->callable = $callable;
87
        $this->groups   = $groups;
88
        $this->identifier = 'route' . $identifier;
89
    }
90
91
    /**
92
     * Finalize the route in preparation for dispatching
93
     */
94
    public function finalize()
95
    {
96
        if ($this->finalized) {
97
            return;
98
        }
99
100
        $groupMiddleware = [];
101
        foreach ($this->getGroups() as $group) {
102
            $groupMiddleware = array_merge($group->getMiddleware(), $groupMiddleware);
103
        }
104
105
        $this->middleware = array_merge($this->middleware, $groupMiddleware);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->middleware, $groupMiddleware) of type array is incompatible with the declared type array<integer,callable> of property $middleware.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
106
107
        foreach ($this->getMiddleware() as $middleware) {
108
            $this->addMiddleware($middleware);
109
        }
110
111
        $this->finalized = true;
112
    }
113
114
    /**
115
     * Get route callable
116
     *
117
     * @return callable
118
     */
119
    public function getCallable()
120
    {
121
        return $this->callable;
122
    }
123
124
    /**
125
     * This method enables you to override the Route's callable
126
     *
127
     * @param string|\Closure $callable
128
     */
129
    public function setCallable($callable)
130
    {
131
        $this->callable = $callable;
132
    }
133
134
    /**
135
     * Get route methods
136
     *
137
     * @return string[]
138
     */
139
    public function getMethods()
140
    {
141
        return $this->methods;
142
    }
143
144
    /**
145
     * Get parent route groups
146
     *
147
     * @return RouteGroup[]
148
     */
149
    public function getGroups()
150
    {
151
        return $this->groups;
152
    }
153
154
    /**
155
     * Get route name
156
     *
157
     * @return null|string
158
     */
159
    public function getName()
160
    {
161
        return $this->name;
162
    }
163
164
    /**
165
     * Get route identifier
166
     *
167
     * @return string
168
     */
169
    public function getIdentifier()
170
    {
171
        return $this->identifier;
172
    }
173
174
    /**
175
     * Get output buffering mode
176
     *
177
     * @return boolean|string
178
     */
179
    public function getOutputBuffering()
180
    {
181
        return $this->outputBuffering;
182
    }
183
184
    /**
185
     * Set output buffering mode
186
     *
187
     * One of: false, 'prepend' or 'append'
188
     *
189
     * @param boolean|string $mode
190
     *
191
     * @throws InvalidArgumentException If an unknown buffering mode is specified
192
     */
193
    public function setOutputBuffering($mode)
194
    {
195
        if (!in_array($mode, [false, 'prepend', 'append'], true)) {
196
            throw new InvalidArgumentException('Unknown output buffering mode');
197
        }
198
        $this->outputBuffering = $mode;
199
    }
200
201
    /**
202
     * Set route name
203
     *
204
     * @param string $name
205
     *
206
     * @return self
207
     *
208
     * @throws InvalidArgumentException if the route name is not a string
209
     */
210
    public function setName($name)
211
    {
212
        if (!is_string($name)) {
213
            throw new InvalidArgumentException('Route name must be a string');
214
        }
215
        $this->name = $name;
216
        return $this;
217
    }
218
219
    /**
220
     * Set a route argument
221
     *
222
     * @param string $name
223
     * @param string $value
224
     *
225
     * @return self
226
     */
227
    public function setArgument($name, $value)
228
    {
229
        $this->arguments[$name] = $value;
230
        return $this;
231
    }
232
233
    /**
234
     * Replace route arguments
235
     *
236
     * @param array $arguments
237
     *
238
     * @return self
239
     */
240
    public function setArguments(array $arguments)
241
    {
242
        $this->arguments = $arguments;
243
        return $this;
244
    }
245
246
    /**
247
     * Retrieve route arguments
248
     *
249
     * @return array
250
     */
251
    public function getArguments()
252
    {
253
        return $this->arguments;
254
    }
255
256
    /**
257
     * Retrieve a specific route argument
258
     *
259
     * @param string $name
260
     * @param mixed $default
261
     *
262
     * @return mixed
263
     */
264
    public function getArgument($name, $default = null)
265
    {
266
        if (array_key_exists($name, $this->arguments)) {
267
            return $this->arguments[$name];
268
        }
269
        return $default;
270
    }
271
272
    /********************************************************************************
273
     * Route Runner
274
     *******************************************************************************/
275
276
    /**
277
     * Prepare the route for use
278
     *
279
     * @param ServerRequestInterface $request
280
     * @param array $arguments
281
     */
282
    public function prepare(ServerRequestInterface $request, array $arguments)
283
    {
284
        // Add the arguments
285
        foreach ($arguments as $k => $v) {
286
            $this->setArgument($k, $v);
287
        }
288
    }
289
290
    /**
291
     * Run route
292
     *
293
     * This method traverses the middleware stack, including the route's callable
294
     * and captures the resultant HTTP response object. It then sends the response
295
     * back to the Application.
296
     *
297
     * @param ServerRequestInterface $request
298
     * @param ResponseInterface      $response
299
     *
300
     * @return ResponseInterface
301
     */
302
    public function run(ServerRequestInterface $request, ResponseInterface $response)
303
    {
304
        // Finalise route now that we are about to run it
305
        $this->finalize();
306
307
        // Traverse middleware stack and fetch updated response
308
        return $this->callMiddlewareStack($request, $response);
309
    }
310
311
    /**
312
     * Dispatch route callable against current Request and Response objects
313
     *
314
     * This method invokes the route object's callable. If middleware is
315
     * registered for the route, each callable middleware is invoked in
316
     * the order specified.
317
     *
318
     * @param ServerRequestInterface $request  The current Request object
319
     * @param ResponseInterface      $response The current Response object
320
     * @return \Psr\Http\Message\ResponseInterface
321
     * @throws \Exception  if the route callable throws an exception
322
     */
323
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
324
    {
325
        $this->callable = $this->resolveCallable($this->callable);
326
327
        /** @var InvocationStrategyInterface $handler */
328
        $handler = isset($this->container) ? $this->container->get('foundHandler') : new RequestResponse();
329
330
        // invoke route callable
331
        if ($this->outputBuffering === false) {
332
            $newResponse = $handler($this->callable, $request, $response, $this->arguments);
333
        } else {
334
            try {
335
                ob_start();
336
                $newResponse = $handler($this->callable, $request, $response, $this->arguments);
337
                $output = ob_get_clean();
338
            } catch (Exception $e) {
339
                ob_end_clean();
340
                throw $e;
341
            }
342
        }
343
344
        if ($newResponse instanceof ResponseInterface) {
345
            // if route callback returns a ResponseInterface, then use it
346
            $response = $newResponse;
347
        } elseif (is_string($newResponse)) {
348
            // if route callback returns a string, then append it to the response
349
            if ($response->getBody()->isWritable()) {
350
                $response->getBody()->write($newResponse);
351
            }
352
        }
353
354
        if (!empty($output) && $response->getBody()->isWritable()) {
355
            if ($this->outputBuffering === 'prepend') {
356
                // prepend output buffer content
357
                $body = new Http\Body(fopen('php://temp', 'r+'));
358
                $body->write($output . $response->getBody());
359
                $response = $response->withBody($body);
360
            } elseif ($this->outputBuffering === 'append') {
361
                // append output buffer content
362
                $response->getBody()->write($output);
363
            }
364
        }
365
366
        return $response;
367
    }
368
}
369