Purge directory.

@param  string $directory
@return boolean
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;
	}
537313
static function delete_empty_dir($dir)
    {
        $dir = self::makeVirtualDir($dir, false);
        if (is_dir($dir) && sizeof(scandir($dir)) == 2){
            return @rmdir($dir);
        }
        self::deleteVirtualDir($dir);
        return false;
    }
264769
public static void purgeDirectory(File directory) {

        if (directory.canRead() && directory.isDirectory()) {
            File[] files = directory.listFiles();
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                if (f.isDirectory()) {
                    purgeDirectory(f);
                }
                if (f.canWrite()) {
                    f.delete();
                }
            }
            directory.delete();
        }
    }
1042112
protected function clearCacheDirectory($directory)
    {
        foreach ($this->filesToClear($directory) as $file) {
            if ($file->isDir()) {
                $this->filesystem->deleteDirectory($file->getPathname());
            } else {
                $this->filesystem->delete($file->getPathname());
            }
        }
    }
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);
    }
982684
public static function deleteDirectory($dir) {
		if (is_dir($dir))
			$dirHandle = opendir($dir);
		
		if (!$dirHandle)
			return false;
		
		while ($file = readdir($dirHandle)) {
			if ($file != "." && $file != "..") {
				if (!is_dir($dir."/".$file))
					unlink($dir."/".$file);
				else
					self::deleteDirectory($dir.'/'.$file);          
			}
		}
		
		closedir($dirHandle);
		rmdir($dir);
		return true;
	}
482144
public function clearIndex($dir)
    {
        $success = 0;

        if (!file_exists($dir))
        {
            $success = 1;
        }

        if (!is_dir($dir))
        {
            return @unlink($dir);
        }

        foreach (scandir($dir) as $item)
        {
            if ($item == '.' || $item == '..')
            {
                continue;
            }

            if (!$this->clearIndex($dir . DIRECTORY_SEPARATOR . $item))
            {
                $success = 0;
            }

        }

        $sucess = @rmdir($dir);

        return $sucess;
    }
635484
public function setDirectory($directory)
	{
		$this->directory = $directory;

		// Set the finder to use this directory as well
		$this->finder->in($directory);

		return $this;
	}
550337
public static function purge(string $dir): void
	{
		if (!is_dir($dir)) {
			mkdir($dir);
		}

		/** @var SplFileInfo $entry */
		foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $entry) {
			if ($entry->isDir()) {
				rmdir((string) $entry->getRealPath());
			} else {
				unlink((string) $entry->getRealPath());
			}
		}
	}
156387
static function delete($directory, $force = false) {
        if (self::exists($directory)) {
            /**
             * Check for files
             *
             */
            $contents = scandir($directory);

            if ($force) {
                foreach ($contents as $content) {
                    if ($content != '.' && $content != '..') {
                        if (self::exists($directory . '/' . $content)) {
                            self::delete($directory . '/' . $content, true);
                        } else {
                            self::delete($directory . '/' . $content);
                        }
                    }
                }

                rmdir($directory);
            } else {
                $contents = scandir($directory);

                rmdir($directory);
            }

            if (!self::exists($directory)) return true;
        }

        return false;
    }
1291799
public function copy($directory) {

		if(false === is_dir($directory)) {
			return trigger_error(sprintf('Directory "%s" passed to %s is not an existing directory', $directory, __METHOD__), E_USER_ERROR);
		}

		if(false === is_writable($directory)) {
			return trigger_error(sprintf('Directory "%s" passed to %s is not writable', $directory, __METHOD__), E_USER_ERROR);
		}

		if(false !== $this -> directory -> getExists()) {
			$this -> rcopy($this -> directory -> getPath(), $directory);
		}

		return $this;
	}
177565
private function purgeRecurse($dir)
    {
        $base = $dir . '/';
        $time = time();
        if (substr($dir, 0, strlen($this->dir)) !== $this->dir) {
            // uh oh, we somehow tried to escape from the cache directory
            assert(0);
            return;
        }
        foreach (scandir($dir) as $f) {
            $fn = $base . $f;

            if ($f[0] === '.') {
                continue;
            }

            if (is_dir($fn)) {
                $this->purgeRecurse($fn);
            } else {
                $update = filemtime($fn);
                if ($time - $update > $this->timeout) {
                    unlink($fn);
                }
            }
        }
    }
1271130
public function deleteDirectory( string $directory, $preserve = false )
	{
		if( !$this->isDirectory( $directory ) )
		{
			return false;
		}

		$items = new FilesystemIterator( $directory );
		foreach( $items as $item )
		{
			// If the item is a directory, we can just recurse into the function and
			// delete that sub-directory otherwise we'll just delete the file and
			// keep iterating through each file until the directory is cleaned.
			if( $item->isDir() && !$item->isLink() )
			{
				$this->deleteDirectory( $item->getPathname() );
			}

			// If the item is just a file, we can go ahead and delete it since we're
			// just looping through and waxing all of the files in this directory
			// and calling directories recursively, so we delete the real path.
			else
			{
				$this->deleteOnce( $item->getPathname() );
			}
		}

		if( ! $preserve )
		{
			@ rmdir( $directory );
		}

		return true;
	}
634077
static function purgeDir($dir) {
		if (!is_dir($dir)) {
			throw new \InvalidArgumentException("$dir is not directory.");
		}
		$content=glob($dir."/*");
		if ($content) {
			foreach($content as $sub) {
				if ($sub=="." or $sub=="..") continue;
				self::remove($sub);
			}
		}
		return true;
	}
1241148
public function purge()
    {
        if (file_exists($this->directory)) {
            $files          = $this->getCachedFiles();
            $successDeleted = true;
            foreach ($files as $file) {
                $successDeleted &= $this->removeCacheFile($file);
            }

            return (bool)$successDeleted;
        }

        return false;
    }
644272
public function delDir($dir)
    {
        $dir = $this->checkpath($dir);
        if (@!ftp_rmdir($this->linkid, $dir)) {
            $this->close();
            return false;
        }
        $this->close();
        return true;
    }
741376
public function deleteDirectories( string $directory )
	{
		try {
			$allDirectories = Iterator::createInstance( $directory )->getDirectories();
		}
		catch( Exceptions\InvalidArgumentPathException $e ) { return false; }

		if( $allDirectories->isNotEmpty() )
		{
			/** @var \SplFileInfo $directoryName */
			foreach( $allDirectories as $directoryName )
			{
				$this->deleteDirectory( $directoryName->getPathname() );
			}
			return true;
		}

		return false;
	}