Skip to content

Commit 077ab41

Browse files
committed
FileStorage: always creates directories (#47)
1 parent 12200db commit 077ab41

File tree

2 files changed

+5
-35
lines changed

2 files changed

+5
-35
lines changed

src/Bridges/CacheDI/CacheExtension.php

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function __construct($tempDir)
2929

3030
public function loadConfiguration()
3131
{
32+
@mkdir($this->tempDir . '/cache'); // @ - directory may exists
33+
3234
$builder = $this->getContainerBuilder();
3335

3436
$builder->addDefinition($this->prefix('journal'))
@@ -45,32 +47,4 @@ public function loadConfiguration()
4547
}
4648
}
4749

48-
49-
public function afterCompile(Nette\PhpGenerator\ClassType $class)
50-
{
51-
if (!$this->checkTempDir($this->tempDir . '/cache')) {
52-
$class->getMethod('initialize')->addBody('Nette\Caching\Storages\FileStorage::$useDirectories = FALSE;');
53-
}
54-
}
55-
56-
57-
private function checkTempDir($dir): bool
58-
{
59-
@mkdir($dir); // @ - directory may exists
60-
61-
// checks whether directory is writable
62-
$uniq = uniqid('_', TRUE);
63-
if (!@mkdir("$dir/$uniq")) { // @ - is escalated to exception
64-
throw new Nette\InvalidStateException("Unable to write to directory '$dir'. Make this directory writable.");
65-
}
66-
67-
// checks whether subdirectory is writable
68-
$isWritable = @file_put_contents("$dir/$uniq/_", '') !== FALSE; // @ - error is expected
69-
if ($isWritable) {
70-
unlink("$dir/$uniq/_");
71-
}
72-
rmdir("$dir/$uniq");
73-
return $isWritable;
74-
}
75-
7650
}

src/Caching/Storages/FileStorage.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,12 @@ class FileStorage implements Nette\Caching\IStorage
5151
/** @var float probability that the clean() routine is started */
5252
public static $gcProbability = 0.001;
5353

54-
/** @var bool */
54+
/** @deprecated */
5555
public static $useDirectories = TRUE;
5656

5757
/** @var string */
5858
private $dir;
5959

60-
/** @var bool */
61-
private $useDirs;
62-
6360
/** @var IJournal */
6461
private $journal;
6562

@@ -74,7 +71,6 @@ public function __construct($dir, IJournal $journal = NULL)
7471
}
7572

7673
$this->dir = $dir;
77-
$this->useDirs = (bool) static::$useDirectories;
7874
$this->journal = $journal;
7975

8076
if (mt_rand() / mt_getrandmax() < static::$gcProbability) {
@@ -143,7 +139,7 @@ private function verify(array $meta): bool
143139
public function lock(string $key): void
144140
{
145141
$cacheFile = $this->getCacheFile($key);
146-
if ($this->useDirs && !is_dir($dir = dirname($cacheFile))) {
142+
if (!is_dir($dir = dirname($cacheFile))) {
147143
@mkdir($dir); // @ - directory may already exist
148144
}
149145
$handle = fopen($cacheFile, 'c+b');
@@ -353,7 +349,7 @@ protected function readData(array $meta)
353349
protected function getCacheFile(string $key): string
354350
{
355351
$file = urlencode($key);
356-
if ($this->useDirs && $a = strrpos($file, '%00')) { // %00 = urlencode(Nette\Caching\Cache::NAMESPACE_SEPARATOR)
352+
if ($a = strrpos($file, '%00')) { // %00 = urlencode(Nette\Caching\Cache::NAMESPACE_SEPARATOR)
357353
$file = substr_replace($file, '/_', $a, 3);
358354
}
359355
return $this->dir . '/_' . $file;

0 commit comments

Comments
 (0)