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\Routines; |
10
|
|
|
|
11
|
|
|
use ReflectionFunction; |
12
|
|
|
use ReflectionMethod; |
13
|
|
|
use ReflectionObject; |
14
|
|
|
use ReflectionClass; |
15
|
|
|
use Closure; |
16
|
|
|
use Respect\Rest\Request; |
17
|
|
|
|
18
|
|
|
/** Base class for routines that sync parameters */ |
19
|
|
|
abstract class AbstractSyncedRoutine extends AbstractRoutine implements ParamSynced |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Reflector |
23
|
|
|
*/ |
24
|
|
|
protected $reflection; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Return parameters that can be used with the routine. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
20 |
|
public function getParameters() |
32
|
|
|
{ |
33
|
20 |
|
$reflection = $this->getReflection(); |
34
|
20 |
|
if (!$reflection instanceof ReflectionObject && !$reflection instanceof ReflectionClass) { |
35
|
17 |
|
return $this->getReflection()->getParameters(); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
return array(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Executes the routine and return its result. |
43
|
|
|
* |
44
|
|
|
* @param Respect\Rest\Request $request |
45
|
|
|
* @param array $params |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
16 |
|
public function execute(Request $request, $params) |
|
|
|
|
49
|
|
|
{ |
50
|
16 |
|
$callback = $this->getCallback(); |
51
|
16 |
|
if (is_string($callback)) { |
52
|
1 |
|
$reflection = $this->getReflection(); |
53
|
1 |
|
$routineInstance = $reflection->newInstanceArgs($params); |
|
|
|
|
54
|
|
|
|
55
|
1 |
|
return $routineInstance(); |
56
|
|
|
} |
57
|
|
|
|
58
|
15 |
|
return call_user_func_array($callback, $params); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns a concrete ReflectionFunctionAbstract for this routine callback. |
63
|
|
|
* |
64
|
|
|
* @return Reflector |
65
|
|
|
*/ |
66
|
20 |
|
protected function getReflection() |
67
|
|
|
{ |
68
|
20 |
|
$callback = $this->getCallback(); |
69
|
20 |
|
if (is_array($callback)) { |
70
|
1 |
|
return new ReflectionMethod($callback[0], $callback[1]); |
71
|
19 |
|
} elseif ($callback instanceof Closure) { |
72
|
16 |
|
return new ReflectionFunction($callback); |
73
|
3 |
|
} elseif (is_string($callback)) { |
74
|
1 |
|
return new ReflectionClass($callback); |
75
|
|
|
} else { |
76
|
2 |
|
return new ReflectionObject($callback); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: