Skip to content
This repository has been archived by the owner on Dec 27, 2017. It is now read-only.

[PHP / SF2] Symfony bundle to automatically inject services.

License

Notifications You must be signed in to change notification settings

bit3archive/service-aware-bundle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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"

About

[PHP / SF2] Symfony bundle to automatically inject services.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages