|
6 | 6 | use ReflectionFunction; |
7 | 7 | use ReflectionMethod; |
8 | 8 | use ReflectionObject; |
| 9 | +use ReflectionClass; |
9 | 10 | use Closure; |
10 | | -use Respect\Rest\Routes\AbstractRoute; |
| 11 | +use Respect\Rest\Routes\AbstractRoute, |
| 12 | + Respect\Rest\Request; |
11 | 13 |
|
12 | 14 | /** Base class for routines that sync parameters */ |
13 | 15 | abstract class AbstractSyncedRoutine extends AbstractRoutine implements ParamSynced |
14 | 16 | { |
15 | | - |
| 17 | + /** |
| 18 | + * @var Reflector |
| 19 | + */ |
16 | 20 | protected $reflection; |
17 | 21 |
|
| 22 | + /** |
| 23 | + * Return parameters that can be used with the routine. |
| 24 | + * |
| 25 | + * @return array |
| 26 | + */ |
18 | 27 | public function getParameters() |
19 | 28 | { |
20 | 29 | $reflection = $this->getReflection(); |
21 | | - if (!$reflection instanceof ReflectionObject) |
| 30 | + if (!$reflection instanceof ReflectionObject && !$reflection instanceof ReflectionClass) |
22 | 31 | return $this->getReflection()->getParameters(); |
23 | 32 |
|
24 | | - $constructorReflection = $reflection->getConstructor(); |
25 | | - if (is_null($constructorReflection)) |
26 | | - return array(); |
27 | | - else |
28 | | - return $constructorReflection->getParameters(); |
| 33 | + return array(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Executes the routine and return its result. |
| 38 | + * |
| 39 | + * @param Respect\Rest\Request $request |
| 40 | + * @param array $params |
| 41 | + * @return mixed |
| 42 | + */ |
| 43 | + public function execute(Request $request, $params) |
| 44 | + { |
| 45 | + $callback = $this->getCallback(); |
| 46 | + if (is_string($callback)) { |
| 47 | + $reflection = $this->getReflection(); |
| 48 | + $routineInstance = $reflection->newInstanceArgs($params); |
| 49 | + return $routineInstance(); |
| 50 | + } |
| 51 | + return call_user_func_array($callback, $params); |
29 | 52 | } |
30 | 53 |
|
31 | | - /** Returns a concrete ReflectionFunctionAbstract for this routine callback */ |
| 54 | + /** |
| 55 | + * Returns a concrete ReflectionFunctionAbstract for this routine callback. |
| 56 | + * |
| 57 | + * @return Reflector |
| 58 | + */ |
32 | 59 | protected function getReflection() |
33 | 60 | { |
34 | 61 | $callback = $this->getCallback(); |
35 | 62 | if (is_array($callback)) |
36 | 63 | return new ReflectionMethod($callback[0], $callback[1]); |
37 | 64 | else if ($callback instanceof Closure) |
38 | 65 | return new ReflectionFunction($callback); |
| 66 | + else if (is_string($callback)) |
| 67 | + return new ReflectionClass($callback); |
39 | 68 | else |
40 | 69 | return new ReflectionObject($callback); |
41 | 70 | } |
|
0 commit comments