protected function makeDirectory($path, $baseDir = null)
    {
        if (is_dir($path) === true) {
            return;
        }

        if ($this->chmod === null) {
            $chmod = 0777;
        } else {
            $chmod = $this->chmod;
        }

        $retries = 3;
        while ($retries --) {
            @mkdir($path, $chmod, true);
            if (is_dir($path)) {
                break;
            }
            usleep(20);
        }

        // enforce the correct mode for all directories created
        if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) {
            $path    = strtr(str_replace($baseDir, '', $path), '\\', '/');
            $folders = explode('/', trim($path, '/'));
            foreach ($folders as $folder) {
                $baseDir .= $folder . DIRECTORY_SEPARATOR;
                if (!chmod($baseDir, $chmod)) {
                    throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true));
                }
            }
        }
    }