Elgg  Version 2.3
Router.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
8 
21 class Router {
22 
24 
25  private $handlers = array();
26  private $hooks;
27 
33  public function __construct(PluginHooksService $hooks) {
34  $this->hooks = $hooks;
35  }
36 
47  public function route(Request $request) {
48  $segments = $request->getUrlSegments();
49  if ($segments) {
50  $identifier = array_shift($segments);
51  } else {
52  $identifier = '';
53 
54  // this plugin hook is deprecated. Use elgg_register_page_handler()
55  // to register for the '' (empty string) handler.
56  // allow plugins to override the front page (return true to indicate
57  // that the front page has been served)
58  $result = _elgg_services()->hooks->trigger('index', 'system', null, false);
59  if ($result === true) {
60  elgg_deprecated_notice("The 'index', 'system' plugin has been deprecated. See elgg_front_page_handler()", 1.9);
61  exit;
62  }
63  }
64 
65  // return false to stop processing the request (because you handled it)
66  // return a new $result array if you want to route the request differently
67  $old = array(
68  'identifier' => $identifier,
69  'handler' => $identifier, // backward compatibility
70  'segments' => $segments,
71  );
72 
73  if ($this->timer) {
74  $this->timer->begin(['build page']);
75  }
76 
77  ob_start();
78 
79  $result = $this->hooks->trigger('route', $identifier, $old, $old);
80  if ($result === false) {
81  $output = ob_get_clean();
82  $response = elgg_ok_response($output);
83  } else {
84  if ($result !== $old) {
85  _elgg_services()->logger->warn('Use the route:rewrite hook to modify routes.');
86  }
87 
88  if ($identifier != $result['identifier']) {
89  $identifier = $result['identifier'];
90  } else if ($identifier != $result['handler']) {
91  $identifier = $result['handler'];
92  }
93 
94  $segments = $result['segments'];
95 
96  $response = false;
97 
98  if (isset($this->handlers[$identifier]) && is_callable($this->handlers[$identifier])) {
99  $function = $this->handlers[$identifier];
100  $response = call_user_func($function, $segments, $identifier);
101  }
102 
103  $output = ob_get_clean();
104 
105  if ($response === false) {
106  return headers_sent();
107  }
108 
109  if (!$response instanceof ResponseBuilder) {
110  $response = elgg_ok_response($output);
111  }
112  }
113 
114  if (_elgg_services()->responseFactory->getSentResponse()) {
115  return true;
116  }
117 
118  _elgg_services()->responseFactory->respond($response);
119  return headers_sent();
120  }
121 
131  public function registerPageHandler($identifier, $function) {
132  if (is_callable($function, true)) {
133  $this->handlers[$identifier] = $function;
134  return true;
135  }
136 
137  return false;
138  }
139 
147  public function unregisterPageHandler($identifier) {
148  unset($this->handlers[$identifier]);
149  }
150 
156  public function getPageHandlers() {
157  return $this->handlers;
158  }
159 
168  public function allowRewrite(Request $request) {
169  $segments = $request->getUrlSegments();
170  if ($segments) {
171  $identifier = array_shift($segments);
172  } else {
173  $identifier = '';
174  }
175 
176  $old = array(
177  'identifier' => $identifier,
178  'segments' => $segments,
179  );
180  $new = _elgg_services()->hooks->trigger('route:rewrite', $identifier, $old, $old);
181  if ($new === $old) {
182  return $request;
183  }
184 
185  if (!isset($new['identifier']) || !isset($new['segments']) || !is_string($new['identifier']) || !is_array($new['segments'])
186  ) {
187  throw new RuntimeException('rewrite_path handler returned invalid route data.');
188  }
189 
190  // rewrite request
191  $segments = $new['segments'];
192  array_unshift($segments, $new['identifier']);
193  return $request->setUrlSegments($segments);
194  }
195 
196 }
unregisterPageHandler($identifier)
Unregister a page handler for an identifier.
Definition: Router.php:147
HTTP response builder interface.
Elgg HTTP request.
Definition: Request.php:12
getUrlSegments($raw=false)
Get the Elgg URL segments.
Definition: Request.php:21
setUrlSegments(array $segments)
Get a cloned request with new Elgg URL segments.
Definition: Request.php:40
trait Profilable
Make an object accept a timer.
Definition: Profilable.php:9
registerPageHandler($identifier, $function)
Register a function that gets called when the first part of a URL is equal to the identifier...
Definition: Router.php:131
__construct(PluginHooksService $hooks)
Constructor.
Definition: Router.php:33
Save menu items.
route(Request $request)
Routes the request to a registered page handler.
Definition: Router.php:47
if(is_array($custom_items)) $new
Definition: save.php:63
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
getPageHandlers()
Get page handlers as array of identifier => callback.
Definition: Router.php:156
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
elgg_ok_response($content= '', $message= '', $forward_url=null, $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
exit
Definition: autoloader.php:34
$output
Definition: item.php:10
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
allowRewrite(Request $request)
Filter a request through the route:rewrite hook.
Definition: Router.php:168