filter.php 4.08 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
<?php namespace Laravel\Routing;

class Filter {

	/**
	 * The route filters for the application.
	 *
	 * @var array
	 */
	protected static $filters = array();

	/**
	 * Register an array of route filters.
	 *
	 * @param  array  $filters
	 * @return void
	 */
	public static function register($filters)
	{
		static::$filters = array_merge(static::$filters, $filters);
	}

	/**
	 * Call a filter or set of filters.
	 *
	 * @param  array|string  $filters
	 * @param  array         $parameters
	 * @param  bool          $override
	 * @return mixed
	 */
	public static function run($filters, $parameters = array(), $override = false)
	{
33
		foreach (static::parse($filters) as $filter)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
		{
			// Parameters may be passed into routes by specifying the list of
			// parameters after a colon. If parameters are present, we will
			// merge them into the parameter array that was passed to the
			// method and slice the parameters off of the filter string.
			if (($colon = strpos($filter, ':')) !== false)
			{
				$parameters = array_merge($parameters, explode(',', substr($filter, $colon + 1)));

				$filter = substr($filter, 0, $colon);
			}

			if ( ! isset(static::$filters[$filter])) continue;

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

			// "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.
			if ( ! is_null($response) and $override) return $response;
		}
	}

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	/**
	 * Parse a string of filters into an array.
	 *
	 * @param  string|array  $filters
	 * @return array
	 */
	public static function parse($filters)
	{
		return (is_string($filters)) ? explode('|', $filters) : (array) $filters;
	}

}

class Filter_Collection {

	/**
	 * The event being filtered.
	 *
	 * @var string
	 */
	public $name;

	/**
	 * The included controller methods.
	 *
	 * @var array
	 */
	public $only = array();

	/**
	 * The excluded controller methods.
	 *
	 * @var array
	 */
	public $except = array();

	/**
	 * The filters contained by the collection.
	 *
	 * @var string|array
	 */
	public $filters = array();

	/**
	 * Create a new filter collection instance.
	 *
	 * @param  string        $name
	 * @param  string|array  $filters
	 */
	public function __construct($name, $filters)
	{
		$this->name = $name;
		$this->filters = Filter::parse($filters);
	}

	/**
	 * Determine if this collection's filters apply to a given method.
	 *
	 * @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;
		}

		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.
	 *
	 * <code>
	 *		// 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(array('index', 'home'));
	 * </code>
	 *
	 * @param  array              $methods
	 * @return Filter_Collection
	 */
	public function except($methods)
	{
		$this->except = (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.
	 *
	 * <code>
	 *		// 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(array('index', 'home'));
	 * </code>
	 *
	 * @param  array              $methods
	 * @return Filter_Collection
	 */
	public function only($methods)
	{
		$this->only = (array) $methods;
		return $this;
	}

181
}