filter.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php namespace Laravel\Routing;
  2. class Filter {
  3. /**
  4. * The route filters for the application.
  5. *
  6. * @var array
  7. */
  8. protected static $filters = array();
  9. /**
  10. * Register an array of route filters.
  11. *
  12. * @param array $filters
  13. * @return void
  14. */
  15. public static function register($filters)
  16. {
  17. static::$filters = array_merge(static::$filters, $filters);
  18. }
  19. /**
  20. * Call a filter or set of filters.
  21. *
  22. * @param array|string $filters
  23. * @param array $parameters
  24. * @param bool $override
  25. * @return mixed
  26. */
  27. public static function run($filters, $parameters = array(), $override = false)
  28. {
  29. if (is_string($filters)) $filters = explode('|', $filters);
  30. foreach ((array) $filters as $filter)
  31. {
  32. // Parameters may be passed into routes by specifying the list of
  33. // parameters after a colon. If parameters are present, we will
  34. // merge them into the parameter array that was passed to the
  35. // method and slice the parameters off of the filter string.
  36. if (($colon = strpos($filter, ':')) !== false)
  37. {
  38. $parameters = array_merge($parameters, explode(',', substr($filter, $colon + 1)));
  39. $filter = substr($filter, 0, $colon);
  40. }
  41. if ( ! isset(static::$filters[$filter])) continue;
  42. $response = call_user_func_array(static::$filters[$filter], $parameters);
  43. // "Before" filters may override the request cycle. For example,
  44. // an authentication filter may redirect a user to a login view
  45. // if they are not logged in. Because of this, we will return
  46. // the first filter response if overriding is enabled.
  47. if ( ! is_null($response) and $override) return $response;
  48. }
  49. }
  50. }