|
1 | | -# Yadic |
| 1 | +# Yet another dependency injection container for PHP |
2 | 2 |
|
3 | 3 |  |
4 | 4 | [](https://packagist.org/packages/webdevcave/yadic) |
5 | 5 | [](https://packagist.org/packages/webdevcave/yadic) |
6 | 6 | [](https://packagist.org/packages/webdevcave/yadic) |
7 | 7 | [](https://packagist.org/packages/webdevcave/yadic) |
8 | 8 |
|
9 | | -Yet another dependency injection container for PHP |
| 9 | +This is a simple to use, yet powerful service container that provides a seamless way to automate dependency injection |
| 10 | +with auto-wiring. |
| 11 | + |
| 12 | +```bash |
| 13 | +composer require webdevcave/yadic |
| 14 | +``` |
| 15 | + |
| 16 | +Alternatively, you can clone the repository or download the source files directly and include them in your project. |
| 17 | + |
| 18 | +```php |
| 19 | +<?php |
| 20 | + |
| 21 | +require_once __DIR__.'/vendor/autoload.php'; |
| 22 | + |
| 23 | +use Webdevcave\Yadic\Annotations\Provides; |
| 24 | +use Webdevcave\Yadic\Annotations\Singleton; |
| 25 | +use Webdevcave\Yadic\ServiceContainer; |
| 26 | + |
| 27 | +interface StorageInterface |
| 28 | +{ |
| 29 | + public function store(mixed $data): bool; |
| 30 | +} |
| 31 | + |
| 32 | +#[Provides(StorageInterface::class)] |
| 33 | +#[Singleton] |
| 34 | +class Storage implements StorageInterface |
| 35 | +{ |
| 36 | + public function store(mixed $data): bool |
| 37 | + { |
| 38 | + //store data... |
| 39 | + |
| 40 | + return true; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +class MyController |
| 45 | +{ |
| 46 | + public function __construct( |
| 47 | + private StorageInterface $storage |
| 48 | + ) { |
| 49 | + } |
| 50 | + |
| 51 | + public function save(): bool |
| 52 | + { |
| 53 | + return $this->storage->store('my data...'); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +$container = new ServiceContainer(); |
| 58 | + |
| 59 | +//No need to do this in a real world application: |
| 60 | +$container->addAlias(StorageInterface::class, Storage::class); |
| 61 | + |
| 62 | +//Use this instead: |
| 63 | +//$container->loadDefinitionsFromDirectory($directory, $namespace); //Loads annotations from classes declared in a PSR4 directory |
| 64 | + |
| 65 | +var_dump($container->get(MyController::class)->save()); //bool(true) |
| 66 | +``` |
| 67 | + |
| 68 | +## Contributing |
| 69 | +Contributions are welcome! If you find any issues or have suggestions for improvements, |
| 70 | +please open an issue or a pull request on GitHub. |
| 71 | + |
| 72 | +## License |
| 73 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments