bartfeenstra / dependency-retriever
Requires
- php: ~5.6 || ~7.0
Requires (Dev)
- phpunit/phpunit: ~5.2
- squizlabs/php_codesniffer: ~2.5
Suggests
- bartfeenstra/dependency-retriever-symfony-bridge: Retrieves dependencies from Symfony's service container.
- drupal/retriever: Retrieves dependencies from Drupal.
README
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.
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:
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.
$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.