Router.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * Delegates requests to controllers based on the registered configuration.
  5. *
  6. * Plugin devs should use these wrapper functions:
  7. * * elgg_register_page_handler
  8. * * elgg_unregister_page_handler
  9. *
  10. * @package Elgg.Core
  11. * @subpackage Router
  12. * @since 1.9.0
  13. * @access private
  14. */
  15. class Router {
  16. private $handlers = array();
  17. private $hooks;
  18. /**
  19. * Constructor
  20. *
  21. * @param \Elgg\PluginHooksService $hooks For customized routing.
  22. */
  23. public function __construct(\Elgg\PluginHooksService $hooks) {
  24. $this->hooks = $hooks;
  25. }
  26. /**
  27. * Routes the request to a registered page handler
  28. *
  29. * This function triggers a plugin hook `'route', $identifier` so that plugins can
  30. * modify the routing or handle a request.
  31. *
  32. * @param \Elgg\Http\Request $request The request to handle.
  33. * @return boolean Whether the request was routed successfully.
  34. * @access private
  35. */
  36. public function route(\Elgg\Http\Request $request) {
  37. $segments = $request->getUrlSegments();
  38. if ($segments) {
  39. $identifier = array_shift($segments);
  40. } else {
  41. $identifier = '';
  42. // this plugin hook is deprecated. Use elgg_register_page_handler()
  43. // to register for the '' (empty string) handler.
  44. // allow plugins to override the front page (return true to indicate
  45. // that the front page has been served)
  46. $result = _elgg_services()->hooks->trigger('index', 'system', null, false);
  47. if ($result === true) {
  48. elgg_deprecated_notice("The 'index', 'system' plugin has been deprecated. See elgg_front_page_handler()", 1.9);
  49. exit;
  50. }
  51. }
  52. // return false to stop processing the request (because you handled it)
  53. // return a new $result array if you want to route the request differently
  54. $result = array(
  55. 'identifier' => $identifier,
  56. 'handler' => $identifier, // backward compatibility
  57. 'segments' => $segments,
  58. );
  59. $result = $this->hooks->trigger('route', $identifier, $result, $result);
  60. if ($result === false) {
  61. return true;
  62. }
  63. if ($identifier != $result['identifier']) {
  64. $identifier = $result['identifier'];
  65. } else if ($identifier != $result['handler']) {
  66. $identifier = $result['handler'];
  67. }
  68. $segments = $result['segments'];
  69. $handled = false;
  70. if (isset($this->handlers[$identifier]) && is_callable($this->handlers[$identifier])) {
  71. $function = $this->handlers[$identifier];
  72. $handled = call_user_func($function, $segments, $identifier);
  73. }
  74. return $handled || headers_sent();
  75. }
  76. /**
  77. * Register a function that gets called when the first part of a URL is
  78. * equal to the identifier.
  79. *
  80. * @param string $identifier The page type to handle
  81. * @param string $function Your function name
  82. *
  83. * @return bool Depending on success
  84. */
  85. public function registerPageHandler($identifier, $function) {
  86. if (is_callable($function, true)) {
  87. $this->handlers[$identifier] = $function;
  88. return true;
  89. }
  90. return false;
  91. }
  92. /**
  93. * Unregister a page handler for an identifier
  94. *
  95. * @param string $identifier The page type identifier
  96. *
  97. * @return void
  98. */
  99. public function unregisterPageHandler($identifier) {
  100. unset($this->handlers[$identifier]);
  101. }
  102. /**
  103. * Get page handlers as array of identifier => callback
  104. *
  105. * @return array
  106. */
  107. public function getPageHandlers() {
  108. return $this->handlers;
  109. }
  110. }