Removes cache files from a given directory.

@param string $directory

@return bool
438848
private function deleteDirectoryTree($directory)
    {
        $filePaths = scandir($directory);

        foreach ($filePaths as $filePath) {
            if ($filePath == '.' || $filePath == '..') {
                continue;
            }

            $fullFilePath = $directory.'/'.$filePath;
            if (is_dir($fullFilePath)) {
                $result = $this->deleteDirectoryTree($fullFilePath);
                if ($result) {
                    $result = rmdir($fullFilePath);
                }
            } else {
                if (basename($fullFilePath) == '.keep') {
                    continue;
                }
                $result = unlink($fullFilePath);
            }

            if (!$result) {
                return false;
            }
        }

        return true;
    }
1200202
public static function removeDirectory($dir = '')
    {
        if ($dir == '' || $dir == NULL || $dir == '/')
            return false;

        $files = array_diff(scandir($dir), array('.','..')); 
        foreach ($files as $file)
            (is_dir("$dir/$file")) ? CiiFileDeleter::removeDirectory("$dir/$file") : unlink("$dir/$file"); 

        return rmdir($dir); 
    }
549013
private function deleteCacheSubfolderRecursively($dir)
    {
        if (is_dir($dir)) {
            $objects = scandir($dir);
            foreach ($objects as $object) {
                if ($dir . DIRECTORY_SEPARATOR . $object == $this->cache_path) {
                    return false;
                } elseif ($object != '.' && $object != '..') {
                    if (is_writable($dir . DIRECTORY_SEPARATOR . $object)) {
                        if (is_dir($dir . DIRECTORY_SEPARATOR . $object)) {
                            $this->deleteCacheSubfolderRecursively($dir . DIRECTORY_SEPARATOR . $object);
                        } else {
                            unlink($dir . DIRECTORY_SEPARATOR . $object);
                        }
                    } else {
                        return false;
                    }
                }
            }

            if ($dir !== $this->cache_path) {
                rmdir($dir);
            }
        }
    }
974946
public function emptyDir($directory)
    {
        [$fs, $directory] = $this->getFilesystemAndPath($directory);

        $fs->emptyDir($directory);
    }
57290
protected function purgeDir($directory)
	{
		if( ! $this->filesystem->isDirectory($directory))
			return true;

		if($this->filesystem->isWritable($directory))
			return $this->filesystem->cleanDirectory($directory);

		$this->error($directory . ' is not writable');

		return false;
	}
959637
private function createDirectory($directory)
    {
        if (!file_exists($directory)) {
            // Let's create the directory:
            $old = umask(0);
            $result = @mkdir($directory, 0775, true);
            umask($old);

            return $result;
        }

        return true;
    }
16382
protected function purgeDir($directory)
	{
		if( ! File::isDirectory($directory))
			return true;

		if(File::isWritable($directory))
			return File::cleanDirectory($directory);

		$this->error($directory . ' is not writable');

		return false;
	}
1298209
public static function removeCacheDirectory($dir)
    {
        if (is_dir($dir)) {
            $objects = scandir($dir);

            if ($objects === false) {
                throw new \UnexpectedValueException('Not expecting false here');
            }

            foreach ($objects as $object) {
                if ($object != '.' && $object != '..') {
                    if (filetype($dir . '/' . $object) == 'dir') {
                        self::removeCacheDirectory($dir . '/' . $object);
                    } else {
                        unlink($dir . '/' . $object);
                    }
                }
            }

            reset($objects);
            rmdir($dir);
        }
    }
854839
public static function delTree($directory)
    {

        $files = array_diff(scandir($directory), array('.','..'));
		foreach ($files as $file) {
			$abspath = $directory.DIRECTORY_SEPARATOR.$file;
			if (is_dir($abspath)) {
				self::delTree($abspath);
			} else {
				unlink($abspath);
			}
		}
		return rmdir($directory);

    }
646580
private function rmdir($dir)
    {
        // remove all files
        $files = scandir($dir);
        foreach ($files as $file) {
            if ($file == '.' || $file == '..') {
                continue;
            }

            $path = $dir . '/' . $file;
            if (is_dir($path)) {
                $this->rmdir($path);
            } elseif (is_file($path)) {
                unlink($path);
            }
        }

        rmdir($dir);
    }
626230
public function clear()
    {
        $cachePath   = $this->options['path'];
        $cachePrefix = $this->options['prefix'] ? $this->options['prefix'].DIRECTORY_SEPARATOR : '';
        $files       = (array) glob($cachePath.$cachePrefix.'*');

        foreach ($files as $file) {
            if (is_dir($file)) {
                $matches = glob($file.'/*.php');
                if (is_array($matches)) {
                    array_map('unlink', $matches);
                }
                rmdir($file);
            } else {
                $this->unlink($file);
            }
        }

        return true;
    }
956818
public static function deleteDirectoryContents($directory, $DELETE_DIRECTORY = false)
    {
        $files = array_diff(scandir($directory), ['.', '..']);

        foreach ($files as $file) {
            $path = self::getPath([$directory, $file]);
            (
                is_dir($path)
                ? self::deleteDirectoryContents($path, true)
                : unlink($path)
            );
        }

        return $DELETE_DIRECTORY ? rmdir($directory) : true;
    }
590969
public function getDir($directory)
    {
        $directory = str_replace('/', DS, $directory);
        if (strpos($directory, $this->getBaseDir()) === false || !file_exists($directory)) {
            $directory = $this->getBaseDir() . DS . ltrim($directory, DS);
        }
        return $directory;
    }
634814
public function delTree($dir)
    {
        if (! file_exists($dir)) {
            return false;
        }

        $files = array_diff(scandir($dir), ['.', '..']);

        $this->deleteAllFiles($dir, $files);

        return rmdir($dir);
    }
228374
public function removeCache()
	{
		if ( ! $this->useCache || ! $this->cacheLocation ) return false;

		if ( ! is_dir( $this->cacheLocation ) ) return false;

		$rmrf = function ($dir) use ( &$rmrf )
		{
		    foreach ( glob( $dir ) as $file )
		    {
		        if ( is_dir( $file ) )
		        {
		            $rmrf( "$file/*" );
		            rmdir( $file );
		        }
		        else
		        {
		            unlink( $file );
		        }
		    }
		};

		$rmrf( $this->cacheLocation );

		return true;
	}
1000051
public function flush($dir = '')
    {
        $dir = $cache_dir = APP_PATH . '/' . Config::get('cache_dir');
        \ePHP\Misc\Dir::deleteDir($dir);
        mkdir($dir, 0777);
    }
200016
protected function clearDir($dir)
    {
        $iterator = new \DirectoryIterator($dir);
        foreach ($iterator as $file) {

            if ($file->isDir() && !$file->isDot() && !preg_match("~\.~", $file)) {
                $this->clearDir($file->getPathname());
                if (!\rmdir($file->getPathname())) {
                    return false;
                }
            } elseif ($file->isFile()) {
                if (!\unlink($file->getPathname())) {
                    return false;
                }
            }
        }

        return true;
    }