|
@ -1,7 +1,7 @@ |
|
|
<?php |
|
|
<?php |
|
|
/** |
|
|
/** |
|
|
* Generates array of classes for autoload |
|
|
* Generates array of classes for autoload |
|
|
* |
|
|
|
|
|
|
|
|
* |
|
|
* @copyright NetMonsters <team@netmonsters.ru> |
|
|
* @copyright NetMonsters <team@netmonsters.ru> |
|
|
* @link http://netmonsters.ru |
|
|
* @link http://netmonsters.ru |
|
|
* @package Majestic |
|
|
* @package Majestic |
|
@ -17,16 +17,19 @@ |
|
|
class AutoloadBuilder |
|
|
class AutoloadBuilder |
|
|
{ |
|
|
{ |
|
|
protected $autoload; |
|
|
protected $autoload; |
|
|
|
|
|
|
|
|
protected $dirs; |
|
|
protected $dirs; |
|
|
|
|
|
|
|
|
protected $handler; |
|
|
protected $handler; |
|
|
|
|
|
|
|
|
protected $regex = '/(class|interface) ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/'; |
|
|
protected $regex = '/(class|interface) ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct($autoload, $dirs = array()) |
|
|
public function __construct($autoload, $dirs = array()) |
|
|
{ |
|
|
{ |
|
|
$this->autoload = $autoload; |
|
|
$this->autoload = $autoload; |
|
|
$this->dirs = $dirs; |
|
|
|
|
|
|
|
|
$this->dirs = $dirs; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function build() |
|
|
public function build() |
|
|
{ |
|
|
{ |
|
|
$this->openAutoload(); |
|
|
$this->openAutoload(); |
|
@ -37,19 +40,19 @@ class AutoloadBuilder |
|
|
new RecursiveDirectoryIterator($dir), |
|
|
new RecursiveDirectoryIterator($dir), |
|
|
RecursiveIteratorIterator::LEAVES_ONLY |
|
|
RecursiveIteratorIterator::LEAVES_ONLY |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($iterator as $file) { |
|
|
foreach ($iterator as $file) { |
|
|
// skip non php files
|
|
|
// skip non php files
|
|
|
$e = explode('.', $file->getFileName()); |
|
|
$e = explode('.', $file->getFileName()); |
|
|
if ((end($e) !== 'php') || strstr($file->getFileName(), 'test')) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((end($e) !== 'php') || $this->rightSubstr($file->getFileName(), 8) == 'Test.php') { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
$content = file_get_contents($file->getRealPath()); |
|
|
$content = file_get_contents($file->getRealPath()); |
|
|
$matches = array(); |
|
|
$matches = array(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$relative_path = substr($file->getRealPath(), strlen(PATH)); |
|
|
$relative_path = substr($file->getRealPath(), strlen(PATH)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (preg_match_all($this->regex, $content, $matches, PREG_SET_ORDER)) { |
|
|
if (preg_match_all($this->regex, $content, $matches, PREG_SET_ORDER)) { |
|
|
foreach ($matches as $match) { |
|
|
foreach ($matches as $match) { |
|
|
if (array_key_exists($match[2], $classes)) { |
|
|
if (array_key_exists($match[2], $classes)) { |
|
@ -64,26 +67,32 @@ class AutoloadBuilder |
|
|
} |
|
|
} |
|
|
$this->closeAutoload(); |
|
|
$this->closeAutoload(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function openAutoload() |
|
|
protected function openAutoload() |
|
|
{ |
|
|
{ |
|
|
$this->write("<?php\n// This file is autogenerated by \n// " . __FILE__ . " script.\nreturn array(\n"); |
|
|
$this->write("<?php\n// This file is autogenerated by \n// " . __FILE__ . " script.\nreturn array(\n"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function closeAutoload() |
|
|
protected function closeAutoload() |
|
|
{ |
|
|
{ |
|
|
if ($this->write(');')) { |
|
|
if ($this->write(');')) { |
|
|
fclose($this->handler); |
|
|
fclose($this->handler); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function write($string) |
|
|
protected function write($string) |
|
|
{ |
|
|
{ |
|
|
if (! $this->handler) { |
|
|
|
|
|
if (! $this->handler = fopen($this->autoload, 'w')) { |
|
|
|
|
|
|
|
|
if (!$this->handler) { |
|
|
|
|
|
if (!$this->handler = fopen($this->autoload, 'w')) { |
|
|
trigger_error("{$this->autoload} is not writable", E_USER_ERROR); |
|
|
trigger_error("{$this->autoload} is not writable", E_USER_ERROR); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return (bool) fwrite($this->handler, $string); |
|
|
return (bool) fwrite($this->handler, $string); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected function rightSubstr($string, $nchars) |
|
|
|
|
|
{ |
|
|
|
|
|
$right = substr($string, strlen($string) - $nchars, $nchars); |
|
|
|
|
|
return $right; |
|
|
|
|
|
} |
|
|
} |
|
|
} |