modified AutoloadBuilder for atomic autoload.php write

This commit is contained in:
Anton Grebnev
2012-06-04 16:04:17 +04:00
parent 29a255776d
commit 359c712962
2 changed files with 46 additions and 19 deletions

View File

@ -46,7 +46,7 @@ class AutoloadBuilder
foreach ($iterator as $file) {
if($this->isExcluded($file->getRealPath())) {
if ($this->isExcluded($file->getRealPath())) {
continue;
}
// skip non php files
@ -70,30 +70,37 @@ class AutoloadBuilder
}
}
}
unset($file); // without this line trigger_error throws 'Serialization of 'SplFileInfo' is not allowed' exception under PHPUnit
}
$array_string .= ');';
$this->writeAutoload($array_string);
$this->isFileConsistent();
}
protected function writeAutoload($string)
{
file_put_contents($this->autoload, $string);
}
protected function isFileConsistent()
{
$autoload_array = include($this->autoload);
if(!is_array($autoload_array)) {
if (!$this->isFileConsistent()) {
unlink($this->autoload);
trigger_error("Error while generating autoload file.", E_USER_ERROR);
}
}
protected function writeAutoload($string)
{
$pid = getmypid();
$tmp = dirname($this->autoload) . '/' . md5(time() + $pid);
file_put_contents($tmp, $string);
rename($tmp, $this->autoload);
}
protected function isFileConsistent()
{
$autoload_array = include($this->autoload);
if (!is_array($autoload_array)) {
return false;
}
return true;
}
protected function isExcluded($file)
{
foreach ($this->exclude as $dir) {
if (stripos($file, PATH . $dir) === 0) {
if (stripos($file, $dir) === 0) {
return true;
}
}