filter.php 1.01 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 27 28 29 30 31 32 33 34 35
	{
		if (is_null(static::$filters))
		{
			static::$filters = require APP_PATH.'filters'.EXT;
		}

		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);

36 37 38
			// 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.
39
			if ( ! is_null($response) and $override)
40 41 42 43 44 45 46
			{
				return $response;
			}
		}
	}

}