public static function extendTwigEnvironment(
        ContainerInterface $container,
        ?TwigEnvironment $twig = null
    ): ?TwigEnvironment {
        if ($twig !== null) {
            $options = self::resolveOptions($container->get('config'));

            if ($options['collector']['twig'] === true) {
                $twig->addExtension(new ProfilerExtension(
                    $container->get(Profile::class)
                ));
            }
        }

        return $twig;
    }