filter.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php namespace Laravel\Routing;
  2. use Closure;
  3. use Laravel\Bundle;
  4. use Laravel\Request;
  5. class Filter {
  6. /**
  7. * The route filters for the application.
  8. *
  9. * @var array
  10. */
  11. public static $filters = array();
  12. /**
  13. * The route filters that are based on pattern.
  14. *
  15. * @var array
  16. */
  17. public static $patterns = array();
  18. /**
  19. * All of the registered filter aliases.
  20. *
  21. * @var array
  22. */
  23. public static $aliases = array();
  24. /**
  25. * Register a filter for the application.
  26. *
  27. * <code>
  28. * // Register a closure as a filter
  29. * Filter::register('before', function() {});
  30. *
  31. * // Register a class callback as a filter
  32. * Filter::register('before', array('Class', 'method'));
  33. * </code>
  34. *
  35. * @param string $name
  36. * @param mixed $callback
  37. * @return void
  38. */
  39. public static function register($name, $callback)
  40. {
  41. if (isset(static::$aliases[$name])) $name = static::$aliases[$name];
  42. // If the filter starts with "pattern: ", the filter is being setup to match on
  43. // all requests that match a given pattern. This is nice for defining filters
  44. // that handle all URIs beginning with "admin" for example.
  45. if (starts_with($name, 'pattern: '))
  46. {
  47. foreach (explode(', ', substr($name, 9)) as $pattern)
  48. {
  49. static::$patterns[$pattern] = $callback;
  50. }
  51. }
  52. else
  53. {
  54. static::$filters[$name] = $callback;
  55. }
  56. }
  57. /**
  58. * Alias a filter so it can be used by another name.
  59. *
  60. * This is convenient for shortening filters that are registered by bundles.
  61. *
  62. * @param string $filter
  63. * @param string $alias
  64. * @return void
  65. */
  66. public static function alias($filter, $alias)
  67. {
  68. static::$aliases[$alias] = $filter;
  69. }
  70. /**
  71. * Parse a filter definition into an array of filters.
  72. *
  73. * @param string|array $filters
  74. * @return array
  75. */
  76. public static function parse($filters)
  77. {
  78. return (is_string($filters)) ? explode('|', $filters) : (array) $filters;
  79. }
  80. /**
  81. * Call a filter or set of filters.
  82. *
  83. * @param array $collections
  84. * @param array $pass
  85. * @param bool $override
  86. * @return mixed
  87. */
  88. public static function run($collections, $pass = array(), $override = false)
  89. {
  90. foreach ($collections as $collection)
  91. {
  92. foreach ($collection->filters as $filter)
  93. {
  94. list($filter, $parameters) = $collection->get($filter);
  95. // We will also go ahead and start the bundle for the developer. This allows
  96. // the developer to specify bundle filters on routes without starting the
  97. // bundle manually, and performance is improved by lazy-loading.
  98. Bundle::start(Bundle::name($filter));
  99. if ( ! isset(static::$filters[$filter])) continue;
  100. $callback = static::$filters[$filter];
  101. // Parameters may be passed into filters by specifying the list of parameters
  102. // as an array, or by registering a Closure which will return the array of
  103. // parameters. If parameters are present, we will merge them with the
  104. // parameters that were given to the method.
  105. $response = call_user_func_array($callback, array_merge($pass, $parameters));
  106. // "Before" filters may override the request cycle. For example, an auth
  107. // filter may redirect a user to a login view if they are not logged in.
  108. // Because of this, we will return the first filter response if
  109. // overriding is enabled for the filter collections
  110. if ( ! is_null($response) and $override)
  111. {
  112. return $response;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. class Filter_Collection {
  119. /**
  120. * The filters contained by the collection.
  121. *
  122. * @var string|array
  123. */
  124. public $filters = array();
  125. /**
  126. * The parameters specified for the filter.
  127. *
  128. * @var mixed
  129. */
  130. public $parameters;
  131. /**
  132. * The included controller methods.
  133. *
  134. * @var array
  135. */
  136. public $only = array();
  137. /**
  138. * The excluded controller methods.
  139. *
  140. * @var array
  141. */
  142. public $except = array();
  143. /**
  144. * The HTTP methods for which the filter applies.
  145. *
  146. * @var array
  147. */
  148. public $methods = array();
  149. /**
  150. * Create a new filter collection instance.
  151. *
  152. * @param string|array $filters
  153. * @param mixed $parameters
  154. * @return void
  155. */
  156. public function __construct($filters, $parameters = null)
  157. {
  158. $this->parameters = $parameters;
  159. $this->filters = Filter::parse($filters);
  160. }
  161. /**
  162. * Parse the filter string, returning the filter name and parameters.
  163. *
  164. * @param string $filter
  165. * @return array
  166. */
  167. public function get($filter)
  168. {
  169. // If the parameters were specified by passing an array into the collection,
  170. // then we will simply return those parameters. Combining passed parameters
  171. // with parameters specified directly in the filter attachment is not
  172. // currently supported by the framework.
  173. if ( ! is_null($this->parameters))
  174. {
  175. return array($filter, $this->parameters());
  176. }
  177. // If no parameters were specified when the collection was created, we will
  178. // check the filter string itself to see if the parameters were injected
  179. // into the string as raw values, such as "role:admin".
  180. if (($colon = strpos(Bundle::element($filter), ':')) !== false)
  181. {
  182. $parameters = explode(',', substr(Bundle::element($filter), $colon + 1));
  183. // If the filter belongs to a bundle, we need to re-calculate the position
  184. // of the parameter colon, since we originally calculated it without the
  185. // bundle identifier because the identifier uses colons as well.
  186. if (($bundle = Bundle::name($filter)) !== DEFAULT_BUNDLE)
  187. {
  188. $colon = strlen($bundle.'::') + $colon;
  189. }
  190. return array(substr($filter, 0, $colon), $parameters);
  191. }
  192. // If no parameters were specified when the collection was created or
  193. // in the filter string, we will just return the filter name as is
  194. // and give back an empty array of parameters.
  195. return array($filter, array());
  196. }
  197. /**
  198. * Evaluate the collection's parameters and return a parameters array.
  199. *
  200. * @return array
  201. */
  202. protected function parameters()
  203. {
  204. if ($this->parameters instanceof Closure)
  205. {
  206. $this->parameters = call_user_func($this->parameters);
  207. }
  208. return $this->parameters;
  209. }
  210. /**
  211. * Determine if this collection's filters apply to a given method.
  212. *
  213. * @param string $method
  214. * @return bool
  215. */
  216. public function applies($method)
  217. {
  218. if (count($this->only) > 0 and ! in_array($method, $this->only))
  219. {
  220. return false;
  221. }
  222. if (count($this->except) > 0 and in_array($method, $this->except))
  223. {
  224. return false;
  225. }
  226. $request = strtolower(Request::method());
  227. if (count($this->methods) > 0 and ! in_array($request, $this->methods))
  228. {
  229. return false;
  230. }
  231. return true;
  232. }
  233. /**
  234. * Set the excluded controller methods.
  235. *
  236. * <code>
  237. * // Specify a filter for all methods except "index"
  238. * $this->filter('before', 'auth')->except('index');
  239. *
  240. * // Specify a filter for all methods except "index" and "home"
  241. * $this->filter('before', 'auth')->except(array('index', 'home'));
  242. * </code>
  243. *
  244. * @param array $methods
  245. * @return Filter_Collection
  246. */
  247. public function except($methods)
  248. {
  249. $this->except = (array) $methods;
  250. return $this;
  251. }
  252. /**
  253. * Set the included controller methods.
  254. *
  255. * <code>
  256. * // Specify a filter for only the "index" method
  257. * $this->filter('before', 'auth')->only('index');
  258. *
  259. * // Specify a filter for only the "index" and "home" methods
  260. * $this->filter('before', 'auth')->only(array('index', 'home'));
  261. * </code>
  262. *
  263. * @param array $methods
  264. * @return Filter_Collection
  265. */
  266. public function only($methods)
  267. {
  268. $this->only = (array) $methods;
  269. return $this;
  270. }
  271. /**
  272. * Set the HTTP methods for which the filter applies.
  273. *
  274. * <code>
  275. * // Specify that a filter only applies on POST requests
  276. * $this->filter('before', 'csrf')->on('post');
  277. *
  278. * // Specify that a filter applies for multiple HTTP request methods
  279. * $this->filter('before', 'csrf')->on(array('post', 'put'));
  280. * </code>
  281. *
  282. * @param array $methods
  283. * @return Filter_Collection
  284. */
  285. public function on($methods)
  286. {
  287. $this->methods = array_map('strtolower', (array) $methods);
  288. return $this;
  289. }
  290. }