filter.php 7.4 KB

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