Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koriym/dii
Dependency Injection Container Plugin for Yii 1
https://github.com/koriym/dii
ray-di yii
Last synced: 10 days ago
JSON representation
Dependency Injection Container Plugin for Yii 1
- Host: GitHub
- URL: https://github.com/koriym/dii
- Owner: koriym
- License: mit
- Created: 2019-07-19T12:31:27.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-09T05:59:53.000Z (5 months ago)
- Last Synced: 2024-10-11T08:14:17.425Z (25 days ago)
- Topics: ray-di, yii
- Language: PHP
- Homepage:
- Size: 6.47 MB
- Stars: 17
- Watchers: 7
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# Dii
## Dependency Injection Container Plugin for Yii 1
This plugin adds the ability to configure object instances and their dependencies before they are used, and to store them into a container class to easy access.
It uses the clean and flexible [Ray.Di](https://github.com/ray-di/Ray.Di) DI framework which is a PHP dependency injection framework in the style of "Google Guice".
Ray.Di also allows you to program using AOP, that is, decorating the configured instances so some logic can be run before or after any of their methods.
## Configuration
### Bootstrap file
Use composer autoloader instead of Yii autoloader.
```php
// composer autoloader
require dirname(__DIR__) . '/vendor/autoload.php';
spl_autoload_unregister([YiiBase::class, 'autoload']);// set context module
Dii::setContext(\YourVendor\YourProject\Context\App::class);// run the application
Yii::createWebApplication()->run();
```### Binding module
Modules are classes that describe how instances and their dependencies should be constructed, they provide a natural way of grouping configurations. An example module looks like this:
```php
bind(FooInterface::class)->to(Foo::class);
$this->bindInterceptor(
$this->matcher->any(),
$this->matcher->startsWith('actionIndex'),
[BarInterceptor::class]
);
}
}
```
## ContextYou can make the necessary bindings depending on the context. The context class specifies which module to bind in.
```php
use Koriym\Dii\Module\AppModule;
use Koriym\Dii\ModuleProvider;
use Ray\Di\AbstractModule;class App implements ModuleProvider
{
public function __invoke() : AbstractModule
{
return new AppModule();
}
}
```In this example we have overridden the binding of `AppModule` with `TestModule`.
```php
class Test implements ModuleProvider
{
public function __invoke() : AbstractModule
{
// override AppModule with TestModule
return new TestModule(new AppModule());
}
}
```## Injecting Dependencies in Controllers
Ray.Di is able to inject instances to your controllers based on annotations:
```php
foo = $foo;
}public function actionIndex()
{
echo 'Hello World' . $this->foo->get();
}
}
```As soon as the controller is created, all methods having the `@Inject` annotation will get instances of the hinted class passed. This works only for setter method, not constructors. Please implemet marker interface `Injectable` to notify Ray.Di the class injectable.
Also any class created by `Yii:createComponent()` method is worked as well.
## Demo
cd demo
composer install
composer serve