filter.php 1.35 KB
Newer Older
1
<?php namespace System\Route;
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

class Filter {

	/**
	 * The loaded route filters.
	 *
	 * @var array
	 */
	public static $filters;

	/**
	 * Call a set of route filters.
	 *
	 * @param  string  $filter
	 * @param  array   $parameters
17
	 * @param  bool    $override
18 19
	 * @return mixed
	 */
20
	public static function call($filters, $parameters = array(), $override = false)
21 22 23 24 25 26
	{
		if (is_null(static::$filters))
		{
			static::$filters = require APP_PATH.'filters'.EXT;
		}

27 28 29
		// --------------------------------------------------------------
		// Filters can be comma-delimited, so spin through each one.
		// --------------------------------------------------------------
30 31 32 33 34 35 36 37 38 39
		foreach (explode(', ', $filters) as $filter)
		{
			if ( ! isset(static::$filters[$filter]))
			{
				throw new \Exception("Route filter [$filter] is not defined.");						
			}

			$response = call_user_func_array(static::$filters[$filter], $parameters);

			// --------------------------------------------------------------
40 41 42 43 44
			// If overriding is set to true and the filter returned a
			// response, return that response. 
			//
			// Overriding allows for convenient halting of the request
			// flow for things like authentication, CSRF protection, etc.
45
			// --------------------------------------------------------------
46
			if ( ! is_null($response) and $override)
47 48 49 50 51 52 53
			{
				return $response;
			}
		}
	}

}