定制 minphp/container 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

minphp/container

最新稳定版本:2.1.0

Composer 安装命令:

composer require minphp/container

包简介

A Dependency Injection Container based on Pimple

README 文档

README

Build Status Coverage Status

A standards compliant Pimple-based container.

Installation

Install via composer:

composer require minphp/container:~2.0

Basic Usage

<?php
use Minphp\Container\Container;

$container = new Container();

// Add/Update
$container->set('queue', function($c) {
    return new \SplQueue();
});

// Verify
$queue_exists = $container->has('queue');

// Fetch
$queue = $container->get('queue');

// Remove
$container->remove('queue');

You can also use the container as an array, as per Pimple. Though, using the ContainerInterface defined methods is preferable as it eases switching to a different container if you ever need to.

<?php
use Minphp\Container\Container;

$container = new Container();
$container['queue'] = function($c) {
    return new \SplQueue();
};

$queue = $container['queue'];

ContainerInterface

interface ContainerInterface
{
    // inherited from Interop\Container\ContainerInterface
    public function get($id);

    // inherited from Interop\Container\ContainerInterface
    public function has($id);

    public function set($id, $value);

    public function remove($id);
}

ContainerAwareInterface

Also provided is the ContainerAwareInterface, which allows your classes to declare that they support container injection. Nifty for granting objects access to your container (you weren't thinking of using a static class, were you?).

interface ContainerAwareInterface
{
    public function setContainer(Minphp\Container\ContainerInterface $container = null);

    public function getContainer();
}

Usage

<?php
namespace myApp\Controller;

use Minphp\Container\ContainerAwareInterface;
use Minphp\Container\ContainerInterface;

class MyController implements ContainerAwareInterface
{
    private $container = null;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    public function getContainer()
    {
        return $this->container;
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-12