ice-cream/di 问题修复 & 功能扩展

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

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

ice-cream/di

Composer 安装命令:

composer require ice-cream/di

包简介

ice-cream di (Dependency Injection) is my own take on DI, to keep it simple and easy to use.

README 文档

README

Build Status Packagist Maintenance Made With Love

  • Requires PHP 7.2.x
  • Is Standalone

I wanted a simple and effective way to replicate pimple and create DI at it's most basic level.

The core concept is simple, you have a container that you can add items to and fetch via the ArrayAccess interface.

We also allow you to create factories, which allow you to return new instances of the object each time instead of the same object every time.

Documentation

You can view this packages documentation here

Philosophy

Ice Cream is not meant to be the next big thing in OSS. It is essentially Pimple plagiarized.

I wanted to build a simple DI container that I could use in my own projects to try and better understand DI at a fundamental level.

Examples:

The following is a super basic example of how to use the container.

use IceCreamDI\Container;

$container = new Container();

$container['service'] = function($c) {
  return new Service();
}

$container['services'] // Returns instance of Service class.

ATTN!!

We pass the instance of the container to all of our closures, this is exactly like pimple. Even when it comes to extending the already registered service provider you can be sure that the new extension will also have the container object.

Even more simpler:

$container = new Container([
  'app.service' => function ($c) {
    return new Service();
  },
]);

ATTN!

Should you have something in the container with the same name and you throw in another object under the same name you will break your container. Make sure your names are unique.

ATTN!

You cannot manipulate a container object after its been called, for example:

$container = new Container();
$container['service'] = function ($c) { return new Service(); };

$service = $container['service'];

$container->extend('service', function($service){ $service->someMethodCall(); });

The above will error out, because when you "build" the item from the service container, we lock it in place.

What if you want to extend an already registered item in the container? Well we can do the following:

use IceCreamDI\Container;

$container = Container();

$container['service'] = function($c) {
  return new Service();
}

$container->extend('service', function($service){
  $service-> ....

  ....

  return $service;
});

This allows you to easily register and then manipulate items in the container.

If you just want the closure back, you can call the raw method:

use IceCreamDI\Container;

$container = Container();

$container['service'] = function($c) {
  return new Service();
}

$container->raw('service'); // => closure instance.

When you call the factory method, you will always get a new instance of the registered object. For instance:

use IceCreamDI\Container;

$container = Container();

$container['service'] = $container->factory(function($c) {
  return new $service();
});

$container['service']; // => Will be a new instance every time.

Where as with the the regular $container['service'] you will always get the same object back.

What if you have a factory that has dependencies? For example, assume you have a class that each time it's called, you need to inject dependencies into the class. You can use the resolveFactory method:

use IceCreamDI\Container;

$container = Container();

$container['service_deps'] = [
  'dep1' => ...,
  ...
];

// Notice how you even have access to the containe object $c.
$container['service'] = $container->factory(function($dep1, $dep2, ..., $c) {
  return new $service($dep1, $dep2, ...);
});

$container->resolveFactory('service', 'service_deps'); // Gives you a new service instance with deps passed in.

As you see above we inject the dependencies. We even add on an addition dependency which is the $c container object dependency so that the registered factory that will be resolved has a instance of the container.

We can also call a method on a container object:

use IceCreamDI\Container;

$container = Container();

$container['service'] = function() { return new Service(); };

$container->call('service', 'process'); // Same as doing: $container['service']->process();

ice-cream/di 适用场景与选型建议

ice-cream/di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 08 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「dependency injection」 「library」 「di」 「dependency management」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ice-cream/di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ice-cream/di 我们能提供哪些服务?
定制开发 / 二次开发

基于 ice-cream/di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-13