定制 bit3/service-aware-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

bit3/service-aware-bundle

Composer 安装命令:

composer require bit3/service-aware-bundle

包简介

Automated service injection for symfony.

关键字:

README 文档

README

Version Stable Build Status Upstream Build Status License Downloads

Service Aware Bundle

Create services with dependencies may result in a lot of duplicated meta code. For example:

service:
    service_foo:
        class: Acme\DemoBundle\Service\Foo
        calls:
            - [setEntityManager, [@doctrine.orm.default_entity_manager]]
            - [setTranslator, [@translator]]
            
    service_bar:
        class: Acme\DemoBundle\Service\Bar
        calls:
            - [setTranslator, [@translator]]
            - [setValidator, [@validator]]
            
    service_zap:
        class: Acme\DemoBundle\Service\Zap
        calls:
            - [setEntityManager, [@doctrine.orm.default_entity_manager]]
            - [setTranslator, [@translator]]
            - [setValidator, [@validator]]

And in the classes:

namespace Acme\DemoBundle\Service;

class Foo {
    private $entityManager;
    private $translator;
    
    public function setEntityManager($entityManager) {
        $this->entityManager = $entityManager;
    }
    
    public function setTranslator($translator) {
        $this->translator = $translator;
    }
}
namespace Acme\DemoBundle\Service;

class Bar {
    private $translator;
    private $validator;
    
    public function setTranslator($translator) {
        $this->translator = $translator;
    }
    
    public function setValidator($validator) {
        $this->validator = $validator;
    }
}
namespace Acme\DemoBundle\Service;

class Zap {
    private $entityManager;
    private $translator;
    private $validator;
    
    public function setEntityManager($entityManager) {
        $this->entityManager = $entityManager;
    }
    
    public function setTranslator($translator) {
        $this->translator = $translator;
    }
    
    public function setValidator($validator) {
        $this->validator = $validator;
    }
}

This bundle help you to avoid to define all the setters calls and implementations. It provide a lot of *Aware interfaces, abstract base classes and traits.

How to use

Using the bundle is simple, you only need to implement the interfaces and remove the setter calls from the services.yml.

service:
    service_foo:
        class: Acme\DemoBundle\Service\Foo
            
    service_bar:
        class: Acme\DemoBundle\Service\Bar
            
    service_zap:
        class: Acme\DemoBundle\Service\Zap

And in the classes:

namespace Acme\DemoBundle\Service;

use Bit3\Symfony\ServiceAwareBundle\Doctrine\DoctrineBundle\EntityManagerAwareInterface;
use Bit3\Symfony\ServiceAwareBundle\Doctrine\DoctrineBundle\EntityManagerAwareTrait;
use Bit3\Symfony\ServiceAwareBundle\Symfony\FrameworkBundle\Translator\TranslatorAwareInterface;
use Bit3\Symfony\ServiceAwareBundle\Symfony\FrameworkBundle\Translator\TranslatorAwareTrait;

class Foo implements EntityManagerAwareInterface, TranslatorAwareInterface {
    use EntityManagerAwareTrait;
    use TranslatorAwareTrait;
}
namespace Acme\DemoBundle\Service;

use Bit3\Symfony\ServiceAwareBundle\Symfony\FrameworkBundle\Translator\TranslatorAwareInterface;
use Bit3\Symfony\ServiceAwareBundle\Symfony\FrameworkBundle\Translator\TranslatorAwareTrait;
use Bit3\Symfony\ServiceAwareBundle\Symfony\FrameworkBundle\Validator\ValidatorAwareInterface;
use Bit3\Symfony\ServiceAwareBundle\Symfony\FrameworkBundle\Validator\ValidatorAwareTrait;

class Bar implements EntityManagerAwareInterface, TranslatorAwareInterface, ValidatorAwareInterface {
    use TranslatorAwareTrait;
    use ValidatorAwareTrait;
}
namespace Acme\DemoBundle\Service;

use Bit3\Symfony\ServiceAwareBundle\Doctrine\DoctrineBundle\EntityManagerAwareInterface;
use Bit3\Symfony\ServiceAwareBundle\Doctrine\DoctrineBundle\EntityManagerAwareTrait;
use Bit3\Symfony\ServiceAwareBundle\Symfony\FrameworkBundle\Translator\TranslatorAwareInterface;
use Bit3\Symfony\ServiceAwareBundle\Symfony\FrameworkBundle\Translator\TranslatorAwareTrait;
use Bit3\Symfony\ServiceAwareBundle\Symfony\FrameworkBundle\Validator\ValidatorAwareInterface;
use Bit3\Symfony\ServiceAwareBundle\Symfony\FrameworkBundle\Validator\ValidatorAwareTrait;

class Zap implements EntityManagerAwareInterface, TranslatorAwareInterface, ValidatorAwareInterface {
    use EntityManagerAwareTrait;
    use TranslatorAwareTrait;
    use ValidatorAwareTrait;
}

Pretty simple, huh?

Define service aware's

You can define your own service aware interfaces in your app/config/config.yml.

service_aware:
  services:
    acme_demo_bundle.services.service_foo:
      interface: "Acme\DemoBundle\Service\FooAwareInterface"
      method:    "setServiceFoo"
      service:   "acme_demo_bundle.service_foo"

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-11-11