* @link http://netmonsters.ru * @package Majestic * @subpackage util * @since 2010-02-24 * @version SVN: $Id$ * @filesource $URL$ */ class AutoloadBuilder { protected $autoload; protected $dirs; protected $handler; protected $regex = '/(class|interface) ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/'; public function __construct($autoload, $dirs = array()) { $this->autoload = $autoload; $this->dirs = $dirs; } public function build() { $this->openAutoload(); echo "parsing started...\n"; // for dublicates check $classes = array(); foreach ($this->dirs as $dir) { $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY ); foreach ($iterator as $file) { // skip non php files $e = explode('.', $file->getFileName()); if ((end($e) !== 'php') || strstr((string)$file, 'test')) { continue; } $content = file_get_contents($file->getRealPath()); $matches = array(); $relative_path = substr($file->getRealPath(), strlen(PATH)); if (preg_match_all($this->regex, $content, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { echo "found {$match[0]}..."; if (array_key_exists($match[2], $classes)) { echo " FAULT\n{$match[0]} also found in ". $file->getRealPath(). ", skipped.\n"; continue; } echo " OK\n"; $string = "'{$match[2]}'=>'" . $relative_path . "',\n"; $this->write($string); $classes[$match[2]] = $file->getRealPath(); } } } } echo "done.\n"; $this->closeAutoload(); } protected function openAutoload() { $this->write("write(");\n")) { fclose($this->handler); } } protected function write($string) { if (! $this->handler) { if (! $this->handler = fopen($this->autoload, 'w')) { echo "{$this->autoload} is not writable\n"; die; } } return (bool) fwrite($this->handler, $string); } }