protected function replaceRouteParameters(string $path, array &$parameters): string
    {
        $path = $this->replaceNamedParameters($path, $parameters);

        $path = \preg_replace_callback('/\{.*?\}/', static function ($match) use (&$parameters) {
            if (\count($parameters) === 0 && ! (\substr($match[0], -\strlen('?}')) === '?}')) {
                return $match[0];
            }

            return \array_shift($parameters);
        }, $path);

        return \trim(\preg_replace('/\{.*?\?\}/', '', $path), '/');
    }