2011-07-31 14:48:17 -05:00
|
|
|
<?php namespace System\Routing;
|
|
|
|
|
|
|
|
|
|
use System\Response;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
|
|
|
|
class Route {
|
|
|
|
|
|
2011-06-18 12:00:52 -05:00
|
|
|
/**
|
|
|
|
|
* The route key, including request method and URI.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public $key;
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* The route callback or array.
|
|
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
2011-06-18 12:00:52 -05:00
|
|
|
public $callback;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The parameters that will passed to the route function.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public $parameters;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new Route instance.
|
|
|
|
|
*
|
2011-06-18 12:00:52 -05:00
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $callback
|
|
|
|
|
* @param array $parameters
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-06-18 12:00:52 -05:00
|
|
|
public function __construct($key, $callback, $parameters = array())
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-06-18 12:00:52 -05:00
|
|
|
$this->key = $key;
|
|
|
|
|
$this->callback = $callback;
|
2011-06-08 23:45:08 -05:00
|
|
|
$this->parameters = $parameters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the route function.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $route
|
|
|
|
|
* @param array $parameters
|
2011-07-08 12:46:21 -07:00
|
|
|
* @return Response
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
|
|
|
|
public function call()
|
|
|
|
|
{
|
|
|
|
|
$response = null;
|
|
|
|
|
|
2011-07-31 21:51:55 -05:00
|
|
|
// The callback may be in array form, meaning it has attached filters or is named.
|
|
|
|
|
// However, the callback may also simply be a closure. If it is just a closure,
|
|
|
|
|
// we can execute it here. Otherwise, we will need to evaluate the route for any
|
|
|
|
|
// filters that need to be called.
|
2011-06-18 12:00:52 -05:00
|
|
|
if (is_callable($this->callback))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-06-18 12:00:52 -05:00
|
|
|
$response = call_user_func_array($this->callback, $this->parameters);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-06-18 12:00:52 -05:00
|
|
|
elseif (is_array($this->callback))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-07-31 14:48:17 -05:00
|
|
|
$response = isset($this->callback['before']) ? Filter::call($this->callback['before'], array(), true) : null;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-06-18 12:00:52 -05:00
|
|
|
if (is_null($response) and isset($this->callback['do']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-06-18 12:00:52 -05:00
|
|
|
$response = call_user_func_array($this->callback['do'], $this->parameters);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-14 17:27:11 -05:00
|
|
|
$response = Response::prepare($response);
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-06-18 12:00:52 -05:00
|
|
|
if (is_array($this->callback) and isset($this->callback['after']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-07-31 14:48:17 -05:00
|
|
|
Filter::call($this->callback['after'], array($response));
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|