2017 © Pedro Peláez
 

library service-aware-bundle

Automated service injection for symfony.

image

bit3/service-aware-bundle

Automated service injection for symfony.

  • Sunday, September 25, 2016
  • by tril
  • Repository
  • 1 Watchers
  • 3 Stars
  • 68 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Version Stable Build Status Upstream Build Status License Downloads, (*1)

Service Aware Bundle

Create services with dependencies may result in a lot of duplicated meta code. For example:, (*2)

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:, (*3)

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., (*4)

How to use

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

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:, (*6)

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?, (*7)

Define service aware's

You can define your own service aware interfaces in your app/config/config.yml., (*8)

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

The Versions