Skip to content

Commit

Permalink
ContainerFactory: rewritten caching mechanism to not use fopen [Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 16, 2014
1 parent 35d925b commit ab02d38
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/DI/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,41 +107,41 @@ protected function generateConfig()
private function loadClass()
{
$key = md5(serialize(array($this->config, $this->configFiles, $this->class, $this->parentClass)));
$handle = fopen($file = "$this->tempDirectory/$key.php", 'c+');
$file = "$this->tempDirectory/$key.php";

if (!$this->autoRebuild && (@include $file) !== FALSE) { // @ - file may not exist
return;
}

$handle = fopen("$file.tmp", 'c+');
if (!$handle) {
throw new Nette\IOException("Unable to open or create file '$file'.");
throw new Nette\IOException("Unable to open or create file '$file.tmp'.");
}

flock($handle, LOCK_SH);
$stat = fstat($handle);
if ($stat['size']) {
if ($this->autoRebuild) {
foreach ((array) @unserialize(file_get_contents($file . '.meta')) as $f => $time) { // @ - file may not exist
if (@filemtime($f) !== $time) { // @ - stat may fail
goto write;
}
if ($this->autoRebuild) {
flock($handle, LOCK_SH);
foreach ((array) @unserialize(file_get_contents("$file.meta")) as $f => $time) { // @ - file may not exist
if (@filemtime($f) !== $time) { // @ - stat may fail
@unlink($file); // @ - file may not exist
break;
}
}
} else {
write:
ftruncate($handle, 0);
}

if (!is_file($file)) {
flock($handle, LOCK_EX);
$stat = fstat($handle);
if (!$stat['size']) {
if (!is_file($file)) {
$this->dependencies = array();
$code = $this->generateCode();
if (fwrite($handle, $code, strlen($code)) !== strlen($code)) {
ftruncate($handle, 0);
if (!file_put_contents($file, $code)) {
throw new Nette\IOException("Unable to write file '$file'.");
}

$tmp = array();
foreach ($this->dependencies as $f) {
$tmp[$f] = @filemtime($f); // @ - stat may fail
}
file_put_contents($file . '.meta', serialize($tmp));
file_put_contents("$file.meta", serialize($tmp));
}
flock($handle, LOCK_SH);
}

require $file;
Expand Down

0 comments on commit ab02d38

Please sign in to comment.