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
02:33
created

Route::setCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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