name = $name;
$this->filters = Filter::parse($filters);
}
/**
* Determine if this collection's filters apply to a given method.
*
* Methods may be included / excluded using the "only" and "except" methods on the
* filter collection. Also, the "on" method may be used to set certain filters to
* only run when the request uses a given HTTP verb.
*
* @param string $method
* @return bool
*/
public function applies($method)
{
if (count($this->only) > 0 and ! in_array($method, $this->only))
{
return false;
}
if (count($this->except) > 0 and in_array($method, $this->except))
{
return false;
}
if (count($this->methods) > 0 and ! in_array(strtolower(Request::method()), $this->methods))
{
return false;
}
return true;
}
/**
* Set the excluded controller methods.
*
* When methods are excluded, the collection's filters will be run for each
* controller method except those explicitly specified via this method.
*
*
* // Specify a filter for all methods except "index"
* $this->filter('before', 'auth')->except('index');
*
* // Specify a filter for all methods except "index" and "home"
* $this->filter('before', 'auth')->except('index', 'home');
*
*
* @param array $methods
* @return Filter_Collection
*/
public function except($methods)
{
$this->except = (count(func_get_args()) > 1) ? func_get_args() : (array) $methods;
return $this;
}
/**
* Set the included controller methods.
*
* This method is the inverse of the "except" methods. The methods specified
* via this method are the only controller methods on which the collection's
* filters will be run.
*
*
* // Specify a filter for only the "index" method
* $this->filter('before', 'auth')->only('index');
*
* // Specify a filter for only the "index" and "home" methods
* $this->filter('before', 'auth')->only('index', 'home');
*
*
* @param array $methods
* @return Filter_Collection
*/
public function only($methods)
{
$this->only = (count(func_get_args()) > 1) ? func_get_args() : (array) $methods;
return $this;
}
/**
* Set the HTTP methods for which the filter applies.
*
* Since some filters, such as the CSRF filter, only make sense in a POST
* request context, this method allows you to limit which HTTP methods
* the filter will apply to.
*
*
* // Specify that a filter only applies on POST requests
* $this->filter('before', 'csrf')->on('post');
*
* // Specify that a filter applies for multiple HTTP request methods
* $this->filter('before', 'csrf')->on('post', 'put');
*
*
* @param array $methods
* @return Filter_Collection
*/
public function on($methods)
{
$methods = (count(func_get_args()) > 1) ? func_get_args() : (array) $methods;
foreach ($methods as $method)
{
$this->methods[] = strtolower($method);
}
return $this;
}
}