All Projects → koriym → dii

koriym / dii

Licence: MIT License
Dependency Injection Container Plugin for Yii 1

Programming Languages

PHP
23972 projects - #3 most used programming language

Labels

Projects that are alternatives of or similar to dii

yii2-presenter
Yii2 View Presenter
Stars: ✭ 13 (-18.75%)
Mutual labels:  yii
timeclock
Simple work time clocking service
Stars: ✭ 21 (+31.25%)
Mutual labels:  yii
ist-yii-cfile
CFile provides popular functions to manipulate files both with Yii Framework and without
Stars: ✭ 56 (+250%)
Mutual labels:  yii
ar-variation
Variation behavior for ActiveRecord
Stars: ✭ 46 (+187.5%)
Mutual labels:  yii
LazyWaimai-Web
懒人外卖的 Web 端,使用Yii2框架+Bootstrap进行开发
Stars: ✭ 32 (+100%)
Mutual labels:  yii
filedb
ActiveRecord for static data definitions based on files
Stars: ✭ 72 (+350%)
Mutual labels:  yii
project-template
Yii2 Project Template
Stars: ✭ 53 (+231.25%)
Mutual labels:  yii
yii2-multi-select-widget
Bootstrap MultiSelect and MultiSelect Listbox widgets for Yii2
Stars: ✭ 45 (+181.25%)
Mutual labels:  yii
ar-dynattribute
Provide ActiveRecord dynamic attributes stored into the single field in serialized state
Stars: ✭ 43 (+168.75%)
Mutual labels:  yii
PayumYiiExtension
Rich payment solutions for Yii framework. Paypal, payex, authorize.net, be2bill, omnipay, recurring paymens, instant notifications and many more
Stars: ✭ 13 (-18.75%)
Mutual labels:  yii
yii2-composer
Yii 2 composer extension
Stars: ✭ 76 (+375%)
Mutual labels:  yii
yiipowered
Yii powered websites showcase
Stars: ✭ 88 (+450%)
Mutual labels:  yii
yii-fullajax
fullajax Yii playground
Stars: ✭ 21 (+31.25%)
Mutual labels:  yii
Ip
🌏根据IpV4、IpV6地址获取定位信息的PHP🐘组件 PHP components that obtain location information based on IpV4, IpV6 addresses
Stars: ✭ 23 (+43.75%)
Mutual labels:  yii
yii2-facades
Facades for Yii 2
Stars: ✭ 21 (+31.25%)
Mutual labels:  yii
ar-role
ActiveRecord behavior, which provides relation roles (table inheritance)
Stars: ✭ 34 (+112.5%)
Mutual labels:  yii
yii2-editable-widget
X-Editable Widget for Yii2
Stars: ✭ 56 (+250%)
Mutual labels:  yii
array-access
PHP multi array access
Stars: ✭ 23 (+43.75%)
Mutual labels:  yii
yii2-starter-kit-lite
Yii2 Starter kit for begin your application
Stars: ✭ 41 (+156.25%)
Mutual labels:  yii
awesome-yii2
Curated list of resources about using Yii2 - Yii is a high-performance component-based PHP framework.
Stars: ✭ 36 (+125%)
Mutual labels:  yii

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 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.

// 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

namespace Koriym\Dii\Module;

use Ray\Di\AbstractModule;
use Vendor\Hello\BarInterceptor;
use Vendor\Hello\Foo;
use Vendor\Hello\FooInterface;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->bind(FooInterface::class)->to(Foo::class);
        $this->bindInterceptor(
            $this->matcher->any(),
            $this->matcher->startsWith('actionIndex'),
            [BarInterceptor::class]
        );
    }
}

Context

You can make the necessary bindings depending on the context. The context class specifies which module to bind in.

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.

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

use Koriym\Dii\Injectable;
use Ray\Di\Di\Inject;
use Vendor\Hello\FooInterface;

class SiteController extends CController implements Injectable
{
    private $foo;

    /**
     * @Inject
     */
    public function setDeps(FooInterface $foo)
    {
        $this->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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].