studiow/autowire 问题修复 & 功能扩展

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

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

studiow/autowire

Composer 安装命令:

composer require studiow/autowire

包简介

Autowire-able container

README 文档

README

This package provides a sort of autowiring wrapper for any psr/container-compatible container.

Installation

The (highly) recommended way to install studiow/autowire is by using Composer

composer require studiow/autowire

How to use

In my application, I have a Configuration object. There is also an interface for objects that can use this Configuration.

class Configuration
{
    //...
}

interface HasConfiguration
{
    public function setConfiguration(Configuration $configuration);
    public function getConfiguration():Configuration
}

I like to use league/container as a DI container, so let's do just that:

$container = new League\Container\Container();
$container->share(Configuration::class);

Now I would like to make sure that our Configuration object gets passed around to any object that needs it. One way to do this is by defining all classes in the container:

class AClassWithConfiguration implements HasConfiguration{
    //...
}

$container->share(AClassWithConfiguration::class, function() use($container){
    $obj = new AClassWithConfiguration();
    $obj->setConfiguration($container->get(Configuration::class));
    return $obj;
});

This works great! Unfortunately, we'll need to this for any class that needs the Configuration object. Boring!

Let's see what we can do if we wrap our Container in an autowire container:

$container = new League\Container\Container();
$container->share(Configuration::class);

$awContainer = Studiow\Autowire\Container($container); 

This looks familiar: it's the same DI container as before, but it now gets wrapped into an autowire container.

Here's what we want the autowire container to do: if an object implements the HasConfiguration interface, use the setConfiguration method to pass on the Configuration object

$awContainer->attach(HasConfiguration::class, function($item, $awContainer){
   $item->setConfiguration($awContainer->get(Configuration::class)); 
});

The attach method takes 2 arguments: the name of the interface, and a callback. The callback also has 2 arguments: the object we're dealing with, and the autowire container.

The callback will now automatically be executed when resolving objects from the container

class AClassWithConfiguration implements HasConfiguration{
    //...
}

$container->share(AClassWithConfiguration::class});

$awContainer = Studiow\Autowire\Container($container); 
$obj = $awContainer->get(AClassWithConfiguration::class);

$obj->getConfiguration();
//Configuration object was automatically injected

If you want, you can bypass "add classes to container" too:

$obj = new AClassWithConfiguration();
$obj = $awContainer->applyCallbacks($obj);
$obj->getConfiguration();
//Configuration object was injected

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-04-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固