定制 caridea/container 二次开发

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

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

caridea/container

最新稳定版本:3.0.1

Composer 安装命令:

composer require caridea/container

包简介

A shrimp of a dependency injection library

README 文档

README

Caridea is a miniscule PHP application library. This shrimpy fellow is what you'd use when you just want some helping hands and not a full-blown framework.

This is its PSR-11 compliant dependency injection container.

Packagist Build Status Scrutinizer Code Quality Code Coverage

Installation

You can install this library using Composer:

$ composer require caridea/container
  • The master branch (version 3.x) of this project requires PHP 7.1 and depends on caridea/event.
  • Version 2.x of this project requires PHP 7.0 and depends on caridea/event.
  • Version 1.x of this project requires PHP 5.5 and depends on caridea/event.

Compliance

Releases of this library will conform to Semantic Versioning.

Our code is intended to comply with PSR-1, PSR-2, and PSR-4. If you find any issues related to standards compliance, please send a pull request!

Overview

  • The Caridea\Container\Properties class is intended for scalar configuration values that might be used as settings for other components.
  • The Caridea\Container\Objects class allows for eager, lazy, and prototype objects.
    • It also implements Caridea\Event\Publisher and will broadcast events to any managed object which implements Caridea\Event\Listener.
  • The Caridea\Container\EmptyContainer class is an empty, no-op container.

You can retrieve contained objects both by name and by type!

Documentation

Examples

Just a few quick examples.

Configuration and Dependencies

$config = [
    'db.uri' => 'mongodb://localhost:27017',
    'mail.host' => '192.168.1.100'
];
$properties = new \Caridea\Container\Properties($config);
$objects = \Caridea\Container\Objects::builder()
    ->eager('mongoClient', 'MongoClient', function($c){
        return new \MongoClient($c->get('db.uri'));
    })
    ->lazy('mailService', 'My\Mail\Service', function($c){
        return new \My\Mail\Service($c->('mail.host'));
    })
    ->lazy('userService', 'My\User\Service', function($c){
        return new \My\User\Service($c->get('mongoClient'), $c->get('objectStorage'));
    })
    ->proto('objectStorage', 'SplObjectStorage', function($c){
        return new \SplObjectStorage();
    })
    ->build($properties);

$userService = $objects->get('userService');

Parent Delegation

You can nest Objects containers. For example, you can have a container with service objects and a child container with web controllers.

$services = \Caridea\Container\Objects::builder()
    ->eager('blogService', 'My\Blog\Service', function($c){
        return new \My\Blog\Service();
    })
    ->build();
$controllers = \Caridea\Container\Objects::builder()
    ->eager('blogController', 'My\Blog\Controller', function($c){
        return new \My\Blog\Controller($c->get('blogService'));
    })
    ->build($services);

$controllers = $controllers->getByType('My\Blog\Controller'); // ['blogController' => BlogController]

Events

$objects = \Caridea\Container\Objects::builder()
    ->eager('eventListener', 'My\Cool\EventListener', function($c){
        // we are assuming that this class implements Caridea\Event\Listener
        return new \My\Cool\EventListener();
    })
    ->build();

// assuming that CustomEvent implements Caridea\Event\Event
$objects->publish(new CustomEvent());
// Here, the eventListener object will have its ->notify() method invoked with the CustomEvent

Any objects returned from an Objects container that implement \Caridea\Event\PublisherAware will receive the container via the setPublisher method.

ContainerAware

Any objects returned from an Objects container that implement \Caridea\Container\ContainerAware will receive the container via the setContainer method. We provide a trait to make this easier.

class MyContainerAware implements \Caridea\Container\ContainerAware
{
    use \Caridea\Container\ContainerSetter;

    public function __construct()
    {
        $this->container = new \Caridea\Container\EmptyContainer();
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2015-06-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固