Skip to content

Commit 6e671cf

Browse files
committed
Legibility improvements on the Callback route
1 parent 69d1806 commit 6e671cf

4 files changed

Lines changed: 68 additions & 9 deletions

File tree

library/Respect/Rest/Request.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ class Request
2020
/** @var string The HTTP method (commonly GET, POST, PUT, DELETE, HEAD) */
2121
public $method = '';
2222

23-
/** @var array A numeric array containing valid URL parameters */
23+
/**
24+
* @var array A numeric array containing valid URL parameters. For a route
25+
* path like /users/*, a Request for /users/alganet should have an array
26+
* equivalent to ['alganet']
27+
*/
2428
public $params = array();
2529

2630
/** @var AbstractRoute A route matched for this request */

library/Respect/Rest/Router.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public static function compareRoutePatterns($patternA, $patternB, $sub)
111111
*
112112
* @param array $params an array of params
113113
*
114+
* @see Respect\Rest\Request::$params
115+
*
114116
* @return array only the non-empty params
115117
*/
116118
protected static function cleanUpParams(array $params)
@@ -235,6 +237,8 @@ public function __toString()
235237
* @param array $etc This function accepts infinite params
236238
* that will be passed to the routine instance
237239
*
240+
* @see Respect\Rest\Request::$params
241+
*
238242
* @return Router the router itself.
239243
*/
240244
public function always($routineName, $param1=null, $param2=null, $etc=null)
@@ -589,6 +593,8 @@ protected function applyVirtualHost()
589593
* @param AbstractRoute $route Some route
590594
* @param array $param A list of URI params
591595
*
596+
* @see Respect\Rest\Request::$params
597+
*
592598
* @return Request a configured Request instance
593599
*/
594600
protected function configureRequest(
@@ -678,6 +684,8 @@ protected function matchesMethod(AbstractRoute $route, $methodName)
678684
* @param AbstractRoute $route Some route
679685
* @param array $params A list of URI params
680686
*
687+
* @see Respect\Rest\Request::$params
688+
*
681689
* @return bool true if the route matches the request with that params
682690
*/
683691
protected function matchRoute(

library/Respect/Rest/Routes/AbstractRoute.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,26 @@ abstract class AbstractRoute
5656

5757
/** @var string The HTTP method for this route (GET, POST, ANY, etc) */
5858
public $method = '';
59-
/** @var string The minimalistic pattern for route paths for this route */
59+
/** @var string The pattern for this route (like /users/*) */
6060
public $pattern = '';
61-
/** @var string The generated regex for the route pattern */
61+
/** @var string The generated regex to match the route pattern */
6262
public $regexForMatch = '';
63-
/** @var string The generated regex for creating URIs from parameters */
63+
/**
64+
* @var string The generated regex for creating URIs from parameters
65+
* @see Respect\Rest\AbstractRoute::createURI
66+
*/
6467
public $regexForReplace = '';
65-
/** @var array A list of routines appended to this route */
68+
/**
69+
* @var array A list of routines appended to this route
70+
* @see Respect\Rest\Routines\AbstractRoutine
71+
*/
6672
public $routines = array();
67-
/** @var array A list of side routes to be used */
73+
/**
74+
* @var array A list of side routes to be used
75+
* @see Respect\Rest\Routes\AbstractRoute
76+
*/
6877
public $sideRoutes = array();
78+
6979
/** @var array A virtualhost applied to this route (deprecated) */
7080
public $virtualHost = null;
7181

@@ -140,6 +150,8 @@ public function appendRoutine(Routinable $routine)
140150
* @param mixed $param1 Some parameter
141151
* @param mixed $etc This route accepts as many parameters you can pass
142152
*
153+
* @see Respect\Rest\Request::$params
154+
*
143155
* @return string the created URI
144156
*/
145157
public function createUri($param1=null, $etc=null)
@@ -158,6 +170,8 @@ public function createUri($param1=null, $etc=null)
158170
* @param array $params Parameters for the processed request
159171
* @see Respect\Rest\Routines\ProxyableWhen
160172
*
173+
* @see Respect\Rest\Request::$params
174+
*
161175
* @return bool always true \,,/
162176
*/
163177
public function matchRoutines(Request $request, $params=array())
@@ -184,6 +198,8 @@ public function matchRoutines(Request $request, $params=array())
184198
* @param Request $request The request you want to process
185199
* @param array $params Parameters for the processed request
186200
*
201+
* @see Respect\Rest\Request::$params
202+
*
187203
* @return bool as true as xkcd (always true)
188204
*/
189205
public function match(Request $request, &$params=array())

library/Respect/Rest/Routes/Callback.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,33 @@
99
class Callback extends AbstractRoute
1010
{
1111

12+
/** @var callable The actual callback this route holds */
1213
protected $callback;
14+
15+
/** @var array String argument parameters from the Request */
1316
public $arguments;
1417

15-
/** @var ReflectionFunctionAbstract */
18+
/** @var ReflectionFunctionAbstract The reflection for the callback */
1619
protected $reflection;
1720

21+
/**
22+
* @param string $method The HTTP method (GET, POST, etc)
23+
* @param string $pattern The URI pattern for this route
24+
* @param callable $callback The callback this route holds
25+
* @param array $arguments Additional arguments for this callback
26+
*/
1827
public function __construct($method, $pattern, $callback, array $arguments=array())
1928
{
2029
$this->callback = $callback;
2130
$this->arguments = $arguments;
2231
parent::__construct($method, $pattern);
2332
}
2433

25-
/** Returns an appropriate Reflection for any is_callable object */
34+
/**
35+
* Returns an appropriate Reflection for any callable object
36+
*
37+
* @return ReflectionFunctionAbstract The returned reflection object
38+
*/
2639
public function getCallbackReflection()
2740
{
2841
if (is_array($this->callback))
@@ -31,6 +44,14 @@ public function getCallbackReflection()
3144
return new ReflectionFunction($this->callback);
3245
}
3346

47+
/**
48+
* Gets the reflection for a specific method. For callables, the reflection
49+
* is always the same. This follows the AbstractRoute implementation
50+
*
51+
* @param string $method The irrelevant HTTP method for this implementation
52+
*
53+
* @return ReflectionFunctionAbstract The returned reflection object
54+
*/
3455
public function getReflection($method)
3556
{
3657
if (empty($this->reflection))
@@ -39,9 +60,19 @@ public function getReflection($method)
3960
return $this->reflection;
4061
}
4162

63+
/**
64+
* Runs the callback when this route is matched with params
65+
*
66+
* @param string $method The irrelevant HTTP method for this implementation
67+
* @param array $params An array of params for this request
68+
*
69+
* @see Respect\Rest\Request::$params
70+
*
71+
* @return mixed Whatever the callback returns
72+
*/
4273
public function runTarget($method, &$params)
4374
{
4475
return call_user_func_array($this->callback, array_merge($params, $this->arguments));
4576
}
4677

47-
}
78+
}

0 commit comments

Comments
 (0)