Skip to content

Commit

Permalink
Improve code readbility.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Oct 18, 2018
1 parent 03da862 commit 084ab25
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/Cache/Engine/FileEngine.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Cache\CacheEngine;
use Cake\Utility\Inflector;
use CallbackFilterIterator;
use Exception;
use LogicException;
use RecursiveDirectoryIterator;
Expand Down Expand Up @@ -470,24 +471,40 @@ public function key($key)
public function clearGroup($group)
{
$this->_File = null;

$prefix = (string)$this->_config['prefix'];

$directoryIterator = new RecursiveDirectoryIterator($this->_config['path']);
$contents = new RecursiveIteratorIterator(
$directoryIterator,
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($contents as $object) {
$containsGroup = strpos($object->getPathname(), DIRECTORY_SEPARATOR . $group . DIRECTORY_SEPARATOR) !== false;
$hasPrefix = true;
if (strlen($this->_config['prefix']) !== 0) {
$hasPrefix = strpos($object->getBasename(), $this->_config['prefix']) === 0;
}
if ($object->isFile() && $containsGroup && $hasPrefix) {
$path = $object->getPathname();
$object = null;
//@codingStandardsIgnoreStart
@unlink($path);
//@codingStandardsIgnoreEnd
$filtered = new CallbackFilterIterator(
$contents,
function ($current) use ($group, $prefix) {
if (!$current->isFile()) {
return false;
}

$hasPrefix = $prefix === ''
|| strpos($current->getBasename(), $prefix) === 0;
if ($hasPrefix === false) {
return false;
}

$pos = strpos(
$current->getPathname(),
DIRECTORY_SEPARATOR . $group . DIRECTORY_SEPARATOR
);

return $pos !== false;
}
);
foreach ($filtered as $object) {
$path = $object->getPathname();
$object = null;
// @codingStandardsIgnoreLine
@unlink($path);
}

return true;
Expand Down

0 comments on commit 084ab25

Please sign in to comment.