<?php
/**
* ClassFinder
*
* This is a part of Tracker package
*
* @author Karel Klima aka Charlie
* @link http://charlie.cz
* @copyright Copyright (c) 2007 Karel Klima
* @package Tracker
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @version 1.0 (January 12, 2007)
*/
/**
* Searches files for PHP classes
*
* Usage:
* $finder = new ClassFinder();
* $classes = $finder->findInFile($filename);
*/
class ClassFinder {
/** @var array list of classes found */
public $classes = array();
/** @var bool helps with searching for class tokens */
private $next = false;
/**
* Looks for class names in a PHP code
*
* @param string PHP code
*/
public function parse($code) {
// "divides" PHP code into tokens
$tokens = token_get_all($code);
foreach ($tokens as $token) {
if (!isset($token[0])) continue;
// next token after this one is our desired class name
if ($token[0] == T_CLASS) {
$this->next = true;
}
// gotcha! :)
if ($token[0] == T_STRING and $this->next === true) {
// adds class to buffer
$this->classes[] = $token[1];
$this->next = false;
}
}
}
/**
* Enter description here...
*
* @param string $file
* @return array found classes
*/
function findInFile($file) {
// locates classes in requested file
$this->parse(file_get_contents($file));
return $this->classes;
}
}
?>