<?php

declare(strict_types=1);

namespace apexl\Io\modules\system\services;

use apexl\Io\factories\IoRequestFactory;
use DI\Bridge\Slim\ControllerInvoker;
use Invoker\Invoker;
use Invoker\ParameterResolver\AssociativeArrayResolver;
use Invoker\ParameterResolver\Container\TypeHintContainerResolver;
use Invoker\ParameterResolver\DefaultValueResolver;
use Invoker\ParameterResolver\ResolverChain;
use Psr\Container\ContainerInterface;

class Bridge extends \DI\Bridge\Slim\Bridge
{
    protected static function createControllerInvoker(ContainerInterface $container): ControllerInvoker
    {
        $resolvers = [
            new RequestResolver(
                $container->get(IoRequestFactory::class),
            ),
            // Inject parameters by name first
            new AssociativeArrayResolver(),
            // Then inject services by type-hints for those that weren't resolved
            new TypeHintContainerResolver($container),
            // Then fall back on parameters default values for optional route parameters
            new DefaultValueResolver(),
        ];

        $invoker = new Invoker(new ResolverChain($resolvers), $container);

        return new ControllerInvoker($invoker);
    }
}
