Permalink
Browse files

Use SPL recursive iterators instead of invention our own.

  • Loading branch information...
1 parent b8134b8 commit 5cd523ee0cbc818260858cbd466b8fd6f559be01 @gauthierm gauthierm committed May 10, 2011
Showing with 25 additions and 46 deletions.
  1. +25 −46 src/PEAR2/Cache/Lite/Main.php
@@ -510,23 +510,25 @@ public function removeGroup($group)
$motif = ($group) ? 'cache_' . $group . '_' : 'cache_';
}
- $unlink = $this->unlink;
+ $iterator = new RecursiveIteratorIterator(
+ new RecursiveDirectoryIterator($this->cacheDir),
+ RecursiveIteratorIterator::CHILD_FIRST
+ );
- $this->applyDir(
- function($filePath) use ($motif, $unlink)
- {
- $fileName = basename($filePath);
+ foreach ($iterator as $path) {
+ if ($path->isFile()) {
+ $fileName = $path->getFilename();
if (strncmp($fileName, $motif, strlen($motif)) === 0) {
- $unlink($filePath);
+ $this->unlink($path->getPathname());
}
- },
- $this->cacheDir
- );
+ }
+ }
return $this;
}
// }}}
+ // {{{ cleanOld()
/**
* Cleans up old files in the cache directory
@@ -540,23 +542,25 @@ function($filePath) use ($motif, $unlink)
*/
public function cleanOld()
{
- $lifeTime = $this->lifeTime;
- $unlink = $this->unlink;
- $this->applyDir(
- function($filePath) use ($lifeTime, $unlink)
- {
- if ( $lifeTime > 0
- && time() - filemtime($filePath) > $lifeTime
- ) {
- $unlink($filePath);
- }
- },
- $this->cacheDir
+ $iterator = new RecursiveIteratorIterator(
+ new RecursiveDirectoryIterator($this->cacheDir),
+ RecursiveIteratorIterator::CHILD_FIRST
);
+ foreach ($iterator as $path) {
+ if ( $path->isFile()
+ && $lifeTime > 0
+ && time() - $path->getMTime() > $this->lifeTime
+ ) {
+ $this->unlink($path->getPathname());
+ }
+ }
+
return $this;
}
+ // }}}
+
public function extendLife($id, $group = 'default')
{
$filePath = $this->getFilePath($id, $group);
@@ -715,31 +719,6 @@ protected function writeAndControl($filePath, $data)
return ($dataRead === $data);
}
- protected function applyDir($apply, $dir, $group = null)
- {
- if (!($dh = opendir($dir))) {
- throw new FileException(
- 'Unable to open directory \'' . $dir . '\'.',
- 0,
- $dir
- );
- }
-
- while ($file = readdir($dh)) {
- if ( $file != '.'
- && $file != '..'
- && substr($file, 0, 6) == 'cache_'
- ) {
- $filePath = $dir . $file;
- if (is_file($filePath)) {
- $apply($group, $filePath);
- } elseif (is_dir($filePath) && $this->nestedDirectoryLevel > 0) {
- $this->applyDir($apply, $filePath . '/', $group);
- }
- }
- }
- }
-
// {{{ hash()
/**

0 comments on commit 5cd523e

Please sign in to comment.