Empty all directory content, but do not delete the directory

@param string $directory

@return void
@throws FilesystemException
329895
public static function emptyDirectory(string $directory): void
    {
        if (is_dir($directory)) {
            foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) {
                if ($path->isFile()) {
                    if (!unlink($path->getPathname())) {
                        throw new FilesystemException("Unable to empty directory while emptying directory on path={$path->getPathname()}");
                    }
                } else {
                    if (!rmdir($path->getPathname())) {
                        throw new FilesystemException("Unable to empty directory while emptying directory on path={$path->getPathname()}");
                    }
                }
            }
        }
    }
1179016
protected function directory( $directory = null )
    {
        switch ( $directory ) {
            case 'storage':
                $this->comment('Cleaning assets library in storage.');
                $count = 0;
                foreach ($this->filesystem->files( $this->directory['storage'] ) as $file)
                {
                    $this->filesystem->delete($file);
                    $count++;
                }
                $this->info($count .' files removed');
            break;

            case 'public':
                $this->comment('Cleaning assets directory in public.');
                $this->filesystem->deleteDirectory( $this->directory['public'] );
                $this->info('Assets directory in public has been removed');
            break;

            default:
                $this->directory('storage');
                $this->directory('public');
            break;
        }
    }
774806
public static function cleanDirectory(string $directory): bool
    {
        self::removeDirectory($directory);
        return self::createDirectory($directory);
    }
429314
public function setDirectory($directory)
    {
        Assert::stringNotEmpty($directory, 'The factory directory must be a non-empty string. Got: %s');

        $this->directory = Path::canonicalize($directory);

        return $this;
    }
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;
	}
1000051
public function flush($dir = '')
    {
        $dir = $cache_dir = APP_PATH . '/' . Config::get('cache_dir');
        \ePHP\Misc\Dir::deleteDir($dir);
        mkdir($dir, 0777);
    }
296806
protected function deleteDirectory($dir)
    {
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($files as $fileInfo) {
            $todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');

            $todo($fileInfo->getRealPath());
        }

        rmdir($dir);
    }
788392
public static function rmdirRecursive(string $directory): void
    {
        if (is_dir($directory)) {
            static::emptyDirectory($directory);

            if (!rmdir($directory)) {
                throw new FilesystemException("Unable to remove directory on path={$directory}");
            }
        }
    }
504963
protected function _makeDirectory( $directory )
    {
        try
        {
            if ( !is_dir( $directory ) && !is_file( $directory ) && !is_link( $directory ) )
            {
                mkdir( $directory, 0755, true );
            }
        }
        catch ( \Exception $e )
        {
            $this->error( $e->getMessage() );
            die;
        }
    }
748082
public static function isEmpty($dir): bool
    {
        $handler = opendir($dir);

        if (false === $handler) {
            throw new FileSystemException("Open the dir failure! DIR: $dir");
        }

        while (($file = readdir($handler)) !== false) {

            if ($file !== '.' && $file !== '..') {
                closedir($handler);

                return false;
            }
        }

        closedir($handler);

        return true;
    }
1108141
protected function emptyDirectory($directory, $deleteDirectory = false)
	{
		if (!is_dir($directory) || !($directoryHandle = opendir($directory))) {
			return;
		}
		
		while (false !== ($object = readdir($directoryHandle))) 
		{
			if ($object == '.' || $object == '..') {
				continue;
			}

			if (!is_dir($directory.'/'.$object)) {
				unlink($directory.'/'.$object);
			}
			else {
				$this->emptyDirectory($directory.'/'.$object, true);	// The object is a folder, recurse through it.
			}
		}
		
		if ($deleteDirectory)
		{
			closedir($directoryHandle);
			rmdir($directory);
		}
	}
272716
public final void removeDirectory(final File directory) {
        try {
            FileUtils.deleteDirectory(directory);
        } catch (IOException e) {
            LOGGER.error("Unable to delete directory '{}'", directory);
        }
    }
407234
public function rm($dir)
    {
        $parseDir = $this->parseLastDirectory($dir);
        if (! ssh2_sftp_rmdir($this->getSFtp(), $parseDir)) {
            throw new DirectoryException('Directory not deleted');
        }
        return true;
    }
180568
protected function createDirectory($directory)
    {
        if (!$this->files->isDirectory($directory)) {
            $this->files->makeDirectory($directory, 0755, true);
        }
    }
1204167
protected function checkDirectory(&$directory)
    {
        $directory = trim($directory, '/');

        $this->checkDirectoryExists($directory);
        $this->checkDirectoryAccess($directory);
    }
656132
private function readDirectoryContent(string $directory): void
    {
        $items = new DirectoryIterator($directory);

        foreach ($items as $item) {

            if ($item->isFile() && $this->isFile($item->getFilename())) {
                $this->readFileContent($item->getPathname());
            }
        }
    }
866960
public function createEmptyDirectory($directory)
    {
        if (!is_dir($directory)) {
            mkdir($directory, 0777, true);
        } else {
            $this->deleteTree($directory, false);
        }
    }