|
1 | | -# dependency-resolver |
2 | | -Dependency-resolver utility based on PSR-11 container to be used for auto-wiring |
| 1 | +![Status][badge] |
| 2 | + |
| 3 | +# Technically Dependency Resolver |
| 4 | + |
| 5 | +`Technically\DependencyResolver` is a simple yet powerful tool to instantiate classes |
| 6 | +autowiring their dependencies by resolving them from a [PSR-11][1] container |
| 7 | +or recursively instantiating them with *DependencyResolver* itself. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- Based on PSR-11 |
| 12 | +- PHP 8.0 ready (supports union type hints; see examples below) |
| 13 | +- PHP 7.1+ compatible |
| 14 | +- Recursive dependencies autowiring |
| 15 | +- Semver |
| 16 | +- Tests |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +Use [Composer][2] package manager to add *Technically\DependencyResolver* to your project: |
| 21 | + |
| 22 | +``` |
| 23 | +composer require technically/dependency-resolver |
| 24 | +``` |
| 25 | + |
| 26 | +## Example |
| 27 | + |
| 28 | +```php |
| 29 | +<?php |
| 30 | + |
| 31 | +final class MyFancyService |
| 32 | +{ |
| 33 | + public class __construct(callable|LoggerInterface $log) |
| 34 | + { |
| 35 | + // initialize |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Resolve service instance, providing dependencies in-place: |
| 40 | +$resolver = new DependencyResolver($container); |
| 41 | +$service = $resolver->resolve(MyFancyService::class, [ |
| 42 | + 'log' => function (string $priority, string $message) { |
| 43 | + error_log("[$priority]: $message"); |
| 44 | + }] |
| 45 | +); |
| 46 | + |
| 47 | +// Resolve service instance, resolving dependencies from container: |
| 48 | +$container->set(LoggerInterface::class, $logger); |
| 49 | +$resolver = new DependencyResolver($container); |
| 50 | +$service = $resolver->resolve(MyFancyService::class); |
| 51 | + |
| 52 | + |
| 53 | +``` |
| 54 | + |
| 55 | +## Credits |
| 56 | + |
| 57 | +- Implemented by [Ivan Voskoboinyk][3] |
| 58 | + |
| 59 | + |
| 60 | +[1]: https://www.php-fig.org/psr/psr-11/ |
| 61 | +[2]: https://getcomposer.org/ |
| 62 | +[3]: https://github.com/e1himself |
| 63 | +[badge]: https://github.com/technically-php/array-container/actions/workflows/test.yml/badge.svg |
0 commit comments