PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/aurora/routing/filter.php

https://gitlab.com/Bartwillemsen/aurora-framework
PHP | 225 lines | 73 code | 26 blank | 126 comment | 6 complexity | 57677dc0a42da8858168a70d9b585593 MD5 | raw file
  1. <?php
  2. namespace Aurora\Routing;
  3. use Aurora\Request;
  4. class Filter
  5. {
  6. /**
  7. * The route filters for the application.
  8. *
  9. * @var array
  10. */
  11. protected static $filters = array();
  12. /**
  13. * Register an array of route filters.
  14. *
  15. * @param array $filters
  16. */
  17. public static function register($filters)
  18. {
  19. static::$filters = array_merge(static::$filters, $filters);
  20. }
  21. /**
  22. * Call a filter or set of filters.
  23. *
  24. * @param array|string $filters
  25. * @param array $pass
  26. * @param bool $override
  27. * @return mixed
  28. */
  29. public static function run($filters, $pass = array(), $override = false)
  30. {
  31. foreach (static::parse($filters) as $filter) {
  32. $parameters = array();
  33. // Parameters may be passed into routes by specifying the list of
  34. // parameters after a colon. If parameters are present, we will
  35. // merge them into the parameter array that was passed to the
  36. // method and slice the parameters off of the filter string.
  37. if (($colon = strpos($filter, ':')) !== false) {
  38. $parameters = explode(',', substr($filter, $colon + 1));
  39. $filter = substr($filter, 0 , $colon);
  40. }
  41. if (! isset(static::$filters[$filter])) continue;
  42. $parameters = array_merge($pass, $parameters);
  43. $response = call_user_func_array(static::$filters[$filter], $parameters);
  44. // "Before" filters may override the request cycle. For example,
  45. // an authentication filter may redirect a user to a login view
  46. // if they are not logged in. Because of this, we will return
  47. // the first filter response if overriding is enabled.
  48. if (! is_null($response) and $override) return $response;
  49. }
  50. }
  51. /**
  52. * Parse a string of filters into an array.
  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. class Filter_Collection
  63. {
  64. /**
  65. * The event being filtered.
  66. *
  67. * @var string
  68. */
  69. public $name;
  70. /**
  71. * The included controller methods.
  72. *
  73. * @var array
  74. */
  75. public $only = array();
  76. /**
  77. * The excluded controller methods.
  78. *
  79. * @var array
  80. */
  81. public $except = array();
  82. /**
  83. * The filters contained by the collection.
  84. *
  85. * @var string|array
  86. */
  87. public $filters = array();
  88. /**
  89. * The HTTP methods for which the filter applies.
  90. *
  91. * @var array
  92. */
  93. public $methods = array();
  94. /**
  95. * Create a new filter collection instance.
  96. *
  97. * @param string $name
  98. * @param string|array $filters
  99. */
  100. public function __construct($name, $filters)
  101. {
  102. $this->name = $name;
  103. $this->filters = Filter::parse($filters);
  104. }
  105. /**
  106. * Determine if this collection's filters apply to a given method.
  107. *
  108. * Methods may be included / excluded using the "only" and "except" methods on the
  109. * filter collection. Also, the "on" method may be used to set certain filters to
  110. * only run when the request uses a given HTTP verb.
  111. *
  112. * @param string $method
  113. * @return bool
  114. */
  115. public function applies($method)
  116. {
  117. if (count($this->only) > 0 and ! in_array($method, $this->only)) {
  118. return false;
  119. }
  120. if (count($this->except) > 0 and ! in_array($method, $this->except)) {
  121. return false;
  122. }
  123. if (count($this->methods) > 0 and ! in_array(strtolower(Request::method()), $this->methods)) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. /**
  129. * Set the excluded controller methods.
  130. *
  131. * When methods are excluded, the collection's filters will be run for each
  132. * controller method except those explicitly specified via this method.
  133. *
  134. * <code>
  135. * // Specify a filter for all methods except "index"
  136. * $this->filter('before', 'auth')->except('index');
  137. *
  138. * // Specify a filter for all methods except "index" and "home"
  139. * $this->filter('before', 'auth')->except('index', 'home');
  140. * </code>
  141. *
  142. * @param array $methods
  143. * @return Filter_Collection
  144. */
  145. public function except($methods)
  146. {
  147. $this->except = (count(func_get_args()) > 1) ? func_get_args() : (array) $methods;
  148. return $this;
  149. }
  150. /**
  151. * Set the included controller methods.
  152. *
  153. * This method is the inverse of the "except" methods. The methods specified
  154. * via this method are the only controller methods on which the collection's
  155. * filters will be run.
  156. *
  157. * <code>
  158. * // Specify a filter for only the "index" method
  159. * $this->filter('before', 'auth')->only('index');
  160. *
  161. * // Specify a filter for only the "index" and "home" methods
  162. * $this->filter('before', 'auth')->only('index', 'home');
  163. * </code>
  164. *
  165. * @param array $methods
  166. * @return Filter_Collection
  167. */
  168. public function only($methods)
  169. {
  170. $this->only = (count(func_get_args()) > 1) ? func_get_args() : (array) $methods;
  171. return $this;
  172. }
  173. /**
  174. * Set the HTTP methods for which the filter applies.
  175. *
  176. * Since some filters, such as the CSRF filter, only make sense in a POST
  177. * request context, this method allows you to limit which HTTP methods
  178. * the filter will apply to.
  179. *
  180. * <code>
  181. * // Specify that a filter only applies on POST requests
  182. * $this->filter('before', 'csrf')->on('post');
  183. *
  184. * // Specify that a filter applies for multiple HTTP request methods
  185. * $this->filter('before', 'csrf')->on('post', 'put');
  186. * </code>
  187. *
  188. * @param array $methods
  189. * @return Filter_Collection
  190. */
  191. public function on($methods)
  192. {
  193. $methods = (count(func_get_args()) > 1) ? func_get_args() : (array) $methods;
  194. foreach ($methods as $method) {
  195. $this->methods[] = strtolower($method);
  196. }
  197. return $this;
  198. }
  199. }