2017 © Pedro Peláez
 

library injectable-bundle

Injectable-bundle is responsible for injecting array of objects into your class. By tagging you mark an service to be injected into your target service.

image

cleancode/injectable-bundle

Injectable-bundle is responsible for injecting array of objects into your class. By tagging you mark an service to be injected into your target service.

  • Sunday, February 26, 2017
  • by dgafka
  • Repository
  • 1 Watchers
  • 1 Stars
  • 64 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Injectable-Bundle

Description

Injectable-bundle is responsible for injecting array of objects into your class. By tagging you mark an service to be injected into your target service., (*1)

Example use JMSDiExtraBundle, but you can easily configure it with services.yml, (*2)

Target Service
/**
* @DI\Service("template_factory")
*/
class TemplateFactory
{
    /**
    * @param array|Template[] $providers
    */
    private $providers;

    /**
     * @DI\InjectParams({
     *      "providers" = @DI\Inject("%empty_array%")
     * })
     */
    public function __construct(array $providers)
    {
        $this->providers = $providers;
    }

    public function createFor(string $type) : Template
    {
        foreach ($this->providers as $provider) {
            if ($provider->isHandling($type)) {
                return $provider;
            }
        }

        throw new \Exception("Template for {$type} doesn't exists!");
    }
}

interface Template
{
    public function render() : string

    public function isHandling(string $type) : bool;
}
Injected Services
/**
 * @DI\Service()
 * @DI\Tag(name="injectable", attributes={"to"="template_factory", "index"="0"})
 */
class NiceTemplate implements Template
{
    public function render()
   {
        return "nice template";
   }

   public function isHandling(string $type) : bool
   {
        return $type === 'nice';
   }
}

/**
 * @DI\Service()
 * @DI\Tag(name="injectable", attributes={"to"="template_factory", "index"="0"})
 */
class UglyTemplate implements Template
{
    public function render()
   {
        return "ugly template";
   }

  public function isHandling(string $type) : bool
  {
       return $type === 'ugly';
  }
}

Above service will be injected into TemplateFactory.
If there will be no services tagged as injectable for template_factory, template factory will use of empty array., (*3)

Understanding the annotations

@DI\Tag(name="injectable", attributes={"to"="template_factory", "index"="0"})
  • name="injectable" - Marks service to be used by InjectableBundle
  • "to"="template_factory" - Sets target service
  • "index"="0" - Sets target service constructor argument index

The Versions

26/02 2017

dev-master

9999999-dev

Injectable-bundle is responsible for injecting array of objects into your class. By tagging you mark an service to be injected into your target service.

  Sources   Download

MIT

The Requires

 

by Dariusz Gafka

26/02 2017

1.0.1

1.0.1.0

Injectable-bundle is responsible for injecting array of objects into your class. By tagging you mark an service to be injected into your target service.

  Sources   Download

MIT

The Requires

 

by Dariusz Gafka

26/02 2017

1.0.0

1.0.0.0

Bundle that allow for injecting classes dynamically

  Sources   Download

MIT

The Requires

 

by Dariusz Gafka