Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
Router.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
5 
18 class Router {
19  use Profilable;
20 
21  private $handlers = array();
22  private $hooks;
23 
29  public function __construct(\Elgg\PluginHooksService $hooks) {
30  $this->hooks = $hooks;
31  }
32 
43  public function route(\Elgg\Http\Request $request) {
44  $segments = $request->getUrlSegments();
45  if ($segments) {
46  $identifier = array_shift($segments);
47  } else {
48  $identifier = '';
49 
50  // this plugin hook is deprecated. Use elgg_register_page_handler()
51  // to register for the '' (empty string) handler.
52  // allow plugins to override the front page (return true to indicate
53  // that the front page has been served)
54  $result = _elgg_services()->hooks->trigger('index', 'system', null, false);
55  if ($result === true) {
56  elgg_deprecated_notice("The 'index', 'system' plugin has been deprecated. See elgg_front_page_handler()", 1.9);
57  exit;
58  }
59  }
60 
61  // return false to stop processing the request (because you handled it)
62  // return a new $result array if you want to route the request differently
63  $old = array(
64  'identifier' => $identifier,
65  'handler' => $identifier, // backward compatibility
66  'segments' => $segments,
67  );
68 
69  if ($this->timer) {
70  $this->timer->begin(['build page']);
71  }
72 
73  $result = $this->hooks->trigger('route', $identifier, $old, $old);
74  if ($result === false) {
75  return true;
76  }
77 
78  if ($result !== $old) {
79  _elgg_services()->logger->warn('Use the route:rewrite hook to modify routes.');
80  }
81 
82  if ($identifier != $result['identifier']) {
83  $identifier = $result['identifier'];
84  } else if ($identifier != $result['handler']) {
85  $identifier = $result['handler'];
86  }
87 
88  $segments = $result['segments'];
89 
90  $handled = false;
91  ob_start();
92 
93  if (isset($this->handlers[$identifier]) && is_callable($this->handlers[$identifier])) {
94  $function = $this->handlers[$identifier];
95  $handled = call_user_func($function, $segments, $identifier);
96  }
97 
98  $output = ob_get_clean();
99 
100  $ajax_api = _elgg_services()->ajax;
101  if ($ajax_api->isReady()) {
102  $path = implode('/', $request->getUrlSegments());
103  $ajax_api->respondFromOutput($output, "path:$path");
104  return true;
105  }
106 
107  echo $output;
108  return $handled || headers_sent();
109  }
110 
120  public function registerPageHandler($identifier, $function) {
121  if (is_callable($function, true)) {
122  $this->handlers[$identifier] = $function;
123  return true;
124  }
125 
126  return false;
127  }
128 
136  public function unregisterPageHandler($identifier) {
137  unset($this->handlers[$identifier]);
138  }
139 
145  public function getPageHandlers() {
146  return $this->handlers;
147  }
148 
157  public function allowRewrite(Request $request) {
158  $segments = $request->getUrlSegments();
159  if ($segments) {
160  $identifier = array_shift($segments);
161  } else {
162  $identifier = '';
163  }
164 
165  $old = array(
166  'identifier' => $identifier,
167  'segments' => $segments,
168  );
169  $new = _elgg_services()->hooks->trigger('route:rewrite', $identifier, $old, $old);
170  if ($new === $old) {
171  return $request;
172  }
173 
174  if (!isset($new['identifier'])
175  || !isset($new['segments'])
176  || !is_string($new['identifier'])
177  || !is_array($new['segments'])
178  ) {
179  throw new \RuntimeException('rewrite_path handler returned invalid route data.');
180  }
181 
182  // rewrite request
183  $segments = $new['segments'];
184  array_unshift($segments, $new['identifier']);
185  return $request->setUrlSegments($segments);
186  }
187 }
188 
unregisterPageHandler($identifier)
Unregister a page handler for an identifier.
Definition: Router.php:136
Elgg HTTP request.
Definition: Request.php:12
__construct(\Elgg\PluginHooksService $hooks)
Constructor.
Definition: Router.php:29
$path
Definition: details.php:88
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:120
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:1070
route(\Elgg\Http\Request $request)
Routes the request to a registered page handler.
Definition: Router.php:43
getPageHandlers()
Get page handlers as array of identifier => callback.
Definition: Router.php:145
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
exit
Definition: autoloader.php:34
$output
Definition: item.php:10
allowRewrite(Request $request)
Filter a request through the route:rewrite hook.
Definition: Router.php:157