Elgg  Version 3.0
Inspector.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Debug;
3 
5 use Elgg\Includer;
7 
16 class Inspector {
17 
23  public function getEvents() {
24  return $this->buildHandlerTree(_elgg_services()->events->getAllHandlers());
25  }
26 
32  public function getPluginHooks() {
33  return $this->buildHandlerTree(_elgg_services()->hooks->getAllHandlers());
34  }
35 
41  public function getViewtypes() {
42  return array_keys($this->getViewsData()['locations']);
43  }
44 
52  public function getViews($viewtype = 'default') {
53  $view_data = $this->getViewsData();
54 
55  // maps view name to array of ViewComponent[] with priority as keys
56  $views = [];
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 = [];
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 = [];
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 = [];
102  if (!empty($handlers['view_vars'])) {
103  $input_filtered_views = array_keys($handlers['view_vars']);
104  }
105 
106  $filtered_views = [];
107  if (!empty($handlers['view'])) {
108  $filtered_views = array_keys($handlers['view']);
109  }
110 
111  $global_hooks = [];
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 [
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 = [];
134  foreach (_elgg_services()->widgets->getAllTypes() as $handler => $handler_obj) {
135  $tree[$handler] = [$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 = [];
153  $access = [
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  if (isset($info['file'])) {
161  $info['file'] = substr($info['file'], $start);
162  } else if ($info['controller']) {
163  $info['file'] = $this->describeCallable($info['controller']);
164  }
165  $tree[$action] = [$info['file'], $access[$info['access']]];
166  }
167  ksort($tree);
168  return $tree;
169  }
170 
176  public function getSimpleCache() {
177 
178  $simplecache = elgg_extract('simplecache', $this->getViewsData(), []);
179  $locations = elgg_extract('locations', $this->getViewsData(), []);
180 
181  $tree = [];
182  foreach ($simplecache as $view => $foo) {
183  $tree[$view] = '';
184  }
185 
186  // add all static views
187  foreach ($locations as $viewtype) {
188  foreach ($viewtype as $view => $location) {
189  if (pathinfo($location, PATHINFO_EXTENSION) !== 'php') {
190  $tree[$view] = '';
191  }
192  }
193  }
194 
195  ksort($tree);
196 
197  return $tree;
198  }
199 
207  public function getRoutes() {
208  $tree = [];
209  foreach (_elgg_services()->routeCollection->all() as $name => $route) {
210  $handler = $route->getDefault('_handler') ? : '';
211  if ($handler) {
213  }
214 
215  $controller = $route->getDefault('_controller') ? : '';
216  if ($controller) {
217  $controller = $this->describeCallable($controller);
218  }
219 
220  $resource = $route->getDefault('_resource') ? : '';
221 
222  $file = $route->getDefault('_file') ? : '';
223 
224  $middleware = $route->getDefault('_middleware');
225  if (!is_array($middleware)) {
226  if (!empty($middleware)) {
227  $middleware = [$middleware];
228  } else {
229  $middleware = [];
230  }
231  }
232  $middleware = array_map(function($e) {
233  return $this->describeCallable($e);
234  }, $middleware);
235 
236  $tree[$name] = [
237  $route->getPath(),
238  $resource,
239  $handler,
240  $controller,
241  $file,
242  $middleware,
243  ];
244  }
245  uasort($tree, function($e1, $e2) {
246  return strcmp($e1[0], $e2[0]);
247  });
248 
249  return $tree;
250  }
251 
257  public function getWebServices() {
258  global $API_METHODS;
259 
260  $tree = [];
261  foreach ($API_METHODS as $method => $info) {
262  $params = implode(', ', array_keys(elgg_extract('parameters', $info, [])));
263  if (!$params) {
264  $params = 'none';
265  }
266  $tree[$method] = [
267  $info['function'],
268  "params: $params",
269  $info['call_method'],
270  ($info['require_api_auth']) ? 'API authentication required' : 'No API authentication required',
271  ($info['require_user_auth']) ? 'User authentication required' : 'No user authentication required',
272  ];
273  }
274 
275  ksort($tree);
276 
277  return $tree;
278  }
279 
285  public function getMenus() {
286 
287  $menus = _elgg_config()->menus;
288 
289  // get JIT menu items
290  // note that 'river' is absent from this list - hooks attempt to get object/subject entities cause problems
291  $jit_menus = ['annotation', 'entity', 'login', 'longtext', 'owner_block', 'user_hover', 'widget'];
292 
293  // create generic ElggEntity, ElggAnnotation, ElggUser, ElggWidget
294  $annotation = new \ElggAnnotation();
295  $annotation->id = 999;
296  $annotation->name = 'generic_comment';
297  $annotation->value = 'testvalue';
298 
299  $entity = new \ElggObject();
300  $entity->guid = 999;
301  $entity->subtype = 'blog';
302  $entity->title = 'test entity';
303  $entity->access_id = ACCESS_PUBLIC;
304 
305  $user = new \ElggUser();
306  $user->guid = 999;
307  $user->name = "Test User";
308  $user->username = 'test_user';
309 
310  $widget = new \ElggWidget();
311  $widget->guid = 999;
312  $widget->title = 'test widget';
313 
314  // call plugin hooks
315  foreach ($jit_menus as $type) {
316  $params = ['entity' => $entity, 'annotation' => $annotation, 'user' => $user];
317  switch ($type) {
318  case 'owner_block':
319  case 'user_hover':
320  $params['entity'] = $user;
321  break;
322  case 'widget':
323  // this does not work because you cannot set a guid on an entity
324  $params['entity'] = $widget;
325  break;
326  case 'longtext':
327  $params['id'] = rand();
328  break;
329  default:
330  break;
331  }
332  $menus[$type] = _elgg_services()->hooks->trigger('register', "menu:$type", $params, []);
333  }
334 
335  // put the menus in tree form for inspection
336  $tree = [];
337 
338  foreach ($menus as $menu_name => $attributes) {
339  foreach ($attributes as $item) {
340  /* @var \ElggMenuItem $item */
341  $name = $item->getName();
342  $text = htmlspecialchars($item->getText(), ENT_QUOTES, 'UTF-8', false);
343  $href = $item->getHref();
344  if ($href === false) {
345  $href = 'not a link';
346  } elseif ($href === "") {
347  $href = 'not a direct link - possibly ajax';
348  }
349  $section = $item->getSection();
350  $parent = $item->getParentName();
351  if (!$parent) {
352  $parent = 'none';
353  }
354 
355  $tree[$menu_name][$name] = [
356  "text: $text",
357  "href: $href",
358  "section: $section",
359  "parent: $parent",
360  ];
361  }
362  }
363 
364  ksort($tree);
365 
366  return $tree;
367  }
368 
378  public function describeCallable($callable, $file_root = '') {
379  return _elgg_services()->handlers->describeCallable($callable, $file_root);
380  }
381 
389  protected function buildHandlerTree($all_handlers) {
390  $tree = [];
391  $root = elgg_get_root_path();
392  $handlers_svc = _elgg_services()->handlers;
393 
394  foreach ($all_handlers as $hook => $types) {
395  foreach ($types as $type => $priorities) {
396  ksort($priorities);
397 
398  foreach ($priorities as $priority => $handlers) {
399  foreach ($handlers as $callable) {
400  $description = $handlers_svc->describeCallable($callable, $root);
401  $callable = "$priority: $description";
402  $tree["$hook, $type"][] = $callable;
403  }
404  }
405  }
406  }
407 
408  ksort($tree);
409 
410  return $tree;
411  }
412 
418  private function getViewsData() {
419  static $data;
420  if ($data === null) {
421  $data = _elgg_services()->views->getInspectorData();
422  }
423  return $data;
424  }
425 
433  public function getServices() {
434  $tree = [];
435 
436  foreach (_elgg_services()->dic_loader->getDefinitions() as $definition) {
437  $services = Includer::includeFile($definition);
438 
439  foreach ($services as $name => $service) {
440  $tree[$name] = [get_class(elgg()->$name), Paths::sanitize($definition, false)];
441  }
442  }
443 
444  ksort($tree);
445 
446  return $tree;
447  }
448 }
static includeFile($file)
Include a file with as little context as possible.
Definition: Includer.php:18
$action
Definition: full.php:111
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
if(!array_key_exists($filename, $text_files)) $file
$params
Saves global plugin settings.
Definition: save.php:13
$annotation
Elgg default annotation view.
Definition: default.php:10
getEvents()
Get Elgg event information.
Definition: Inspector.php:23
getWidgets()
Get Elgg widget information.
Definition: Inspector.php:132
if(!$items) $item
Definition: delete.php:13
getPluginHooks()
Get Elgg plugin hooks information.
Definition: Inspector.php:32
$extensions
getServices()
Returns public DI services.
Definition: Inspector.php:433
getMenus()
Get information about registered menus.
Definition: Inspector.php:285
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:57
$type
Definition: delete.php:21
$resource
getViews($viewtype= 'default')
Get Elgg view information.
Definition: Inspector.php:52
$text
Definition: default.php:28
if(!$owner||!$owner->canEdit()) if(!$owner->hasIcon('master')) if(!$owner->saveIconFromElggFile($owner->getIcon('master'), 'icon', $coords)) $view
Definition: crop.php:30
$entity
Definition: reset.php:8
getActions()
Get Elgg actions information.
Definition: Inspector.php:151
$user
Definition: ban.php:7
buildHandlerTree($all_handlers)
Build a tree of event handlers.
Definition: Inspector.php:389
elgg_get_root_path()
Get the project path (where composer is installed), ending with slash.
elgg ElggUser
Definition: ElggUser.js:12
if(!empty($screenshots)) $info
Definition: details.php:59
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$viewtype
Definition: default.php:11
getViewtypes()
Get all view types for known views.
Definition: Inspector.php:41
getRoutes()
Get Elgg route information.
Definition: Inspector.php:207
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
Definition: item.php:39
if($guid===false) $widget
Definition: add.php:30
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:1131
getSimpleCache()
Get simplecache information.
Definition: Inspector.php:176
WARNING: API IN FLUX.
Definition: Inspector.php:16
_elgg_config()
Get the Elgg config service.
$location
Definition: default.php:42
static sanitize($path, $append_slash=true)
Sanitise file paths ensuring that they begin and end with slashes etc.
Definition: Paths.php:76
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
const ACCESS_PUBLIC
Definition: constants.php:14
$handler
Definition: add.php:7
if(elgg_view_exists("plugins/{$plugin_id}/settings")) $description
Definition: full.php:179
describeCallable($callable, $file_root= '')
Get a string description of a callback.
Definition: Inspector.php:378
$attributes
Definition: ajax_loader.php:13
var elgg
Definition: elgglib.js:4
$priority
if(empty($value)&&$value!==0 &&$value!== '0') $href
Definition: tag.php:16
$views
Definition: item.php:17
$access
Definition: save.php:18
getWebServices()
Get Elgg web services API methods.
Definition: Inspector.php:257