route.php 2.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
<?php namespace System;

class Route {

	/**
	 * The route callback or array.
	 *
	 * @var mixed
	 */
	public $route;

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

	/**
	 * Create a new Route instance.
	 *
	 * @param  mixed  $route
	 * @param  array  $parameters
	 * @return void
	 */
	public function __construct($route, $parameters = array())
	{
		$this->route = $route;
		$this->parameters = $parameters;
	}

	/**
	 * Execute the route function.
	 *
	 * @param  mixed     $route
	 * @param  array     $parameters
	 * @return mixed
	 */
	public function call()
	{
		$response = null;

		// --------------------------------------------------------------
		// If the route just has a callback, call it.
		// --------------------------------------------------------------
		if (is_callable($this->route))
		{
			$response = call_user_func_array($this->route, $this->parameters);
		}
		// --------------------------------------------------------------
		// The route value is an array. We'll need to evaluate it.
		// --------------------------------------------------------------
		elseif (is_array($this->route))
		{
			// --------------------------------------------------------------
			// Call the "before" route filters.
			// --------------------------------------------------------------
			$response = isset($this->route['before']) ? Filter::call($this->route['before']) : null;

			// --------------------------------------------------------------
			// Call the route callback.
			// --------------------------------------------------------------
			if (is_null($response) and isset($this->route['do']))
			{
				$response = call_user_func_array($this->route['do'], $this->parameters);
			}
		}

		$response = ( ! $response instanceof Response) ? new Response($response) : $response;

		// --------------------------------------------------------------
		// Call the "after" route filters.
		// --------------------------------------------------------------
		if (is_array($this->route) and isset($this->route['after']))
		{
			Filter::call($this->route['after'], array($response));
		}

		return $response;
	}

}