Code refactoring, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@114 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
94
util/AutoloadBuilder.php
Normal file
94
util/AutoloadBuilder.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* Generates array of classes for autoload
|
||||
*
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @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("<?php\n// This file is autogenerated by \n// " . __FILE__ . " script.\nreturn array(\n");
|
||||
}
|
||||
|
||||
protected function closeAutoload()
|
||||
{
|
||||
if ($this->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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user