https://github.com/webdevcave/yadic-php
Yet another dependency injection container for PHP
https://github.com/webdevcave/yadic-php
autowiring dependency-injection hydration object-hydration php php81 service-container
Last synced: 5 months ago
JSON representation
Yet another dependency injection container for PHP
- Host: GitHub
- URL: https://github.com/webdevcave/yadic-php
- Owner: WebdevCave
- License: mit
- Created: 2024-06-18T00:51:21.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-06T02:38:59.000Z (almost 2 years ago)
- Last Synced: 2025-02-15T20:42:20.901Z (over 1 year ago)
- Topics: autowiring, dependency-injection, hydration, object-hydration, php, php81, service-container
- Language: PHP
- Homepage:
- Size: 104 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Yet another dependency injection container for PHP

[](https://packagist.org/packages/webdevcave/yadic)
[](https://packagist.org/packages/webdevcave/yadic)
[](https://packagist.org/packages/webdevcave/yadic)
[](https://packagist.org/packages/webdevcave/yadic)
[](https://codecov.io/github/WebdevCave/yadic-php)
This is a simple to use, yet powerful service container that provides a seamless way to automate dependency injection
featuring auto-wiring and object hydration.
```bash
composer require webdevcave/yadic
```
Alternatively, you can clone the repository or download the source files directly and include them in your project.
## Usage
### Autowiring
```php
storage->store('my data...');
}
}
$container = new ServiceContainer();
//No need to do this in a real world application:
$container->addAlias(StorageInterface::class, Storage::class);
//Use this instead:
//$container->loadDefinitionsFromDirectory($directory, $namespace); //Loads annotations from classes declared in a PSR4 directory
//var_dump($container->get('storage')->store($data));
var_dump($container->get(MyController::class)->save()); //bool(true)
```
### Invoking a method ft. autowiring
```php
$arguments = ['nonInjectableArgument' => 'value']; //optional
$container->invoke([$instance, 'methodName'], $arguments);
```
### Hydration
```php
//Class declarations:
use Webdevcave\Yadic\Annotations\ArrayOf;
class Candidate
{
public function __construct(
public ?string $name = null,
public ?int $age = null,
#[ArrayOf(Skill::class)]
public array $skills = []
) {
}
}
class Skill
{
public function __construct(
public string $title,
) {
}
}
// Hydration example 1:
$data = [
'name' => 'John Doe',
'age' => 25,
'skills' => [
['title' => 'PHP'],
['title' => 'Java'],
['title' => 'Rust'],
['title' => 'React'],
],
];
$instance = $container->hydrate(Candidate::class, $data);
//Results output
/*
print_r($instance);
This test printed output:
Candidate Object
(
[name] => John Doe
[age] => 25
[skills] => Array
(
[0] => Skill Object
(
[title] => PHP
)
[1] => Skill Object
(
[title] => Java
)
[2] => Skill Object
(
[title] => Rust
)
[3] => Skill Object
(
[title] => React
)
)
)
*/
// Hydration example 2:
$data = [
[
'name' => 'Foo',
//...
],
[
'name' => 'Bar',
//...
]
];
$instances = $container->hydrate(Candidate::class, $data);
//Results output
/*
print_r($instances);
This test printed output:
Array
(
[0] => Candidate Object
(
[name] => Foo
//...
)
[1] => Candidate Object
(
[name] => Bar
//...
)
)
*/
```
## Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements,
please open an issue or a pull request on GitHub.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.