Files
spa-api/system/route.php

76 lines
1.4 KiB
PHP
Raw Normal View History

2011-06-08 23:45:08 -05:00
<?php namespace System;
class Route {
/**
* 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
*/
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.
*
* @param string $key
* @param mixed $callback
* @param array $parameters
2011-06-08 23:45:08 -05:00
* @return void
*/
public function __construct($key, $callback, $parameters = array())
2011-06-08 23:45:08 -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;
if (is_callable($this->callback))
2011-06-08 23:45:08 -05:00
{
$response = call_user_func_array($this->callback, $this->parameters);
2011-06-08 23:45:08 -05:00
}
elseif (is_array($this->callback))
2011-06-08 23:45:08 -05:00
{
2011-06-28 23:26:42 -05:00
$response = isset($this->callback['before']) ? Route\Filter::call($this->callback['before'], array(), true) : null;
2011-06-08 23:45:08 -05:00
if (is_null($response) and isset($this->callback['do']))
2011-06-08 23:45:08 -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
if (is_array($this->callback) and isset($this->callback['after']))
2011-06-08 23:45:08 -05:00
{
2011-06-28 23:26:42 -05:00
Route\Filter::call($this->callback['after'], array($response));
2011-06-08 23:45:08 -05:00
}
return $response;
}
}