skrz/autowiring-bundle 问题修复 & 功能扩展

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

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

skrz/autowiring-bundle

Composer 安装命令:

composer require skrz/autowiring-bundle

包简介

Annotation-based autowiring for Symfony 4 dependency injection container

README 文档

README

Build Status Quality Score Code Coverage Downloads this Month Latest stable

Annotation-based autowiring for Symfony 4 dependency injection container

Installation

Add as Composer dependency:

$ composer require skrz/autowiring-bundle

Then add SkrzAutowiringBundle to Symfony Kernel:

use Skrz\Bundle\AutowiringBundle\SkrzAutowiringBundle;

class AppKernel
{

    public function registerBundles()
    {
        return [
            ...
            new SkrzAutowiringBundle()
            ...
        ];
    }

}

Usage

Annotate your application components using @Component annotation and its subclasses, or so called "stereotypes". Predefined stereotypes are @Controller, @Repository, and @Service, e.g.:

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;

/**
 * @Controller
 */
class HomepageController
{
    ...
}

Create your own application stereotypes by subclassing @Component.

Constructor dependency injection

// services.yml
services:
  Example\HomepageController: ~
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var SomeService
     */
    private $someService;

    public function __construct(SomeService $someService)
    {
        $this->someService = $someService;
    }

    ...

}

SomeService is automatically injected into HomepageController instance during creation in container.

Note that constructor is ALWAYS autowired if there is not enough arguments specified in services.yml. If you really do not want the constructor to be autowired, add the service to ignored_services configuration directive.

Note: if you need to specify some of the constructor arguments and autowire other constructor aurguments, you need to configure your service the following way:

// services.yml
services:
  Example\HomepageController:
    arguments: 
      someParameter: %kernel.whatever%
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var SomeService
     */
    private $someService;
    
    /**
     * @var string
     */
    private $someParameter;

    public function __construct(SomeService $someService, $someParameter)
    {
        $this->someService = $someService;
        $this->someParameter = $someParameter;
    }

    ...

}

The $someService argument is autowired and the $someParameter argument is injected depending on the configuration.

Method dependency injection

// services.yml

services:
  Example\HomepageController: ~
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;
use Skrz\Bundle\AutowiringBundle\Annotation\Autowired;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var SomeService
     */
    private $someService;

    /**
     * @param SomeService $someService
     * @return void
     *
     * @Autowired
     */
    public function setSomeService(SomeService $someService)
    {
        $this->someService = $someService;
    }

    ...

}

Property dependency injection

// services.yml

services:
  Example\HomepageController: ~
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;
use Skrz\Bundle\AutowiringBundle\Annotation\Autowired;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var SomeService
     *
     * @Autowired
     */
    public $someService;

    ...

}

Note: using property dependency injection is quite addictive.

Property parameter injection

You can also inject container parameters using @Value annotation.

// services.yml

services:
  Example\HomepageController: ~
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;
use Skrz\Bundle\AutowiringBundle\Annotation\Value;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var string
     *
     * @Value("%kernel.root_dir%")
     */
    public $rootDir;

    ...

}

Pro-Tip: inject always scalar values, do not inject arrays. When you inject scalar values, their presence in container is validated during container compilation.

Autoscan

Autoscan was a feature of version 1.x of SkrzAutowiringBundle. However, since Symfony 4.0, container supports this feature natively. Therefore, it was removed from the bundle and you should use resource key to import directories of services.

// services.yml

services:
  Example\:
    resource: "../path/to/controllers/*Controller.php"
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;
use Skrz\Bundle\AutowiringBundle\Annotation\Autowired;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var SomeService
     *
     * @Autowired
     */
    public $someService;

    ...

}

Configuration

# container extension key is "autowiring"
autowiring:

  # these service IDs won't be processed by autowiring
  ignored_services:
    # either specify exact service IDs
    - kernel
    - http_kernel

    # or use regular expressions (they must start with "/")
    - /^debug\./
    - /^file/

  # match interfaces to exact services
  preferred_services:
    Psr\Log\LoggerInterface: logger
    Monolog\Logger: logger
    Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface: session.storage.native

  # if you create your own stereotypes, you must add then here
  fast_annotation_checks: [ @Task, @Widget ]

License

The MIT license. See LICENSE file.

skrz/autowiring-bundle 适用场景与选型建议

skrz/autowiring-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 75k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2015 年 05 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 skrz/autowiring-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-25