Skip to content

Commit 365d8a0

Browse files
committed
add handle is_really_writable() early in __construct
1 parent 1fe3de0 commit 365d8a0

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

system/Cache/Exceptions/CacheException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
class CacheException extends \RuntimeException implements ExceptionInterface
44
{
5+
/**
6+
* @return \CodeIgniter\Cache\Exceptions\CacheException
7+
*/
8+
public static function forUnableToWrite(string $path)
9+
{
10+
return new static(lang('Cache.unableToWrite', [$path]));
11+
}
12+
513
/**
614
* @return \CodeIgniter\Cache\Exceptions\CacheException
715
*/

system/Cache/Handlers/FileHandler.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*/
3838

3939
use CodeIgniter\Cache\CacheInterface;
40+
use CodeIgniter\Cache\Exceptions\CacheException;
4041

4142
class FileHandler implements CacheInterface
4243
{
@@ -59,10 +60,14 @@ class FileHandler implements CacheInterface
5960

6061
public function __construct($config)
6162
{
62-
$this->prefix = $config->prefix ?: '';
63-
$this->path = ! empty($config->storePath) ? $config->storePath : WRITEPATH . 'cache';
63+
$path = ! empty($config->storePath) ? $config->storePath : WRITEPATH . 'cache';
64+
if (! is_really_writable($path))
65+
{
66+
throw CacheException::forUnableToWrite($path);
67+
}
6468

65-
$this->path = rtrim($this->path, '/') . '/';
69+
$this->prefix = $config->prefix ?: '';
70+
$this->path = rtrim($path, '/') . '/';
6671
}
6772

6873
//--------------------------------------------------------------------
@@ -329,11 +334,7 @@ protected function getItem(string $key)
329334
*/
330335
protected function writeFile($path, $data, $mode = 'wb')
331336
{
332-
if (($fp = @fopen($path, $mode)) === false)
333-
{
334-
return false;
335-
}
336-
337+
fopen($path, $mode);
337338
flock($fp, LOCK_EX);
338339

339340
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)

system/Language/en/Cache.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
return [
18+
'unableToWrite' => 'Cache unable to write to {0}',
1819
'invalidHandlers' => 'Cache config must have an array of $validHandlers.',
1920
'noBackup' => 'Cache config must have a handler and backupHandler set.',
2021
'handlerNotFound' => 'Cache config has an invalid handler or backup handler specified.',

0 commit comments

Comments
 (0)