I think I'm doing something wrong when trying to write my own Dependency Injection Container. I'm looking at the laravel's one, and it states in a comment
// If the class is null, it means the dependency is a string or some other // primitive type which we can not resolve since it is not a class and // we will just bomb out with an error since we have no-where to go.
And the code for resolving the class dependencies:
protected function resolveDependencies(array $dependencies)
{
$results = [];
foreach ($dependencies as $dependency) {
// If the dependency has an override for this particular build we will use
// that instead as the value. Otherwise, we will continue with this run
// of resolutions and let reflection attempt to determine the result.
if ($this->hasParameterOverride($dependency)) {
$results[] = $this->getParameterOverride($dependency);
continue;
}
// If the class is null, it means the dependency is a string or some other
// primitive type which we can not resolve since it is not a class and
// we will just bomb out with an error since we have no-where to go.
$result = is_null(Util::getParameterClassName($dependency))
? $this->resolvePrimitive($dependency)
: $this->resolveClass($dependency);
if ($dependency->isVariadic()) {
$results = array_merge($results, $result);
} else {
$results[] = $result;
}
}
return $results;
}
This is my implementation of a such class: Link to the code
So I have the following questions:
- What should I do If I have primitive types in my constructor? For example:
public function __construct(ServerConfig $config, RequestInterface $request)But here$requestis actually aGuzzleHttp\Psr7\Requestthat has the following dependecies:
public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1') {}
- Should I DI classes that have no primitive types?
- What happens if I have a class A, that Needs class B as DI, but Class B is dependant on class C, that has some primitive types?