Xinax\LaravelGettext\FileSystem::clearDirectory PHP Method

clearDirectory() public static method

Removes the directory contents recursively
public static clearDirectory ( string $path ) : null | boolean
$path string
return null | boolean
    public static function clearDirectory($path)
    {
        if (!file_exists($path)) {
            return null;
        }
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
        foreach ($files as $fileinfo) {
            // if the file isn't a .gitignore file we should remove it.
            if ($fileinfo->getFilename() !== '.gitignore') {
                $todo = $fileinfo->isDir() ? 'rmdir' : 'unlink';
                $todo($fileinfo->getRealPath());
            }
        }
        // since the folder now contains a .gitignore we can't remove it
        //rmdir($path);
        return true;
    }

Usage Example

 /**
  * Clear all files generated for testing purposes
  */
 protected function clearFiles()
 {
     $dir = __DIR__ . '/../lang/i18n';
     FileSystem::clearDirectory($dir);
 }