filter.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php namespace System;
  2. class Filter {
  3. /**
  4. * The loaded route filters.
  5. *
  6. * @var array
  7. */
  8. public static $filters;
  9. /**
  10. * Call a set of route filters.
  11. *
  12. * @param string $filter
  13. * @param array $parameters
  14. * @return mixed
  15. */
  16. public static function call($filters, $parameters = array())
  17. {
  18. // --------------------------------------------------------------
  19. // Load the route filters.
  20. // --------------------------------------------------------------
  21. if (is_null(static::$filters))
  22. {
  23. static::$filters = require APP_PATH.'filters'.EXT;
  24. }
  25. foreach (explode(', ', $filters) as $filter)
  26. {
  27. // --------------------------------------------------------------
  28. // Verify that the filter is defined.
  29. // --------------------------------------------------------------
  30. if ( ! isset(static::$filters[$filter]))
  31. {
  32. throw new \Exception("Route filter [$filter] is not defined.");
  33. }
  34. $response = call_user_func_array(static::$filters[$filter], $parameters);
  35. // --------------------------------------------------------------
  36. // If the filter returned a response, return it.
  37. // --------------------------------------------------------------
  38. if ( ! is_null($response))
  39. {
  40. return $response;
  41. }
  42. }
  43. }
  44. }