filter.php 7.41 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
	 * All of the registered filter aliases.
18
	 *
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
	 * @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>
	 *
	 * @param  string   $name
	 * @param  Closure  $callback
36 37
	 * @return void
	 */
38
	public static function register($name, Closure $callback)
39
	{
40 41 42
		if (isset(static::$aliases[$name])) $name = static::$aliases[$name];

		static::$filters[$name] = $callback;
43 44 45
	}

	/**
46
	 * Alias a filter so it can be used by another name.
47
	 *
48 49 50 51 52
	 * This is convenient for shortening filters that are registered by bundles.
	 *
	 * @param  string  $filter
	 * @param  string  $alias
	 * @return void
53
	 */
54
	public static function alias($filter, $alias)
55
	{
56
		static::$aliases[$alias] = $filter;
57 58
	}

59
	/**
60
	 * Parse a filter definition into an array of filters.
61 62 63 64 65 66 67 68 69
	 *
	 * @param  string|array  $filters
	 * @return array
	 */
	public static function parse($filters)
	{
		return (is_string($filters)) ? explode('|', $filters) : (array) $filters;
	}

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
	/**
	 * 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
				// bundle manually, and performance is improved since the bundle is only
				// started when needed.
				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;
				}				
			}
		}
	}

114 115 116 117 118
}

class Filter_Collection {

	/**
119 120 121 122 123 124 125 126
	 * The filters contained by the collection.
	 *
	 * @var string|array
	 */
	public $filters = array();

	/**
	 * The parameters specified for the filter.
127
	 *
128
	 * @var mixed
129
	 */
130
	public $parameters;
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

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

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

	/**
Taylor Otwell committed
147 148 149 150 151 152 153
	 * The HTTP methods for which the filter applies.
	 *
	 * @var array
	 */
	public $methods = array();

	/**
154 155 156
	 * Create a new filter collection instance.
	 *
	 * @param  string|array  $filters
157
	 * @param  mixed         $parameters
158
	 */
159
	public function __construct($filters, $parameters = null)
160
	{
161
		$this->parameters = $parameters;
162 163 164 165
		$this->filters = Filter::parse($filters);
	}

	/**
166
	 * Parse the filter string, returning the filter name and parameters.
167
	 *
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 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
	 * @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
223
	 *
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	 * @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;
		}

239 240 241
		$request = strtolower(Request::method());

		if (count($this->methods) > 0 and ! in_array($request, $this->methods))
Taylor Otwell committed
242 243 244 245
		{
			return false;
		}

246 247 248 249 250 251 252 253 254 255 256
		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"
257
	 *		$this->filter('before', 'auth')->except(array('index', 'home'));
258 259 260 261 262 263 264
	 * </code>
	 *
	 * @param  array              $methods
	 * @return Filter_Collection
	 */
	public function except($methods)
	{
265
		$this->except = (array) $methods;
266 267 268 269 270 271 272 273 274 275 276
		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
277
	 *		$this->filter('before', 'auth')->only(array('index', 'home'));
278 279 280 281 282 283 284
	 * </code>
	 *
	 * @param  array              $methods
	 * @return Filter_Collection
	 */
	public function only($methods)
	{
285
		$this->only = (array) $methods;
286 287 288
		return $this;
	}

Taylor Otwell committed
289 290 291 292 293 294 295 296
	/**
	 * 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
297
	 *		$this->filter('before', 'csrf')->on(array('post', 'put'));
Taylor Otwell committed
298 299 300 301 302 303 304
	 * </code>
	 *
	 * @param  array              $methods
	 * @return Filter_Collection
	 */
	public function on($methods)
	{
305
		$this->methods = array_map('strtolower', (array) $methods);
Taylor Otwell committed
306 307 308
		return $this;
	}

309
}