Skip to content

Instantly share code, notes, and snippets.

@mrsinguyen
Created June 30, 2010 02:02
Show Gist options
  • Save mrsinguyen/458120 to your computer and use it in GitHub Desktop.
Save mrsinguyen/458120 to your computer and use it in GitHub Desktop.
PHP's reflection api
<?php
// $Id$
function reflection_menu() {
$items = array();
$items['reflection-page'] = array(
'page callback' => 'reflection_page',
'title' => 'Reflection test',
'access arguments' => array('access content'),
);
return $items;
}
function reflection_page() {
$header = array("path", "Declared by", 'Callback', "Callback Location");
foreach (module_implements('menu') as $hook) {
// get the function by its ball
$func = new ReflectionFunction($hook . '_menu');
// know where it lives
$fun = $func->getStartLine();
// make it talk
if ($results = $func->invoke()) {
foreach ($results as $path => $item) {
if (isset($item['file'])) {
$row = array();
$row[] = $path;
// write down its address...
$row[] = "Line " . $func->getStartLine() . " in " . $func->getFileName();
if (isset($item['page callback']) && function_exists($item['page callback'])) {
$row[] = $item['page callback'];
// most includes have a module path
$include_path = drupal_get_path('module', $hook);
// but a sneaky few use other modules paths.....
if (isset($item['file path'])) {
$include_path = $item['file path'];
}
require_once($include_path . '/' . $item['file']);
// now lets explore this function...
$callback = new ReflectionFunction($item['page callback']);
// same crap
$row[] = "Line " . $callback->getStartLine() . " in " . $callback->getFileName();
$rows[] = $row;
}
}
}
}
}
return theme('table', array('header' => $header, 'rows' => $rows,));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment