How to install and use "Twig Component" with Slim4

33 views Asked by At

i'm trying to add symfony/ux-twig-component to my Slim 4 project (PHP 8.2) but I don't see how to add the extension in the Twig::class of my container.

My project is initialized by my init.php file:

// ...
require_once $_SERVER['APP_ROOT'] . '/vendor/autoload.php';
$container = (new ContainerBuilder())
    ->addDefinitions($_SERVER['APP_ROOT'] . '/config/container.php')
    ->build();

return $container->get(App::class);

And in container.php:

// ...

// all use
use Symfony\UX\TwigComponent\DependencyInjection\TwigComponentExtension;

return [
    // ...
    App::class => function (ContainerInterface $container) {
        $app = AppFactory::createFromContainer($container);

        // Register routes
        // ...

        // Register middleware
        // ...

        return $app;
    },

    Twig::class => function (ContainerInterface $container) {
        $settings = $container->get('settings');
        $twigSettings = $settings['twig'];
        $options = $twigSettings['options'];
        $options['cache'] = $options['cache_enabled'] ? $options['cache_path'] : false;
        $twig = Twig::create($twigSettings['paths'], $options);
        
        // Extensions
        $twig->addExtension(new DebugExtension());
        $twig->addExtension(new InstanceofExtension());

        // My test
        $twigComponentExtension = $container->get(TwigComponentExtension::class);
        $twig->addExtension($twigComponentExtension);
        // -------
        // Or :
        /*
        $twig->addExtension(new TwigComponentExtension());
        // but "Expected parameter of type 'Twig\Extension\ExtensionInterface', 'Symfony\UX\TwigComponent\DependencyInjection\TwigComponentExtension' provided"
        */

        $environment = $twig->getEnvironment();
        $environment->addGlobal('ASSER_LOGO', 'logo_assercar.svg');
        $environment->addGlobal('APP_VERSION', $settings['APP_VERSION']);
        $environment->addGlobal('ASR_ENV', $settings['ASR_ENV']);
        return $twig;
    },
    TwigMiddleware::class => function (ContainerInterface $container) {
        return TwigMiddleware::createFromContainer(
            $container->get(App::class),
            Twig::class
        );
    },
    
    // ...
];

But I have:

Uncaught Error: Interface "Symfony\Component\Config\Definition\ConfigurationInterface" not found in /home/project/slim/vendor/symfony/ux-twig-component/src/DependencyInjection/TwigComponentExtension.php:43

I don't know what to do because there is no documentation on integrating symfony/ux-twig-component into frameworks other than Symfony

0

There are 0 answers