Skip to content

Commit ef55701

Browse files
committed
Legibility improvements on Callback, ClassName, Error and Exception routes
1 parent 6e671cf commit ef55701

4 files changed

Lines changed: 86 additions & 41 deletions

File tree

library/Respect/Rest/Routes/Callback.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ class Callback extends AbstractRoute
2424
* @param callable $callback The callback this route holds
2525
* @param array $arguments Additional arguments for this callback
2626
*/
27-
public function __construct($method, $pattern, $callback, array $arguments=array())
28-
{
27+
public function __construct(
28+
$method,
29+
$pattern,
30+
$callback,
31+
array $arguments=array()
32+
) {
2933
$this->callback = $callback;
3034
$this->arguments = $arguments;
3135
parent::__construct($method, $pattern);
@@ -38,10 +42,11 @@ public function __construct($method, $pattern, $callback, array $arguments=array
3842
*/
3943
public function getCallbackReflection()
4044
{
41-
if (is_array($this->callback))
45+
if (is_array($this->callback)) {
4246
return new ReflectionMethod($this->callback[0], $this->callback[1]);
43-
else
47+
} else {
4448
return new ReflectionFunction($this->callback);
49+
}
4550
}
4651

4752
/**
@@ -54,8 +59,9 @@ public function getCallbackReflection()
5459
*/
5560
public function getReflection($method)
5661
{
57-
if (empty($this->reflection))
62+
if (empty($this->reflection)) {
5863
$this->reflection = $this->getCallbackReflection();
64+
}
5965

6066
return $this->reflection;
6167
}
@@ -72,7 +78,10 @@ public function getReflection($method)
7278
*/
7379
public function runTarget($method, &$params)
7480
{
75-
return call_user_func_array($this->callback, array_merge($params, $this->arguments));
81+
return call_user_func_array(
82+
$this->callback,
83+
array_merge($params, $this->arguments)
84+
);
7685
}
7786

7887
}

library/Respect/Rest/Routes/ClassName.php

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,104 @@
88
use ReflectionMethod;
99
use Respect\Rest\Routable;
1010

11+
/** A route that builds an instance of a class to run it */
1112
class ClassName extends AbstractRoute
1213
{
1314

15+
/** @var string The class name this route will instantiate */
1416
public $class = '';
17+
18+
/** @var array Constructor params for the built instance */
1519
public $constructorParams = array();
20+
21+
/** @var object The built class instance */
1622
protected $instance = null;
1723

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+
) {
2039
$this->class = $class;
21-
$this->constructorParams = $constructorParams;
40+
$this->constructorParams = $params;
2241
parent::__construct($method, $pattern);
2342
}
2443

44+
/**
45+
* Creates an instance of the class this route builds
46+
*
47+
* @return object The created instance
48+
*/
2549
protected function createInstance()
2650
{
2751
$className = $this->class;
28-
2952
$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+
}
3259

33-
if (empty($this->constructorParams) || !method_exists($this->class,
34-
'__construct'))
60+
if (
61+
empty($this->constructorParams)
62+
|| !method_exists($this->class, '__construct')
63+
) {
3564
return new $className;
65+
}
3666

3767
$reflection = new ReflectionClass($this->class);
68+
3869
return $reflection->newInstanceArgs($this->constructorParams);
3970
}
4071

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+
*/
4181
public function getReflection($method)
4282
{
4383
$mirror = new ReflectionClass($this->class);
44-
if ($mirror->hasMethod($method))
84+
85+
if ($mirror->hasMethod($method)) {
4586
return new ReflectionMethod($this->class, $method);
87+
}
4688
}
4789

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+
*/
48100
public function runTarget($method, &$params)
49101
{
50-
if (is_null($this->instance))
102+
if (is_null($this->instance)) {
51103
$this->instance = $this->createInstance();
104+
}
52105

53106
return call_user_func_array(
54-
array($this->instance, $method), $params
107+
array($this->instance, $method),
108+
$params
55109
);
56110
}
57111

library/Respect/Rest/Routes/Error.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,18 @@
33

44
use ReflectionClass;
55

6-
class Error extends AbstractRoute
6+
class Error extends Callback
77
{
88
public $callback;
99
public $errors = array();
1010

1111
public function __construct($callback)
1212
{
13-
$this->callback = $callback;
14-
parent::__construct('ANY', '^$');
15-
}
16-
17-
public function getReflection($method)
18-
{
19-
if (empty($this->reflection))
20-
$this->reflection = new ReflectionClass('stdClass');
21-
22-
return $this->reflection;
13+
parent::__construct('ANY', '^$', $callback);
2314
}
2415

2516
public function runTarget($method, &$params)
2617
{
2718
return call_user_func($this->callback, $this->errors);
2819
}
29-
}
20+
}

library/Respect/Rest/Routes/Exception.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use ReflectionClass;
55

6-
class Exception extends AbstractRoute
6+
class Exception extends Callback
77
{
88
public $class;
99
public $callback;
@@ -12,20 +12,11 @@ class Exception extends AbstractRoute
1212
public function __construct($class, $callback)
1313
{
1414
$this->class = $class;
15-
$this->callback = $callback;
16-
parent::__construct('ANY', '^$');
17-
}
18-
19-
public function getReflection($method)
20-
{
21-
if (empty($this->reflection))
22-
$this->reflection = new ReflectionClass('stdClass');
23-
24-
return $this->reflection;
15+
parent::__construct('ANY', '^$', $callback);
2516
}
2617

2718
public function runTarget($method, &$params)
2819
{
2920
return call_user_func($this->callback, $this->exception);
3021
}
31-
}
22+
}

0 commit comments

Comments
 (0)