Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bartfeenstra/dependency-retriever
https://github.com/bartfeenstra/dependency-retriever
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/bartfeenstra/dependency-retriever
- Owner: bartfeenstra
- License: mit
- Created: 2016-03-06T12:12:00.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-03-25T08:29:23.000Z (almost 9 years ago)
- Last Synced: 2023-08-21T08:09:51.806Z (over 1 year ago)
- Language: PHP
- Size: 22.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Dependency Retriever (woof!)
[![Build Status](https://travis-ci.org/bartfeenstra/dependency-retriever.svg?branch=master)](https://travis-ci.org/bartfeenstra/dependency-retriever)
This package is a tool to make
[dependency injection](https://en.wikipedia.org/wiki/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:```php
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.```php
$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.