承接 bigbit/oddin 相关项目开发

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

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

bigbit/oddin

Composer 安装命令:

composer require bigbit/oddin

包简介

On Demand Dependency INjection

README 文档

README

About

Sometimes coding is a pain. Your boss needs it now or simply you are bored writing all sugar again and again and ...

If you are using DI in your projects, you have to write property declarations and initialize them in constructors. You can use dependency container or define injectable constructor arguments. It depends on framework used. For of PHP7.4.0 only, 7.4.1 breaks functionality, php bug #78904.

class Foo {
    private Dependency1 $dep1;
    
    private Dependency2 $dep2;
    
    private Depencency3 $dep3;
    
    function __construct(Dependency1 $dep1, Dependency2 $dep2, Container $container)
    {
        $this->dep1 = $dep1;
        $this->dep2 = $dep2;
        $this->dep3 = $container->get(Dependency3::class);
    }
    
    public function bar() 
    {
        $this->dep1->doSomething();
    }
    
    protected function baz()
    {
        $this->dep2->doSomethind();
    }
    
    private function bye() 
    {
        $this->dep3->doSomething();
    }
}

With ODDIN you can skip constructor part. Just declare properties and access properties anytime you need. Keep in mind those properties becomes accessible from anywhere by magic __get method. Your preferred IDE will help you deal with that problem

class Foo {
    use InjectsOnDemand;
    
    private Dependency1 $dep1;
    
    private Dependency2 $dep2;
    
    private Depencency3 $dep3;

    /**
    * Foo constructor.
    * as of php7.4.1, we have to unset properties in constructor
    * https://bugs.php.net/bug.php?id=78904 
    */
    public function __construct() 
    {
        unset($this->dep1, $this->dep2, $this->dep3);
    }

    public function bar() 
    {
        $this->dep1->doSomething();
    }
    
    private function baz() 
    {
        $this->dep2->doSomething();
    }
    
    private function bye()
    {
        $this->dep3->doSomethind();
    }
}

How it works

PHP classes can have magic methods. The __get magic method is invoked every time you want to use unset property. DIResolver uses parser to get dependency metadata from class or deprecated property annotations. InjectsOnDemand trait defines magic __get method, which handles all our property requests. Once property is initialized by trait, magic method is not called again.

Pros

  • less coding
  • dependency instantiation on demand (lazy - not before constructor, if properly defined in DI container)

Cons

  • all properties becomes public ? all injectables are "public"
  • antipattern ? use it only for prototyping, clean the code later.

Purpouse

Cleaner controller classes, less resource demanding. But it's up to you, where you use ODDIN.

Known Issues

  • no code fixer yet

Quick start

You can use any DI container, which implements Psr\Container\ContainerInterface. For quick start, you can use Bootstrap class, which uses SmartContainer.

use BigBIT\DIBootstrap\Bootstrap;
use Psr\Container\ContainerInterface;

// custom bindings
$bindings = [
    FooInterface::class => function(ContainerInterface $container) {
        return new BarImplementation(
                $container->get(BazDependency::class)
            );
        }
];

$container = Bootstrap::getContainer($bindings);

$app = new SomeApp($container);

$app->run();

Other frameworks

You can request other frameworks support or write you own bootstrap based on Bootstrap class.

PHP-DI comparison

@todo

Cache Generator

Experimental implementation of cache generator was added. If your project has phpstan installed, it's recommended to install tracy/tracy as well. Oddin uses Psr\SimpleCache\CacheInterface implementations and Symfony as default.

Creating cache is not necessary, but recommended for production environments:

vendor/bin/oddin cache:injectables:create php-files -a oddin -a 0 -a cache

Arguments for cli commands are derived from adapter constructor.

Instantiating cache:

$bindings[CacheInterface::class] = function(ContainerInterface $container) {
    return new Psr16Cache(new PhpFilesAdapter('oddin', 0, dirname(__DIR__) . '/cache'));
};

@TODO - Code Fixer

Cli command for fixing code. It will remove class property annotations, declare properties and add constructor or getters and container to constructor.

bigbit/oddin 适用场景与选型建议

bigbit/oddin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 bigbit/oddin 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-12