public function clearDirectory($dir)
    {
        if (empty($dir) || !@is_dir($dir)) {
            return false;
        }

        $iterator = new \RecursiveDirectoryIterator($dir);
        $filter = new \RecursiveCallbackFilterIterator($iterator, function ($current) {
            /** @var \SplFileInfo $current */
            $filename = $current->getBasename();
            // Check for files and dirs starting with "dot" (.gitignore, etc)
            return !($filename && $filename[0] === '.');
        });

        /** @var \SplFileInfo[] $listing */
        $listing = new \RecursiveIteratorIterator($filter, \RecursiveIteratorIterator::CHILD_FIRST);
        foreach ($listing as $item) {
            $path = $item->getPathname();
            $item->isDir() ? rmdir($path) : unlink($path);
        }
        return true;
    }