return static::$instances[$recursive][$directory];
}
- /**
- * Executes a callback on each file
- *
- * @param callback $callback Valid callback
- * @param string $pattern Apply callback only to files matching the given pattern
- * @return boolean Returns false if callback is missing or no files available
- */
- public function executeCallback($callback, $pattern = '') {
- if (!is_callable($callback) || empty($this->files)) return false;
-
- $files = $this->getFiles();
- // check for pattern only once -> faster
- if (empty($pattern)) {
- foreach ($files as $filename) {
- call_user_func($callback, $filename);
- }
- }
- else {
- foreach ($files as $filename) {
- if (!preg_match($pattern, $filename)) continue;
-
- call_user_func($callback, $filename);
- }
- }
-
- return true;
- }
-
/**
* Returns a sorted list of files
*
$this->files[$obj->getFilename()] = $obj->getFilename();
}
}
+
+ // add the directory itself
+ $this->filesObj[$this->directory] = $this->directory;
}
/**
$this->filesObj[$obj->getFilename()] = $obj;
}
}
- }
+ // add the directory itself
+ $this->filesObj[$this->directory] = new \SPLFileInfo($this->directory);
+ }
+
/**
- * Recursive remove of directory
+ * Executes a callback on each file
+ *
+ * @param callback $callback Valid callback
+ * @param string $pattern Apply callback only to files matching the given pattern
+ * @return boolean Returns false if callback is invalid
*/
- public function removeAll() {
- if (!$this->recursive) throw new SystemException('Removing of directory only works in recursive mode');
+ public function executeCallback($callback, $pattern = '') {
+ if (!is_callable($callback)) return false;
- $files = $this->getFilesObj(self::SORT_NONE);
+ $files = $this->getFilesObj(self::SORT_NONE, $pattern);
foreach ($files as $filename => $obj) {
- if (!is_writable($obj->getPath())) {
- throw new SystemException("Could not remove directory: '".$obj->getPath()."' is not writable");
- }
-
- if ($obj->isDir()) {
- rmdir($filename);
- }
- else if ($obj->isFile()) {
- unlink($filename);
- }
+ call_user_func($callback, $filename, $obj);
}
- rmdir($this->directory);
- // clear cache
- $this->filesObj = array();
- $this->scanFilesObj();
-
- $this->files = array();
- $this->scanFiles();
+ return true;
+ }
+
+ /**
+ * Recursive remove of directory
+ */
+ public function removeAll() {
+ $this->removePattern('');
// destroy cached instance
unset(static::$instances[$this->recursive][$this->directory]);
public function removePattern($pattern) {
if (!$this->recursive) throw new SystemException('Removing of files only works in recursive mode');
- $files = $this->getFilesObj(self::SORT_NONE);
- foreach ($files as $filename => $obj) {
- if (!preg_match($pattern, $filename)) continue;
+ $files = $this->getFilesObj(self::SORT_NONE, $pattern);
+ foreach ($files as $filename => $obj) {
if (!is_writable($obj->getPath())) {
throw new SystemException("Could not remove directory: '".$obj->getPath()."' is not writable");
}