private function createDirIfNotExists(string $dir): bool
    {
        if (!is_dir($dir)) {
            try {
                set_error_handler(function($errno, $errstr, $errfile, $errline) {
                    restore_error_handler();
                    throw new \IvoPetkov\ObjectStorage\ErrorException($errstr, 0, $errno, $errfile, $errline);
                });
                $result = mkdir($dir, 0777, true);
                restore_error_handler();
                return (bool) $result;
            } catch (\IvoPetkov\ObjectStorage\ErrorException $e) {
                if ($e->getMessage() !== 'mkdir(): File exists') { // The directory may be just created in other process.
                    throw $e;
                }
            }
        }
        return true;
    }