scan();
// output stats
$globals = $scanner->get_globals();
$global_count = count($globals);
echo $scanner->get_file_count() . ' searchable files found.
';
echo $global_count . ' globals found.
';
echo '
';
// save to file
$str = "\n" .
" * " . $global_count . " globals documented.\n" .
" */\n\n";
foreach ($globals as $global)
{
$str .= "/**\n * \n */\nglobal " . $global . ";\n\n";
}
$str .= "?>";
file_put_contents('globals.php', $str);
/**
* Scans directory recursively for variables accessed with the global keyword.
*/
class DirectoryScanner
{
private $root_directory;
private $file_count = 0;
private $globals = array();
public function DirectoryScanner($root_directory)
{
$this->root_directory = $root_directory;
}
public function scan()
{
$this->scanDirectory($this->root_directory);
}
private function scanDirectory($directory)
{
global $extensions, $ignore;
$path = $directory . DIRECOTORY_SEPORATOR;
$contents = scandir($path);
foreach ($contents as $item)
{
$pos = strripos($item, '.');
$extension = ($pos !== false ? substr($item, $pos + 1) : null);
if (!in_array($item, $ignore))
{
if ($extension !== null)
{
if (in_array($extension, $extensions))
{
// a file with one of the correct extensions
preg_match_all('/global[ ]([$].*?)[;]/', file_get_contents($path . $item), $matches, PREG_SET_ORDER);
if (!empty($matches))
{
foreach ($matches as $match)
{
$globals = explode(',', $match[1]);
foreach ($globals as $global)
{
$global = trim($global);
if (!in_array($global, $this->globals))
{
$this->globals[] = $global;
}
}
}
}
$this->file_count++;
}
}
else if (is_dir($path . $item))
{
// a directory that is not ignored
$this->scanDirectory($path . $item);
}
}
}
}
public function get_file_count()
{
return $this->file_count;
}
public function get_globals()
{
return $this->globals;
}
}
?>