-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Additional information on installation, contribution, license and usage example
- Loading branch information
1 parent
88e8901
commit 96c0951
Showing
1 changed file
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,73 @@ | ||
# Yadic | ||
# Yet another dependency injection container for PHP | ||
|
||
![StyleCi](https://github.styleci.io/repos/816542238/shield) | ||
[![Latest Stable Version](https://poser.pugx.org/webdevcave/yadic/v/stable?format=flat-square)](https://packagist.org/packages/webdevcave/yadic) | ||
[![Latest Unstable Version](https://poser.pugx.org/webdevcave/yadic/v/unstable?format=flat-square)](https://packagist.org/packages/webdevcave/yadic) | ||
[![Total Downloads](https://poser.pugx.org/webdevcave/yadic/downloads?format=flat-square)](https://packagist.org/packages/webdevcave/yadic) | ||
[![License](https://poser.pugx.org/webdevcave/yadic/license?format=flat-square)](https://packagist.org/packages/webdevcave/yadic) | ||
|
||
Yet another dependency injection container for PHP | ||
This is a simple to use, yet powerful service container that provides a seamless way to automate dependency injection | ||
with auto-wiring. | ||
|
||
```bash | ||
composer require webdevcave/yadic | ||
``` | ||
|
||
Alternatively, you can clone the repository or download the source files directly and include them in your project. | ||
|
||
```php | ||
<?php | ||
|
||
require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
use Webdevcave\Yadic\Annotations\Provides; | ||
use Webdevcave\Yadic\Annotations\Singleton; | ||
use Webdevcave\Yadic\ServiceContainer; | ||
|
||
interface StorageInterface | ||
{ | ||
public function store(mixed $data): bool; | ||
} | ||
|
||
#[Provides(StorageInterface::class)] | ||
#[Singleton] | ||
class Storage implements StorageInterface | ||
{ | ||
public function store(mixed $data): bool | ||
{ | ||
//store data... | ||
|
||
return true; | ||
} | ||
} | ||
|
||
class MyController | ||
{ | ||
public function __construct( | ||
private StorageInterface $storage | ||
) { | ||
} | ||
|
||
public function save(): bool | ||
{ | ||
return $this->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(MyController::class)->save()); //bool(true) | ||
``` | ||
|
||
## 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. |