|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Respect\Rest package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Respect\Rest\Routes; |
|
10
|
|
|
|
|
11
|
|
|
use ReflectionFunction; |
|
12
|
|
|
use ReflectionMethod; |
|
13
|
|
|
|
|
14
|
|
|
/** A callback-based route */ |
|
15
|
|
|
class Callback extends AbstractRoute |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var callable The actual callback this route holds */ |
|
18
|
|
|
protected $callback; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array String argument parameters from the Request */ |
|
21
|
|
|
public $arguments; |
|
22
|
|
|
|
|
23
|
|
|
/** @var ReflectionFunctionAbstract The reflection for the callback */ |
|
24
|
|
|
protected $reflection; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $method The HTTP method (GET, POST, etc) |
|
28
|
|
|
* @param string $pattern The URI pattern for this route |
|
29
|
|
|
* @param callable $callback The callback this route holds |
|
30
|
|
|
* @param array $arguments Additional arguments for this callback |
|
31
|
|
|
*/ |
|
32
|
31 |
|
public function __construct( |
|
33
|
|
|
$method, |
|
34
|
|
|
$pattern, |
|
35
|
|
|
$callback, |
|
36
|
|
|
array $arguments = array() |
|
37
|
|
|
) { |
|
38
|
31 |
|
$this->callback = $callback; |
|
39
|
31 |
|
$this->arguments = $arguments; |
|
40
|
31 |
|
parent::__construct($method, $pattern); |
|
41
|
31 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns an appropriate Reflection for any callable object |
|
45
|
|
|
* |
|
46
|
|
|
* @return ReflectionFunctionAbstract The returned reflection object |
|
47
|
|
|
*/ |
|
48
|
10 |
|
public function getCallbackReflection() |
|
49
|
|
|
{ |
|
50
|
10 |
|
if (is_array($this->callback)) { |
|
51
|
|
|
return new ReflectionMethod($this->callback[0], $this->callback[1]); |
|
52
|
|
|
} else { |
|
53
|
10 |
|
return new ReflectionFunction($this->callback); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Gets the reflection for a specific method. For callables, the reflection |
|
59
|
|
|
* is always the same. This follows the AbstractRoute implementation |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $method The irrelevant HTTP method for this implementation |
|
62
|
|
|
* |
|
63
|
|
|
* @return ReflectionFunctionAbstract The returned reflection object |
|
64
|
|
|
*/ |
|
65
|
10 |
|
public function getReflection($method) |
|
66
|
|
|
{ |
|
67
|
10 |
|
if (empty($this->reflection)) { |
|
68
|
10 |
|
$this->reflection = $this->getCallbackReflection(); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
10 |
|
return $this->reflection; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Runs the callback when this route is matched with params |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $method The irrelevant HTTP method for this implementation |
|
78
|
|
|
* @param array $params An array of params for this request |
|
79
|
|
|
* |
|
80
|
|
|
* @see Respect\Rest\Request::$params |
|
81
|
|
|
* |
|
82
|
|
|
* @return mixed Whatever the callback returns |
|
83
|
|
|
*/ |
|
84
|
17 |
|
public function runTarget($method, &$params) |
|
85
|
|
|
{ |
|
86
|
17 |
|
return call_user_func_array( |
|
87
|
17 |
|
$this->callback, |
|
88
|
17 |
|
array_merge($params, $this->arguments) |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
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..