route.php 1.44 KB
Newer Older
1 2 3 4 5
<?php namespace System;

class Route {

	/**
6 7 8 9 10 11 12
	 * The route key, including request method and URI.
	 *
	 * @var string
	 */
	public $key;

	/**
13 14 15 16
	 * The route callback or array.
	 *
	 * @var mixed
	 */
17
	public $callback;
18 19 20 21 22 23 24 25 26 27 28

	/**
	 * The parameters that will passed to the route function.
	 *
	 * @var array
	 */
	public $parameters;

	/**
	 * Create a new Route instance.
	 *
29 30 31
	 * @param  string  $key
	 * @param  mixed   $callback
	 * @param  array   $parameters
32 33
	 * @return void
	 */
34
	public function __construct($key, $callback, $parameters = array())
35
	{
36 37
		$this->key = $key;
		$this->callback = $callback;
38 39 40 41 42 43 44 45
		$this->parameters = $parameters;
	}

	/**
	 * Execute the route function.
	 *
	 * @param  mixed     $route
	 * @param  array     $parameters
46
	 * @return Response
47 48 49 50 51
	 */
	public function call()
	{
		$response = null;

52
		if (is_callable($this->callback))
53
		{
54
			$response = call_user_func_array($this->callback, $this->parameters);
55
		}
56
		elseif (is_array($this->callback))
57
		{
58
			$response = isset($this->callback['before']) ? Route\Filter::call($this->callback['before'], array(), true) : null;
59

60
			if (is_null($response) and isset($this->callback['do']))
61
			{
62
				$response = call_user_func_array($this->callback['do'], $this->parameters);
63 64 65
			}
		}

66
		$response = Response::prepare($response);
67

68
		if (is_array($this->callback) and isset($this->callback['after']))
69
		{
70
			Route\Filter::call($this->callback['after'], array($response));
71 72 73 74 75 76
		}

		return $response;
	}

}