cleancode/injectable-bundle 问题修复 & 功能扩展

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

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

cleancode/injectable-bundle

Composer 安装命令:

composer require cleancode/injectable-bundle

包简介

Injectable-bundle is responsible for injecting array of objects into your class. By tagging you mark an service to be injected into your target service.

README 文档

README

Description

Injectable-bundle is responsible for injecting array of objects into your class. By tagging you mark an service to be injected into your target service.

Example use JMSDiExtraBundle, but you can easily configure it with services.yml

Target Service
/**
* @DI\Service("template_factory")
*/
class TemplateFactory
{
    /**
    * @param array|Template[] $providers
    */
    private $providers;

    /**
     * @DI\InjectParams({
     *      "providers" = @DI\Inject("%empty_array%")
     * })
     */
    public function __construct(array $providers)
    {
        $this->providers = $providers;
    }
    
    public function createFor(string $type) : Template
    {
        foreach ($this->providers as $provider) {
            if ($provider->isHandling($type)) {
                return $provider;
            }
        }
        
        throw new \Exception("Template for {$type} doesn't exists!");
    }
}

interface Template
{
    public function render() : string
    
    public function isHandling(string $type) : bool;
}
Injected Services
/**
 * @DI\Service()
 * @DI\Tag(name="injectable", attributes={"to"="template_factory", "index"="0"})
 */
class NiceTemplate implements Template
{
    public function render()
   {
        return "nice template";
   }
   
   public function isHandling(string $type) : bool
   {
        return $type === 'nice';
   }
}

/**
 * @DI\Service()
 * @DI\Tag(name="injectable", attributes={"to"="template_factory", "index"="0"})
 */
class UglyTemplate implements Template
{
    public function render()
   {
        return "ugly template";
   }
   
  public function isHandling(string $type) : bool
  {
       return $type === 'ugly';
  }
}

Above service will be injected into TemplateFactory.
If there will be no services tagged as injectable for template_factory, template factory will use of empty array.

Understanding the annotations

@DI\Tag(name="injectable", attributes={"to"="template_factory", "index"="0"})
  • name="injectable" - Marks service to be used by InjectableBundle
  • "to"="template_factory" - Sets target service
  • "index"="0" - Sets target service constructor argument index

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固