Background
I am writing some reusable library, which contains few classes. One of them needs to have a dependency, because of some more complex logic and I want to delegate the responsibility of that class somewhere else (another class).
Goal
I don't want to create a bundle e.g. Symfony Bundle which could handle my dependency injection and provide a simple way to integrate it with a client code. My goal is to provide reusable and framework independent solution.
Additional information
I am using composer and I've read about DI containers like php-di. There is a demo application example by php-di, but it doesn't fit into my requirements.
Example
Fragment of library code
<?php
class W3CAnalyzer implements WebStandardAnalyzer
{
private $httpClient;
public function __construct(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
}
public function analyze(string $url): WC3AnalysisMetaData
{
$siteContent = $this->httpClient->getContent($url);
//further logic there
}
}
As you can see W3CAnalyzer contains the dependency HttpClient class.
I will somehow configure a DI container to register and resolve that dependency.
Sample client code
<?php
class WebStandardController
{
private $webStandardAnalyzer;
public function __construct(WebStandardAnalyzer $webStandardAnalyzer)
{
$this->webStandardAnalyzer = $webStandardAnalyzer;
}
}
In client project, programmer will register W3CAnalyzer to be injected into controller as WebStandardAnalyzer using his DI configurations (it doesn't really matter how).
The question
If I use some DI configuration and container in my library internally, how can I integrate this configuration with the client code, so there will be a possibility to register library class and all of its dependencies would have been resolved then? I don't know yet, how I will organize DI inside my library (this is actually the first part of the problem), I want to somehow use the php-di library mentioned above.
To summarize, how to organize DI inside non-framework reusable library, so that library could be used inside every other project (no matter which framework is used) and its (that library) dependencies would also be resolved during runtime of that project.
Thank you!