action.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. /**
  9. * This file contains a series of method filters that allow you to intercept different parts of
  10. * Lithium's dispatch cycle. The filters below are used for on-demand loading of routing
  11. * configuration, and automatically configuring the correct environment in which the application
  12. * runs.
  13. *
  14. * For more information on in the filters system, see `lithium\util\collection\Filters`.
  15. *
  16. * @see lithium\util\collection\Filters
  17. */
  18. use lithium\core\Libraries;
  19. use lithium\core\Environment;
  20. use lithium\action\Dispatcher;
  21. /**
  22. * This filter intercepts the `run()` method of the `Dispatcher`, and first passes the `'request'`
  23. * parameter (an instance of the `Request` object) to the `Environment` class to detect which
  24. * environment the application is running in. Then, loads all application routes in all plugins,
  25. * loading the default application routes last.
  26. *
  27. * Change this code if plugin routes must be loaded in a specific order (i.e. not the same order as
  28. * the plugins are added in your bootstrap configuration), or if application routes must be loaded
  29. * first (in which case the default catch-all routes should be removed).
  30. *
  31. * If `Dispatcher::run()` is called multiple times in the course of a single request, change the
  32. * `include`s to `include_once`.
  33. *
  34. * @see lithium\action\Request
  35. * @see lithium\core\Environment
  36. * @see lithium\net\http\Router
  37. */
  38. Dispatcher::applyFilter('run', function($self, $params, $chain) {
  39. Environment::set($params['request']);
  40. foreach (array_reverse(Libraries::get()) as $name => $config) {
  41. if ($name === 'lithium') {
  42. continue;
  43. }
  44. $file = "{$config['path']}/config/routes.php";
  45. file_exists($file) ? call_user_func(function() use ($file) { include $file; }) : null;
  46. }
  47. return $chain->next($self, $params, $chain);
  48. });
  49. ?>