bartfeenstra/dependency-retriever

This package is abandoned and no longer maintained. No replacement package was suggested.

0.4.3 2016-03-25 08:26 UTC

This package is auto-updated.

Last update: 2024-01-29 02:51:16 UTC


README

Build Status

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.