Skip to content

Instantly share code, notes, and snippets.

@mrclay
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrclay/764b8b9d5e61564ec957 to your computer and use it in GitHub Desktop.
Save mrclay/764b8b9d5e61564ec957 to your computer and use it in GitHub Desktop.
Elgg: in HTML pages, logs the PHP included file paths to the JS console
<?php
// Don't use in production!
// Does not cover shutdown event activities.
elgg_register_plugin_hook_handler('output', 'page', function ($h, $t, $html, $p) {
if (false === strpos($html, '</body>')) {
return;
}
$root = elgg_get_root_path();
$root_len = strlen($root);
$data_root = elgg_get_data_path();
$data_root_len = strlen($data_root);
$stats = [];
// does not include non-include file reads
foreach (get_included_files() as $file) {
// rewrite paths
if (0 === strpos($file, $root)) {
$file = "<elgg>/" . substr($file, $root_len);
} elseif (0 === strpos($file, $data_root)) {
$file = "<data>/" . substr($file, $data_root_len);
}
// build stats
$stats['all'][] = $file;
if (0 === strpos($file, '<elgg>/views/') || preg_match('~^<elgg>/mod/[^/]+/views/~', $file)) {
$stats['view files'][] = $file;
} elseif (0 === strpos($file, '<elgg>/engine/classes/')) {
$stats['core classes'][] = substr($file, strlen('<elgg>/engine/classes/'));
} elseif (0 === strpos($file, '<elgg>/engine/')) {
$stats['core others'][] = substr($file, strlen('<elgg>/engine/'));
} elseif (preg_match('~<elgg>/vendor/([^/]+/[^/]+)/~', $file, $m)) {
$stats['vendor'][$m[1]][] = substr($file, strlen($m[0]));
} elseif (0 === strpos($file, '<elgg>/mod/')) {
$stats['plugin files'][] = substr($file, strlen('<elgg>/mod/'));
} else {
$stats['others'][] = $file;
}
}
$script = "<script>console.log('PHP include()s:', " . json_encode($stats) . ");</script>";
$html = str_replace('</body>', "$script</body>", $html);
return $html;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment