Skip to content

Commit

Permalink
Merge pull request #1525 from samsonasik/rem-unneeded-try-catch-fopen
Browse files Browse the repository at this point in the history
remove unneeded try {} catch {} on @fopen at Cache FileHandler::writeFile()
  • Loading branch information
lonnieezell authored Nov 30, 2018
2 parents db77587 + 57afc99 commit ef8619d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
8 changes: 8 additions & 0 deletions system/Cache/Exceptions/CacheException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

class CacheException extends \RuntimeException implements ExceptionInterface
{
/**
* @return \CodeIgniter\Cache\Exceptions\CacheException
*/
public static function forUnableToWrite(string $path)
{
return new static(lang('Cache.unableToWrite', [$path]));
}

/**
* @return \CodeIgniter\Cache\Exceptions\CacheException
*/
Expand Down
20 changes: 9 additions & 11 deletions system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/

use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Cache\Exceptions\CacheException;

class FileHandler implements CacheInterface
{
Expand All @@ -59,10 +60,14 @@ class FileHandler implements CacheInterface

public function __construct($config)
{
$this->prefix = $config->prefix ?: '';
$this->path = ! empty($config->storePath) ? $config->storePath : WRITEPATH . 'cache';
$path = ! empty($config->storePath) ? $config->storePath : WRITEPATH . 'cache';
if (! is_really_writable($path))
{
throw CacheException::forUnableToWrite($path);
}

$this->path = rtrim($this->path, '/') . '/';
$this->prefix = $config->prefix ?: '';
$this->path = rtrim($path, '/') . '/';
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -329,14 +334,7 @@ protected function getItem(string $key)
*/
protected function writeFile($path, $data, $mode = 'wb')
{
try
{
if (($fp = @fopen($path, $mode)) === false)
{
return false;
}
}
catch (\ErrorException $e)
if (($fp = @fopen($path, $mode)) === false)
{
return false;
}
Expand Down
1 change: 1 addition & 0 deletions system/Language/en/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

return [
'unableToWrite' => 'Cache unable to write to {0}',
'invalidHandlers' => 'Cache config must have an array of $validHandlers.',
'noBackup' => 'Cache config must have a handler and backupHandler set.',
'handlerNotFound' => 'Cache config has an invalid handler or backup handler specified.',
Expand Down
9 changes: 9 additions & 0 deletions tests/system/Cache/Handlers/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public function testNew()
$this->assertInstanceOf(FileHandler::class, $this->fileHandler);
}

/**
* @expectedException \CodeIgniter\Cache\Exceptions\CacheException
*/
public function testNewWithNonWritablePath()
{
chmod($this->config->storePath, 0444);
new FileHandler($this->config);
}

public function testSetDefaultPath()
{
//Initialize path
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/libraries/caching.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ File-based Caching
Unlike caching from the Output Class, the driver file-based caching
allows for pieces of view files to be cached. Use this with care, and
make sure to benchmark your application, as a point can come where disk
I/O will negate positive gains by caching.
I/O will negate positive gains by caching. This require a writable cache directory to be really writable (0777).

=================
Memcached Caching
Expand Down

0 comments on commit ef8619d

Please sign in to comment.