bit3/service-aware-bundle

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.
This package is abandoned and no longer maintained. No replacement package was suggested.

Automated service injection for symfony.

dev-develop / 1.1.x-dev 2016-09-25 11:57 UTC

This package is not auto-updated.

Last update: 2019-02-20 18:17:41 UTC


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"