Skip to content

Commit 1864d6e

Browse files
committed
ContainerFactory: rewritten caching mechanism to not use fopen [Closes #27]
1 parent 35d925b commit 1864d6e

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/DI/ContainerFactory.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,41 +107,41 @@ protected function generateConfig()
107107
private function loadClass()
108108
{
109109
$key = md5(serialize(array($this->config, $this->configFiles, $this->class, $this->parentClass)));
110-
$handle = fopen($file = "$this->tempDirectory/$key.php", 'c+');
110+
$file = "$this->tempDirectory/$key.php";
111+
112+
if (!$this->autoRebuild && (@include $file) !== FALSE) { // @ - file may not exist
113+
return;
114+
}
115+
116+
$handle = fopen("$file.tmp", 'c+');
111117
if (!$handle) {
112-
throw new Nette\IOException("Unable to open or create file '$file'.");
118+
throw new Nette\IOException("Unable to open or create file '$file.tmp'.");
113119
}
114120

115-
flock($handle, LOCK_SH);
116-
$stat = fstat($handle);
117-
if ($stat['size']) {
118-
if ($this->autoRebuild) {
119-
foreach ((array) @unserialize(file_get_contents($file . '.meta')) as $f => $time) { // @ - file may not exist
120-
if (@filemtime($f) !== $time) { // @ - stat may fail
121-
goto write;
122-
}
121+
if ($this->autoRebuild) {
122+
flock($handle, LOCK_SH);
123+
foreach ((array) @unserialize(file_get_contents("$file.meta")) as $f => $time) { // @ - file may not exist
124+
if (@filemtime($f) !== $time) { // @ - stat may fail
125+
unlink($file);
126+
break;
123127
}
124128
}
125-
} else {
126-
write:
127-
ftruncate($handle, 0);
129+
}
130+
131+
if (!is_file($file)) {
128132
flock($handle, LOCK_EX);
129-
$stat = fstat($handle);
130-
if (!$stat['size']) {
133+
if (!is_file($file)) {
131134
$this->dependencies = array();
132135
$code = $this->generateCode();
133-
if (fwrite($handle, $code, strlen($code)) !== strlen($code)) {
134-
ftruncate($handle, 0);
136+
if (!file_put_contents($file, $code)) {
135137
throw new Nette\IOException("Unable to write file '$file'.");
136138
}
137-
138139
$tmp = array();
139140
foreach ($this->dependencies as $f) {
140141
$tmp[$f] = @filemtime($f); // @ - stat may fail
141142
}
142-
file_put_contents($file . '.meta', serialize($tmp));
143+
file_put_contents("$file.meta", serialize($tmp));
143144
}
144-
flock($handle, LOCK_SH);
145145
}
146146

147147
require $file;

0 commit comments

Comments
 (0)