|
8 | 8 | use ReflectionMethod; |
9 | 9 | use Respect\Rest\Routable; |
10 | 10 |
|
| 11 | +/** A route that builds an instance of a class to run it */ |
11 | 12 | class ClassName extends AbstractRoute |
12 | 13 | { |
13 | 14 |
|
| 15 | + /** @var string The class name this route will instantiate */ |
14 | 16 | public $class = ''; |
| 17 | + |
| 18 | + /** @var array Constructor params for the built instance */ |
15 | 19 | public $constructorParams = array(); |
| 20 | + |
| 21 | + /** @var object The built class instance */ |
16 | 22 | protected $instance = null; |
17 | 23 |
|
18 | | - public function __construct($method, $pattern, $class, array $constructorParams=array()) |
19 | | - { |
| 24 | + /** |
| 25 | + * @param string $method The HTTP method (GET, POST, etc) |
| 26 | + * @param string $pattern The URI pattern for this route (like /users/*) |
| 27 | + * @param string $class The class name |
| 28 | + * @param array $params Constructor params for the class |
| 29 | + * |
| 30 | + * @see Respect\Rest\Routes\ClassName::$class |
| 31 | + * @see Respect\Rest\Routes\ClassName::$constructorParams |
| 32 | + */ |
| 33 | + public function __construct( |
| 34 | + $method, |
| 35 | + $pattern, |
| 36 | + $class, |
| 37 | + array $params=array() |
| 38 | + ) { |
20 | 39 | $this->class = $class; |
21 | | - $this->constructorParams = $constructorParams; |
| 40 | + $this->constructorParams = $params; |
22 | 41 | parent::__construct($method, $pattern); |
23 | 42 | } |
24 | 43 |
|
| 44 | + /** |
| 45 | + * Creates an instance of the class this route builds |
| 46 | + * |
| 47 | + * @return object The created instance |
| 48 | + */ |
25 | 49 | protected function createInstance() |
26 | 50 | { |
27 | 51 | $className = $this->class; |
28 | | - |
29 | 52 | $reflection = new ReflectionClass($className); |
30 | | - if (!$reflection->implementsInterface('Respect\\Rest\\Routable')) |
31 | | - throw new InvalidArgumentException('Routed classes must implement the Respect\\Rest\\Routable interface'); |
| 53 | + |
| 54 | + if (!$reflection->implementsInterface('Respect\\Rest\\Routable')) { |
| 55 | + throw new InvalidArgumentException( |
| 56 | + 'Routed classes must implement Respect\\Rest\\Routable' |
| 57 | + ); |
| 58 | + } |
32 | 59 |
|
33 | | - if (empty($this->constructorParams) || !method_exists($this->class, |
34 | | - '__construct')) |
| 60 | + if ( |
| 61 | + empty($this->constructorParams) |
| 62 | + || !method_exists($this->class, '__construct') |
| 63 | + ) { |
35 | 64 | return new $className; |
| 65 | + } |
36 | 66 |
|
37 | 67 | $reflection = new ReflectionClass($this->class); |
| 68 | + |
38 | 69 | return $reflection->newInstanceArgs($this->constructorParams); |
39 | 70 | } |
40 | 71 |
|
| 72 | + |
| 73 | + /** |
| 74 | + * Gets the reflection for a specific method. For this route, the reflection |
| 75 | + * is given for the class method having the same name as the HTTP method. |
| 76 | + * |
| 77 | + * @param string $method The HTTP method for this implementation |
| 78 | + * |
| 79 | + * @return ReflectionMethod The returned reflection object |
| 80 | + */ |
41 | 81 | public function getReflection($method) |
42 | 82 | { |
43 | 83 | $mirror = new ReflectionClass($this->class); |
44 | | - if ($mirror->hasMethod($method)) |
| 84 | + |
| 85 | + if ($mirror->hasMethod($method)) { |
45 | 86 | return new ReflectionMethod($this->class, $method); |
| 87 | + } |
46 | 88 | } |
47 | 89 |
|
| 90 | + /** |
| 91 | + * Runs the class method when this route is matched with params |
| 92 | + * |
| 93 | + * @param string $method The HTTP method for this implementation |
| 94 | + * @param array $params An array of params for this request |
| 95 | + * |
| 96 | + * @see Respect\Rest\Request::$params |
| 97 | + * |
| 98 | + * @return mixed Whatever the class method returns |
| 99 | + */ |
48 | 100 | public function runTarget($method, &$params) |
49 | 101 | { |
50 | | - if (is_null($this->instance)) |
| 102 | + if (is_null($this->instance)) { |
51 | 103 | $this->instance = $this->createInstance(); |
| 104 | + } |
52 | 105 |
|
53 | 106 | return call_user_func_array( |
54 | | - array($this->instance, $method), $params |
| 107 | + array($this->instance, $method), |
| 108 | + $params |
55 | 109 | ); |
56 | 110 | } |
57 | 111 |
|
|
0 commit comments