modified AutoloadBuilder for atomic autoload.php write
This commit is contained in:
@ -75,7 +75,7 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
public function testBuild()
|
public function testBuild()
|
||||||
{
|
{
|
||||||
$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_keys(self::$inc_dirs));
|
||||||
|
|
||||||
$this->assertFileNotExists(self::$file);
|
$this->assertFileNotExists(self::$file);
|
||||||
$builder->build();
|
$builder->build();
|
||||||
@ -92,18 +92,38 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @runInSeparateProcess
|
* @runInSeparateProcess
|
||||||
*/
|
*/
|
||||||
|
public function testBuildWithExcluded()
|
||||||
|
{
|
||||||
|
$this->setConstants();
|
||||||
|
$builder = new AutoloadBuilder(self::$file, array_keys(self::$inc_dirs), array(self::$path . '/lib/core/app/'));
|
||||||
|
|
||||||
|
$this->assertFileNotExists(self::$file);
|
||||||
|
$builder->build();
|
||||||
|
|
||||||
|
$this->assertFileExists(self::$file);
|
||||||
|
|
||||||
|
$array = require self::$file;
|
||||||
|
$this->assertInternalType('array', $array);
|
||||||
|
$this->assertNotEmpty($array);
|
||||||
|
$this->assertArrayHasKey('AutoloadBuilder', $array);
|
||||||
|
$this->assertArrayNotHasKey('FrontController', $array);
|
||||||
|
$this->assertArrayNotHasKey('AjaxAction', $array);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
public function testAccessDenied()
|
public function testAccessDenied()
|
||||||
{
|
{
|
||||||
$this->setConstants();
|
$this->setConstants();
|
||||||
|
|
||||||
$this->setExpectedException('PHPUnit_Framework_Error');
|
|
||||||
$this->assertFileNotExists(self::$file);
|
$this->assertFileNotExists(self::$file);
|
||||||
|
|
||||||
touch(self::$file);
|
$path = dirname(self::$file);
|
||||||
$this->assertFileExists(self::$file);
|
chmod($path, 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'));
|
||||||
|
|
||||||
|
$this->setExpectedException('PHPUnit_Framework_Error', 'Permission denied');
|
||||||
$builder->build();
|
$builder->build();
|
||||||
chmod(self::$file, 0777);
|
chmod(self::$file, 0777);
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ class AutoloadBuilder
|
|||||||
|
|
||||||
foreach ($iterator as $file) {
|
foreach ($iterator as $file) {
|
||||||
|
|
||||||
if($this->isExcluded($file->getRealPath())) {
|
if ($this->isExcluded($file->getRealPath())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// skip non php files
|
// 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 .= ');';
|
$array_string .= ');';
|
||||||
$this->writeAutoload($array_string);
|
$this->writeAutoload($array_string);
|
||||||
$this->isFileConsistent();
|
if (!$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);
|
unlink($this->autoload);
|
||||||
trigger_error("Error while generating autoload file.", E_USER_ERROR);
|
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)
|
protected function isExcluded($file)
|
||||||
{
|
{
|
||||||
foreach ($this->exclude as $dir) {
|
foreach ($this->exclude as $dir) {
|
||||||
if (stripos($file, PATH . $dir) === 0) {
|
if (stripos($file, $dir) === 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user