Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
Inspector.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Debug;
3 
5 
14 class Inspector {
15 
21  public function getEvents() {
22  return $this->buildHandlerTree(_elgg_services()->events->getAllHandlers());
23  }
24 
30  public function getPluginHooks() {
31  return $this->buildHandlerTree(_elgg_services()->hooks->getAllHandlers());
32  }
33 
39  public function getViewtypes() {
40  return array_keys($this->getViewsData()['locations']);
41  }
42 
50  public function getViews($viewtype = 'default') {
51  global $CONFIG;
52 
53  $view_data = $this->getViewsData();
54 
55  // maps view name to array of ViewComponent[] with priority as keys
56  $views = array();
57 
58  // add plugins and handle overrides
59  foreach ($view_data['locations'][$viewtype] as $view => $location) {
60  $component = new ViewComponent();
61  $component->view = $view;
62  $component->file = $location;
63 
64  $views[$view] = [500 => $component];
65  }
66 
67  // now extensions
68  foreach ($view_data['extensions'] as $view => $extensions) {
69  $view_list = array();
70  foreach ($extensions as $priority => $ext_view) {
71  if (isset($views[$ext_view])) {
72  $view_list[$priority] = $views[$ext_view][500];
73  }
74  }
75  if (count($view_list) > 0) {
76  $views[$view] = $view_list;
77  }
78  }
79 
80  ksort($views);
81 
82  // now overrides
83  foreach ($views as $view => $view_list) {
84  if (!empty($view_data['overrides'][$viewtype][$view])) {
85  $overrides_list = array();
86  foreach ($view_data['overrides'][$viewtype][$view] as $i => $location) {
87  $component = new ViewComponent();
88  $component->overridden = true;
89  $component->view = $view;
90  $component->file = $location;
91 
92  $overrides_list["o:$i"] = $component;
93  }
94  $views[$view] = $overrides_list + $view_list;
95  }
96  }
97 
98  // view handlers
99  $handlers = _elgg_services()->hooks->getAllHandlers();
100 
101  $input_filtered_views = array();
102  if (!empty($handlers['view_vars'])) {
103  $input_filtered_views = array_keys($handlers['view_vars']);
104  }
105 
106  $filtered_views = array();
107  if (!empty($handlers['view'])) {
108  $filtered_views = array_keys($handlers['view']);
109  }
110 
111  $global_hooks = array();
112  if (!empty($handlers['view_vars']['all'])) {
113  $global_hooks[] = 'view_vars, all';
114  }
115  if (!empty($handlers['view']['all'])) {
116  $global_hooks[] = 'view, all';
117  }
118 
119  return array(
120  'views' => $views,
121  'global_hooks' => $global_hooks,
122  'input_filtered_views' => $input_filtered_views,
123  'filtered_views' => $filtered_views,
124  );
125  }
126 
132  public function getWidgets() {
133  $tree = array();
134  foreach (_elgg_services()->widgets->getAllTypes() as $handler => $handler_obj) {
135  $tree[$handler] = array($handler_obj->name, implode(',', array_values($handler_obj->context)));
136  }
137 
138  ksort($tree);
139 
140  return $tree;
141  }
142 
143 
151  public function getActions() {
152  $tree = array();
153  $access = array(
154  'public' => 'public',
155  'logged_in' => 'logged in only',
156  'admin' => 'admin only',
157  );
158  $start = strlen(elgg_get_root_path());
159  foreach (_elgg_services()->actions->getAllActions() as $action => $info) {
160  $info['file'] = substr($info['file'], $start);
161  $tree[$action] = array($info['file'], $access[$info['access']]);
162  }
163  ksort($tree);
164  return $tree;
165  }
166 
172  public function getSimpleCache() {
173 
174  $simplecache = elgg_extract('simplecache', $this->getViewsData(), []);
175  $locations = elgg_extract('locations', $this->getViewsData(), []);
176 
177  $tree = [];
178  foreach ($simplecache as $view => $foo) {
179  $tree[$view] = '';
180  }
181 
182  // add all static views
183  foreach ($locations as $viewtype) {
184  foreach ($viewtype as $view => $location) {
185  if (pathinfo($location, PATHINFO_EXTENSION) !== 'php') {
186  $tree[$view] = '';
187  }
188  }
189  }
190 
191  ksort($tree);
192 
193  return $tree;
194  }
195 
201  public function getWebServices() {
202  global $API_METHODS;
203 
204  $tree = array();
205  foreach ($API_METHODS as $method => $info) {
206  $params = implode(', ', array_keys(elgg_extract('parameters', $info, [])));
207  if (!$params) {
208  $params = 'none';
209  }
210  $tree[$method] = array(
211  $info['function'],
212  "params: $params",
213  $info['call_method'],
214  ($info['require_api_auth']) ? 'API authentication required' : 'No API authentication required',
215  ($info['require_user_auth']) ? 'User authentication required' : 'No user authentication required',
216  );
217  }
218 
219  ksort($tree);
220 
221  return $tree;
222  }
223 
229  public function getMenus() {
230 
231  $menus = elgg_get_config('menus');
232 
233  // get JIT menu items
234  // note that 'river' is absent from this list - hooks attempt to get object/subject entities cause problems
235  $jit_menus = array('annotation', 'entity', 'login', 'longtext', 'owner_block', 'user_hover', 'widget');
236 
237  // create generic ElggEntity, ElggAnnotation, ElggUser, ElggWidget
238  $annotation = new \ElggAnnotation();
239  $annotation->id = 999;
240  $annotation->name = 'generic_comment';
241  $annotation->value = 'testvalue';
242 
243  $entity = new \ElggObject();
244  $entity->guid = 999;
245  $entity->subtype = 'blog';
246  $entity->title = 'test entity';
247  $entity->access_id = ACCESS_PUBLIC;
248 
249  $user = new \ElggUser();
250  $user->guid = 999;
251  $user->name = "Test User";
252  $user->username = 'test_user';
253 
254  $widget = new \ElggWidget();
255  $widget->guid = 999;
256  $widget->title = 'test widget';
257 
258  // call plugin hooks
259  foreach ($jit_menus as $type) {
260  $params = array('entity' => $entity, 'annotation' => $annotation, 'user' => $user);
261  switch ($type){
262  case 'owner_block':
263  case 'user_hover':
264  $params['entity'] = $user;
265  break;
266  case 'widget':
267  // this does not work because you cannot set a guid on an entity
268  $params['entity'] = $widget;
269  break;
270  case 'longtext':
271  $params['id'] = rand();
272  break;
273  default:
274  break;
275  }
276  $menus[$type] = elgg_trigger_plugin_hook('register', "menu:$type", $params, array());
277  }
278 
279  // put the menus in tree form for inspection
280  $tree = array();
281 
282  foreach ($menus as $menu_name => $attributes) {
283  foreach ($attributes as $item) {
284  /* @var \ElggMenuItem $item */
285  $name = $item->getName();
286  $text = htmlspecialchars($item->getText(), ENT_QUOTES, 'UTF-8', false);
287  $href = $item->getHref();
288  if ($href === false) {
289  $href = 'not a link';
290  } elseif ($href === "") {
291  $href = 'not a direct link - possibly ajax';
292  }
293  $section = $item->getSection();
294  $parent = $item->getParentName();
295  if (!$parent) {
296  $parent = 'none';
297  }
298 
299  $tree[$menu_name][$name] = array(
300  "text: $text",
301  "href: $href",
302  "section: $section",
303  "parent: $parent",
304  );
305  }
306  }
307 
308  ksort($tree);
309 
310  return $tree;
311  }
312 
322  public function describeCallable($callable, $file_root = '') {
323  if (is_string($callable)) {
324  return $callable;
325  }
326  if (is_array($callable) && array_keys($callable) === array(0, 1) && is_string($callable[1])) {
327  if (is_string($callable[0])) {
328  return "{$callable[0]}::{$callable[1]}";
329  }
330  return "(" . get_class($callable[0]) . ")->{$callable[1]}";
331  }
332  if ($callable instanceof \Closure) {
333  $ref = new \ReflectionFunction($callable);
334  $file = $ref->getFileName();
335  $line = $ref->getStartLine();
336 
337  if ($file_root && 0 === strpos($file, $file_root)) {
338  $file = substr($file, strlen($file_root));
339  }
340 
341  return "(Closure {$file}:{$line})";
342  }
343  if (is_object($callable)) {
344  return "(" . get_class($callable) . ")->__invoke()";
345  }
346  return print_r($callable, true);
347  }
348 
356  protected function buildHandlerTree($all_handlers) {
357  $tree = array();
358  $root = elgg_get_root_path();
359 
360  foreach ($all_handlers as $hook => $types) {
361  foreach ($types as $type => $priorities) {
362  ksort($priorities);
363 
364  foreach ($priorities as $priority => $handlers) {
365  foreach ($handlers as $callable) {
366  $description = $this->describeCallable($callable, $root);
367  $callable = "$priority: $description";
368  $tree["$hook, $type"][] = $callable;
369  }
370  }
371  }
372  }
373 
374  ksort($tree);
375 
376  return $tree;
377  }
378 
384  private function getViewsData() {
385  static $data;
386  if ($data === null) {
387  $data = _elgg_services()->views->getInspectorData();
388  }
389  return $data;
390  }
391 }
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
$view
Definition: crop.php:34
$action
Definition: full.php:125
if(!array_key_exists($filename, $text_files)) $file
$annotation
Elgg default annotation view.
Definition: default.php:10
getEvents()
Get Elgg event information.
Definition: Inspector.php:21
getWidgets()
Get Elgg widget information.
Definition: Inspector.php:132
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$method
Definition: form.php:25
getPluginHooks()
Get Elgg plugin hooks information.
Definition: Inspector.php:30
$extensions
$data
Definition: opendd.php:13
getMenus()
Get information about registered menus.
Definition: Inspector.php:229
$widget
Definition: delete.php:9
getViews($viewtype= 'default')
Get Elgg view information.
Definition: Inspector.php:50
if($screenshots) $info
Definition: details.php:58
$params
Definition: login.php:72
$text
Definition: default.php:25
if($categories) $description
Definition: full.php:166
getActions()
Get Elgg actions information.
Definition: Inspector.php:151
$item
Definition: item.php:12
global $CONFIG
$user
Definition: ban.php:13
buildHandlerTree($all_handlers)
Build a tree of event handlers.
Definition: Inspector.php:356
elgg_get_root_path()
Get the root directory path for this installation.
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:801
getViewtypes()
Get all view types for known views.
Definition: Inspector.php:39
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1310
getSimpleCache()
Get simplecache information.
Definition: Inspector.php:172
const ACCESS_PUBLIC
Definition: elgglib.php:1990
$entity
Definition: delete.php:7
$handler
Definition: add.php:10
describeCallable($callable, $file_root= '')
Get a string description of a callback.
Definition: Inspector.php:322
$attributes
Definition: ajax_loader.php:13
$priority
$access
Definition: save.php:15
getWebServices()
Get Elgg web services API methods.
Definition: Inspector.php:201
if(!$display_name) $type
Definition: delete.php:27