Dependency Retriever (woof!)
, (*1)
This package is a tool to make
dependency injection and
class instantiation easier. Its API allows class' dependencies to be discovered
and injected automatically by the factory., (*2)
Retrievers help you inject dependencies, even if you can't or won't from the
calling code, by retrieving them based on suggestions from the class authors:, (*3)
use Psr\Log\LoggerInterface;
class Bar {
/**
* @suggestedDependency drupalContainerService:logger.channel.form $formLogger
*/
public function __construct(LoggerInterface $formLogger, $severity) {
// ...
}
}
When used in a system in which Drupal's service container is available, the
logger.channel.form service is a suggested dependency for the $formLogger
parameter. The drupalContainerService retriever can retrieve this dependency
and give it to the factory to be injected during class instantiation., (*4)
$factory = new SimpleFactory(new AnnotatedFinder(), new DrupalContainerServiceRetriever());
$bar = $factory->instantiate(Bar::class, [
'severity' => LogLevel::WARNING,
]);
In this example, Bar is instantiated using an overridden dependency (value) for $severity, but AnnotatedFinder,
and the hypothetical DrupalContainerServiceRetriever provide the factory with a dependency for $formLogger based on
Bar's @suggestedDependency annotation., (*5)