承接 anonymous-php/simple-di 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

anonymous-php/simple-di

Composer 安装命令:

composer require anonymous-php/simple-di

包简介

Simple Dependency Resolving and Injection Container

README 文档

README

Actually this library is more about the dependency container than auto-wiring but don't worry auto-wiring is there. You have the possibility to create instances of classes with auto-wiring of their arguments and to inject dependencies to called methods.

History

I like PHP-DI very much but when I started new project I understood it needs another approach of determining it's definitions and dependencies. I wanted to have the simplest as possible syntax of definitions and powerful, but not complex at the same time, mechanism of injection. So I just wrote my own DI.

Peculiarities

I didn't want to wrap any definition to unnecessary functions which just marks this definition as an instantiable class. I already know what is it while write a code and I want to decide how to use this information in my application.

My colleague asked me about the primitives injection in my library. I don't think is a good idea. Just make the dependency for the Container and get the value you need. Keep this type of values under control by yourself.

Compatibility

Auto-wiring is DISABLED by default.

Installation

composer require anonymous-php/simple-di

Methods

Сlass Container implements \Psr\Container\ContainerInterface interface so it has two methods has($id) which works as usual and get($id) which can has the behavior different from your preferred library. There is the method set($id, $value) which does exactly the same it means.

get($id)

The method responds with a primitive with the only one exception - Closures. In case of Closure the library resolves it with the injection of it's dependencies. The Closure may to return any primitive or an instance of any class you wish. This method caches results of the resolving.

<?php

$container = new \Anonymous\SimpleDi\Container([
    'primitive' => 42,
    'wrapped-primitive' => function (\Psr\Container\ContainerInterface $c) {
        return (string)$c->get('primitive');
    },
]);

var_dump($container->get('primitive'), $container->get('wrapped-primitive'));

// int(42)
// string(2) "42"

instantiate($id, array $arguments = [], $instanceOf = null)

This method creates an instance of the certain class. It tries to resolve the definition or instantiate provided class in case of definition absence. The method creates the new one instance on each call. In case of closure as argument instantiate resolves it each time too.

<?php

interface I {}
class A implements I {}
class B implements I {}

$container = new \Anonymous\SimpleDi\Container([
    I::class => A::class,
    B::class => function () {
        return new B();
    },
]);

var_dump(
    $container->instantiate(I::class),
    $container->instantiate(A::class),
    $container->instantiate(B::class, [], I::class)
);

$container->instantiate('C');

/*

object(A)#4 (0) {
}
object(A)#5 (0) {
}
object(B)#7 (0) {
}
PHP Fatal error:  Uncaught Anonymous\SimpleDi\FactoryException Unresolvable dependency 'C'

*/

make($id, array $arguments = [], $recreate = false)

Almost the same as previous but with the cache. It means you will get the same instance on each method call. If method make will be called after instantiate using the same $id you will get already cached instance of class you provided.

<?php

class A {}

$container = new \Anonymous\SimpleDi\Container();

var_dump(
    $container->instantiate(A::class),
    $container->make(A::class)
);

/*

object(A)#2 (0) {
}
object(A)#2 (0) {
}

*/

injectOn($callable, array $arguments = [])

Injects arguments to the method of provided instance and calls it. If array of arguments doesn't contain all variables which called method wait for the library tries to resolve them. Notice: Closure is an object with the method __invoke so you can use injectOn on it.

<?php

class A {
    public function e($v) {
        return $v;
    }
}

class B { 
    public function strtoupper($v) {
        return strtoupper($v);
    }
}

$container = new \Anonymous\SimpleDi\Container();

var_dump(
    $container->injectOn([new A(), 'e'], ['v' => 'value1']),
    $container->injectOn(function (B $b, $v) { return $b->strtoupper($v); }, ['v' => 'value2'])
);

// string(5) "value1"
// string(5) "VALUE2"

call($callable, array $arguments = [])

Almost the same as injectOn but provides the possibility to resolve and instantiate provided class.

<?php

interface Filter
{
    public function __invoke($v);
}

class Upper implements Filter
{
    public function __invoke($v)
    {
        return strtoupper($v);
    }
}

interface Output
{
    public function __invoke($v);
}

class StdOutput implements Output
{
    public function __invoke($v)
    {
        echo $v, PHP_EOL;
    }
}

class Printer
{
    protected $filter;

    public function __construct(Filter $filter)
    {
        $this->filter = $filter;
    }

    public function out(Output $output, $value)
    {
        $filter = $this->filter;
        $output($filter instanceof Filter ? $filter($value) : $value);
    }
}

$container = new \Anonymous\SimpleDi\Container([
    Output::class => StdOutput::class,
    Filter::class => Upper::class,
]);

$container->call([Printer::class, 'out'], ['value' => 'Text to print 1']);
$container->call('Printer::out', ['value' => 'Text to print 2']);

// TEXT TO PRINT 1
// TEXT TO PRINT 2

Todo:

  • Documentation
  • Tests
  • Performance measurement

anonymous-php/simple-di 适用场景与选型建议

anonymous-php/simple-di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-02