Files
spa-api/system/routing/filter.php

59 lines
1.2 KiB
PHP
Raw Normal View History

<?php namespace System\Routing;
2011-06-08 23:45:08 -05:00
class Filter {
2011-06-08 23:45:08 -05:00
/**
* The loaded route filters.
*
* @var array
*/
2011-07-31 21:51:55 -05:00
private static $filters = array();
/**
* Register a set of route filters.
*
* @param array $filters
* @return void
*/
public static function register($filters)
{
static::$filters = array_merge(static::$filters, $filters);
}
/**
* Clear all of the registered route filters.
*
* @return void
*/
public static function clear()
{
static::$filters = array();
}
2011-06-08 23:45:08 -05:00
/**
* Call a set of route filters.
*
* @param string $filter
* @param array $parameters
2011-06-14 17:27:11 -05:00
* @param bool $override
2011-06-08 23:45:08 -05:00
* @return mixed
*/
2011-06-14 17:27:11 -05:00
public static function call($filters, $parameters = array(), $override = false)
2011-06-08 23:45:08 -05:00
{
foreach (explode(', ', $filters) as $filter)
{
if ( ! isset(static::$filters[$filter])) continue;
2011-06-08 23:45:08 -05:00
$response = call_user_func_array(static::$filters[$filter], $parameters);
2011-07-31 21:51:55 -05:00
// "Before" filters may override the request cycle. For example, an authentication
// filter may redirect a user to a login view if they are not logged in. Because of
// this, we will return the first filter response if overriding is enabled.
2011-06-14 17:27:11 -05:00
if ( ! is_null($response) and $override)
2011-06-08 23:45:08 -05:00
{
return $response;
}
}
}
}