modified AutoloadBuilder to atomic write operation
This commit is contained in:
@ -77,6 +77,7 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->setConstants();
|
$this->setConstants();
|
||||||
$builder = new AutoloadBuilder(self::$file, array(self::$path . '/' . self::$app . '/src', self::$path . '/' . self::$app . '/cache', self::$path . '/lib'));
|
$builder = new AutoloadBuilder(self::$file, array(self::$path . '/' . self::$app . '/src', self::$path . '/' . self::$app . '/cache', self::$path . '/lib'));
|
||||||
|
|
||||||
|
$this->assertFileNotExists(self::$file);
|
||||||
$builder->build();
|
$builder->build();
|
||||||
|
|
||||||
$this->assertFileExists(self::$file);
|
$this->assertFileExists(self::$file);
|
||||||
@ -84,6 +85,8 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
$array = require self::$file;
|
$array = require self::$file;
|
||||||
$this->assertInternalType('array', $array);
|
$this->assertInternalType('array', $array);
|
||||||
$this->assertNotEmpty($array);
|
$this->assertNotEmpty($array);
|
||||||
|
$this->assertArrayHasKey('AutoloadBuilder', $array);
|
||||||
|
$this->assertArrayHasKey('Load', $array);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,7 +97,10 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->setConstants();
|
$this->setConstants();
|
||||||
|
|
||||||
$this->setExpectedException('PHPUnit_Framework_Error');
|
$this->setExpectedException('PHPUnit_Framework_Error');
|
||||||
|
$this->assertFileNotExists(self::$file);
|
||||||
|
|
||||||
|
touch(self::$file);
|
||||||
|
$this->assertFileExists(self::$file);
|
||||||
chmod(self::$file, 0400);
|
chmod(self::$file, 0400);
|
||||||
$builder = new AutoloadBuilder(self::$file, array(self::$path . '/' . self::$app . '/src', self::$path . '/' . self::$app . '/cache', self::$path . '/lib'));
|
$builder = new AutoloadBuilder(self::$file, array(self::$path . '/' . self::$app . '/src', self::$path . '/' . self::$app . '/cache', self::$path . '/lib'));
|
||||||
|
|
||||||
@ -102,15 +108,6 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
chmod(self::$file, 0777);
|
chmod(self::$file, 0777);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tearDown()
|
|
||||||
{
|
|
||||||
// if (defined('PATH')) {
|
|
||||||
// echo PHP_EOL . __CLASS__ . ' ' . PATH . PHP_EOL;
|
|
||||||
// } else {
|
|
||||||
// echo PHP_EOL . __CLASS__ . ' ' . 'PATH NOT DEFINED' . PHP_EOL;
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function tearDownAfterClass()
|
public static function tearDownAfterClass()
|
||||||
{
|
{
|
||||||
if (file_exists(self::$file)) {
|
if (file_exists(self::$file)) {
|
||||||
|
@ -35,7 +35,7 @@ class AutoloadBuilder
|
|||||||
|
|
||||||
public function build()
|
public function build()
|
||||||
{
|
{
|
||||||
$this->openAutoload();
|
$array_string = "<?php\n// This file is autogenerated by \n// " . __FILE__ . " script.\nreturn array(\n";
|
||||||
// for dublicates check
|
// for dublicates check
|
||||||
$classes = array();
|
$classes = array();
|
||||||
foreach ($this->dirs as $dir) {
|
foreach ($this->dirs as $dir) {
|
||||||
@ -65,14 +65,29 @@ class AutoloadBuilder
|
|||||||
if (array_key_exists($match[2], $classes)) {
|
if (array_key_exists($match[2], $classes)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$string = "'{$match[2]}'=>'" . $relative_path . "',\n";
|
$array_string .= "'{$match[2]}'=>'" . $relative_path . "',\n";
|
||||||
$this->write($string);
|
|
||||||
$classes[$match[2]] = $file->getRealPath();
|
$classes[$match[2]] = $file->getRealPath();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->closeAutoload();
|
$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)) {
|
||||||
|
unlink($this->autoload);
|
||||||
|
trigger_error("Error while generating autoload file.", E_USER_ERROR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function isExcluded($file)
|
protected function isExcluded($file)
|
||||||
@ -85,29 +100,6 @@ class AutoloadBuilder
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected function openAutoload()
|
|
||||||
{
|
|
||||||
$this->write("<?php\n// This file is autogenerated by \n// " . __FILE__ . " script.\nreturn array(\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function closeAutoload()
|
|
||||||
{
|
|
||||||
if ($this->write(');')) {
|
|
||||||
fclose($this->handler);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function write($string)
|
|
||||||
{
|
|
||||||
if (!$this->handler) {
|
|
||||||
if (!$this->handler = fopen($this->autoload, 'w')) {
|
|
||||||
trigger_error("{$this->autoload} is not writable", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (bool) fwrite($this->handler, $string);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function rightSubstr($string, $nchars)
|
protected function rightSubstr($string, $nchars)
|
||||||
{
|
{
|
||||||
$right = substr($string, strlen($string) - $nchars, $nchars);
|
$right = substr($string, strlen($string) - $nchars, $nchars);
|
||||||
|
Reference in New Issue
Block a user