Search code examples
phpdependency-injectionphp-di

Partial dependency injection


In a MVC context, I have a controller that depends on a service, the service in turn depends on a data_source (in the specific case, a client to fetch data from a third-party API).

In order to instantiate the service with a mock data_source when testing, the service's constructor expects a data_source. The same holds for the controller, whose constructor expects a service.

When creating a controller, I want to pass it a request object as well, because I'd prefer this

new Controller(request, service).action_name

to this

new Controller(service).action_name(request)

Achieving this without using any container for Dependency Injection is trivial.

What I don't understand is how to do so using php-di

My objective is to have the service injected into the controller by the container, while passing the request object to the controller myself.

UPDATE 1

This is my ApplicationController

namespace DEC;


class ApplicationController {

    private $service;
    private $request;

    public function __construct(Foo $service, $request) {
        $this->service= $service;
        $this->request = $request;
    }


    public function index() {
        $out = $this->service->foo();
        $out .= $this->request->method();
        return $out;
    }

}

Foo follows

namespace DEC;

class Foo {

    public function __construct() {
    }

    public function foo() {
        return "FOO";
    }
}

This my Request

namespace DEC;

class Foo {

    public function __construct() {
    }

    public function foo() {
        return "FOO";
    }
}

And this is my attempt to get DI to work as I'd like:

$container = ContainerBuilder::buildDevContainer();
$response = $container->call([ApplicationController::class, 'index'], [
            'request' => new Request('GET')
]);
echo $response;

This is the error I get:

Entry "DEC\ApplicationController" cannot be resolved: Parameter $request of __construct() has no value defined or guessable
Full definition:
Object (
    class = DEC\ApplicationController
    scope = singleton
    lazy = false
    __construct(
        $service = get(DEC\Foo)
        $request = #UNDEFINED#
    )
)

N.B.: the error stays the same if I type-hint the request and/or switch the order of params in the constructor

Looking at the error, I infer that the ::call() solution proposed by Matthew Napoli works if I instantiate the controller with just the service and pass the request as a parameter for the action method.

Does this mean that I can't rely on the container for "partial" injection?

UPDATE 2

For the solution described in this update, please look at my own answer to the question


Solution

  • I managed to do this by setting my request in the container before asking for a controller:

    $container->set('DEC\Request', new Request('GET'));
    $controller = $container->get('DEC\ApplicationController');
    $response = $controller->index();