filter.php 7.89 KB
Newer Older
1 2
<?php namespace Laravel\Routing;

3 4
use Closure;
use Laravel\Bundle;
Taylor Otwell committed
5 6
use Laravel\Request;

7 8 9 10 11 12 13
class Filter {

	/**
	 * The route filters for the application.
	 *
	 * @var array
	 */
14
	public static $filters = array();
15 16

	/**
17 18 19 20 21 22 23
	 * The route filters that are based on pattern.
	 *
	 * @var array
	 */
	public static $patterns = array();

	/**
24
	 * All of the registered filter aliases.
25
	 *
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	 * @var array
	 */
	public static $aliases = array();

	/**
	 * Register a filter for the application.
	 *
	 * <code>
	 *		// Register a closure as a filter
	 *		Filter::register('before', function() {});
	 *
	 *		// Register a class callback as a filter
	 *		Filter::register('before', array('Class', 'method'));
	 * </code>
	 *
41 42
	 * @param  string  $name
	 * @param  mixed   $callback
43 44
	 * @return void
	 */
45
	public static function register($name, $callback)
46
	{
47 48
		if (isset(static::$aliases[$name])) $name = static::$aliases[$name];

49 50 51
		// If the filter starts with "pattern: ", the filter is being setup to match on
		// all requests that match a given pattern. This is nice for defining filters
		// that handle all URIs beginning with "admin" for example.
52 53 54 55 56 57 58 59 60 61 62
		if (starts_with($name, 'pattern: '))
		{
			foreach (explode(', ', substr($name, 9)) as $pattern)
			{
				static::$patterns[$pattern] = $callback;
			}
		}
		else
		{
			static::$filters[$name] = $callback;
		}
63 64 65
	}

	/**
66
	 * Alias a filter so it can be used by another name.
67
	 *
68 69 70 71 72
	 * This is convenient for shortening filters that are registered by bundles.
	 *
	 * @param  string  $filter
	 * @param  string  $alias
	 * @return void
73
	 */
74
	public static function alias($filter, $alias)
75
	{
76
		static::$aliases[$alias] = $filter;
77 78
	}

79
	/**
80
	 * Parse a filter definition into an array of filters.
81 82 83 84 85 86 87 88 89
	 *
	 * @param  string|array  $filters
	 * @return array
	 */
	public static function parse($filters)
	{
		return (is_string($filters)) ? explode('|', $filters) : (array) $filters;
	}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	/**
	 * Call a filter or set of filters.
	 *
	 * @param  array   $collections
	 * @param  array   $pass
	 * @param  bool    $override
	 * @return mixed
	 */
	public static function run($collections, $pass = array(), $override = false)
	{
		foreach ($collections as $collection)
		{
			foreach ($collection->filters as $filter)
			{
				list($filter, $parameters) = $collection->get($filter);

				// We will also go ahead and start the bundle for the developer. This allows
				// the developer to specify bundle filters on routes without starting the
108
				// bundle manually, and performance is improved by lazy-loading.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
				Bundle::start(Bundle::name($filter));

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

				$callback = static::$filters[$filter];

				// Parameters may be passed into filters by specifying the list of parameters
				// as an array, or by registering a Closure which will return the array of
				// parameters. If parameters are present, we will merge them with the
				// parameters that were given to the method.
				$response = call_user_func_array($callback, array_merge($pass, $parameters));

				// "Before" filters may override the request cycle. For example, an auth
				// 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 for the filter collections
				if ( ! is_null($response) and $override)
				{
					return $response;
				}				
			}
		}
	}

133 134 135 136 137
}

class Filter_Collection {

	/**
138 139 140 141 142 143 144 145
	 * The filters contained by the collection.
	 *
	 * @var string|array
	 */
	public $filters = array();

	/**
	 * The parameters specified for the filter.
146
	 *
147
	 * @var mixed
148
	 */
149
	public $parameters;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

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

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

	/**
Taylor Otwell committed
166 167 168 169 170 171 172
	 * The HTTP methods for which the filter applies.
	 *
	 * @var array
	 */
	public $methods = array();

	/**
173 174 175
	 * Create a new filter collection instance.
	 *
	 * @param  string|array  $filters
176
	 * @param  mixed         $parameters
Phill Sparks committed
177
	 * @return void
178
	 */
179
	public function __construct($filters, $parameters = null)
180
	{
181
		$this->parameters = $parameters;
182 183 184 185
		$this->filters = Filter::parse($filters);
	}

	/**
186
	 * Parse the filter string, returning the filter name and parameters.
187
	 *
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	 * @param  string  $filter
	 * @return array
	 */
	public function get($filter)
	{
		// If the parameters were specified by passing an array into the collection,
		// then we will simply return those parameters. Combining passed parameters
		// with parameters specified directly in the filter attachment is not
		// currently supported by the framework.
		if ( ! is_null($this->parameters))
		{
			return array($filter, $this->parameters());
		}

		// If no parameters were specified when the collection was created, we will
		// check the filter string itself to see if the parameters were injected
		// into the string as raw values, such as "role:admin".
		if (($colon = strpos(Bundle::element($filter), ':')) !== false)
		{
			$parameters = explode(',', substr(Bundle::element($filter), $colon + 1));

			// If the filter belongs to a bundle, we need to re-calculate the position
			// of the parameter colon, since we originally calculated it without the
			// bundle identifier because the identifier uses colons as well.
			if (($bundle = Bundle::name($filter)) !== DEFAULT_BUNDLE)
			{
				$colon = strlen($bundle.'::') + $colon;
			}

			return array(substr($filter, 0, $colon), $parameters);
		}

		// If no parameters were specified when the collection was created or
		// in the filter string, we will just return the filter name as is
		// and give back an empty array of parameters.
		return array($filter, array());
	}

	/**
	 * Evaluate the collection's parameters and return a parameters array.
	 *
	 * @return array
	 */
	protected function parameters()
	{
		if ($this->parameters instanceof Closure)
		{
			$this->parameters = call_user_func($this->parameters);
		}

		return $this->parameters;
	}

	/**
	 * Determine if this collection's filters apply to a given method.
Taylor Otwell committed
243
	 *
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	 * @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;
		}

259 260 261
		$request = strtolower(Request::method());

		if (count($this->methods) > 0 and ! in_array($request, $this->methods))
Taylor Otwell committed
262 263 264 265
		{
			return false;
		}

266 267 268 269 270 271 272 273 274 275 276
		return true;
	}

	/**
	 * Set the excluded controller methods.
	 *
	 * <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"
277
	 *		$this->filter('before', 'auth')->except(array('index', 'home'));
278 279 280 281 282 283 284
	 * </code>
	 *
	 * @param  array              $methods
	 * @return Filter_Collection
	 */
	public function except($methods)
	{
285
		$this->except = (array) $methods;
286 287 288 289 290 291 292 293 294 295 296
		return $this;
	}

	/**
	 * Set the included controller methods.
	 *
	 * <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
297
	 *		$this->filter('before', 'auth')->only(array('index', 'home'));
298 299 300 301 302 303 304
	 * </code>
	 *
	 * @param  array              $methods
	 * @return Filter_Collection
	 */
	public function only($methods)
	{
305
		$this->only = (array) $methods;
306 307 308
		return $this;
	}

Taylor Otwell committed
309 310 311 312 313 314 315 316
	/**
	 * Set the HTTP methods for which the filter applies.
	 *
	 * <code>
	 *		// 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
317
	 *		$this->filter('before', 'csrf')->on(array('post', 'put'));
Taylor Otwell committed
318 319 320 321 322 323 324
	 * </code>
	 *
	 * @param  array              $methods
	 * @return Filter_Collection
	 */
	public function on($methods)
	{
325
		$this->methods = array_map('strtolower', (array) $methods);
Taylor Otwell committed
326 327 328
		return $this;
	}

329
}