trifs/di 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

trifs/di

Composer 安装命令:

composer require trifs/di

包简介

A simple dependency injection container

README 文档

README

trifs/DI is a simple Dependency Injection Container for PHP, based on awesome Pimple internals. It is, however, behaving as an object, not as an array. It also provides less features, namely protecting parameters, modifying services after creation and fetching the service creation function.

Build Status

Installation

To include trifs/DI in your project, add it to your composer.json:

{
    "require": {
        "trifs/di": "~1.0"
    }
}

Usage

Creating a container is as simple as instantiating it:

use trifs\DI;

$container = new Container();

As many other dependency injection containers, trifs\DI is able to manage two different kind of data; services and parameters.

Defining parameters

$container->cookie_name = 'SESSION_ID';
$container->session_storage_class = 'SessionStorage';

Defining services

A service is an object that does something as part of a larger system, for example: database connection, session handler, etc.

Services are defined by anonymous functions that return an instance of an object:

$container->session_storage = function (Container $container) {
    return new $container->session_storage_class($container->cookie_name);
};

$container->session = function (Container $container) {
    return new Session($c['session_storage']);
};

Notice that the anonymous function has access to the current container instance, allowing references to other services or parameters.

As objects are only created when you get them, the order of the definitions does not matter, and there is no performance penalty.

Using the defined services is also very easy:

// get the session object
$session = $container->session;

// the above call is roughly equivalent to the following code:
// $storage = new SessionStorage('SESSION_ID');
// $session = new Session($storage);

Extending a container

If you use the same libraries over and over, you might want to reuse some services from one project to the other; package your services into a provider by implementing trifs\DI\ServiceProviderInterface:

use trifs\DI\Container;
use trifs\DI\ServiceProviderInterface;

class FooProvider implements ServiceProviderInterface
{
    public function register(Container $container)
    {
        // register services and parameters
    }
}

And to register it:

$container->register(new FooProvider());

Defining factory services

By default, each time you get a service, trifs\DI returns the same instance of it. If you want a different instance to be returned for all calls, wrap your anonymous function with the factory() method:

$container->session = $container->factory(function (Container $container) {
    return new Session($container->session_storage);
});

统计信息

  • 总下载量: 24.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-07-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固