filter.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php namespace System\Routing;
  2. class Filter {
  3. /**
  4. * The loaded route filters.
  5. *
  6. * @var array
  7. */
  8. private static $filters = array();
  9. /**
  10. * Register a set 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. * Clear all of the registered route filters.
  21. *
  22. * @return void
  23. */
  24. public static function clear()
  25. {
  26. static::$filters = array();
  27. }
  28. /**
  29. * Call a set of route filters.
  30. *
  31. * @param string $filter
  32. * @param array $parameters
  33. * @param bool $override
  34. * @return mixed
  35. */
  36. public static function call($filters, $parameters = array(), $override = false)
  37. {
  38. foreach (explode(', ', $filters) as $filter)
  39. {
  40. if ( ! isset(static::$filters[$filter]))
  41. {
  42. throw new \Exception("Route filter [$filter] is not defined.");
  43. }
  44. $response = call_user_func_array(static::$filters[$filter], $parameters);
  45. // "Before" filters may override the request cycle. For example, an authentication
  46. // filter may redirect a user to a login view if they are not logged in. Because of
  47. // this, we will return the first filter response if overriding is enabled.
  48. if ( ! is_null($response) and $override)
  49. {
  50. return $response;
  51. }
  52. }
  53. }
  54. }