2017 © Pedro PelĂĄez
 

library autowiring-bundle

Annotation-based autowiring for Symfony 4 dependency injection container

image

skrz/autowiring-bundle

Annotation-based autowiring for Symfony 4 dependency injection container

  • Wednesday, February 21, 2018
  • by jakubkulhan
  • Repository
  • 3 Watchers
  • 6 Stars
  • 38,002 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 5 Forks
  • 3 Open issues
  • 9 Versions
  • 9 % Grown

The README.md

Skrz\Bundle\AutowiringBundle

Build Status Quality Score Code Coverage Downloads this Month Latest stable, (*1)

Annotation-based autowiring for Symfony 4 dependency injection container, (*2)

Installation

Add as Composer dependency:, (*3)

$ composer require skrz/autowiring-bundle

Then add SkrzAutowiringBundle to Symfony Kernel:, (*4)

use Skrz\Bundle\AutowiringBundle\SkrzAutowiringBundle;

class AppKernel
{

    public function registerBundles()
    {
        return [
            ...
            new SkrzAutowiringBundle()
            ...
        ];
    }

}

Usage

Annotate your application components using @Component annotation and its subclasses, or so called "stereotypes". Predefined stereotypes are @Controller, @Repository, and @Service, e.g.:, (*5)

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;

/**
 * @Controller
 */
class HomepageController
{
    ...
}

Create your own application stereotypes by subclassing @Component., (*6)

Constructor dependency injection

// services.yml
services:
  Example\HomepageController: ~
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var SomeService
     */
    private $someService;

    public function __construct(SomeService $someService)
    {
        $this->someService = $someService;
    }

    ...

}

SomeService is automatically injected into HomepageController instance during creation in container., (*7)

Note that constructor is ALWAYS autowired if there is not enough arguments specified in services.yml. If you really do not want the constructor to be autowired, add the service to ignored_services configuration directive., (*8)

Note: if you need to specify some of the constructor arguments and autowire other constructor aurguments, you need to configure your service the following way:, (*9)

// services.yml
services:
  Example\HomepageController:
    arguments: 
      someParameter: %kernel.whatever%
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var SomeService
     */
    private $someService;

    /**
     * @var string
     */
    private $someParameter;

    public function __construct(SomeService $someService, $someParameter)
    {
        $this->someService = $someService;
        $this->someParameter = $someParameter;
    }

    ...

}

The $someService argument is autowired and the $someParameter argument is injected depending on the configuration., (*10)

Method dependency injection

// services.yml

services:
  Example\HomepageController: ~
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;
use Skrz\Bundle\AutowiringBundle\Annotation\Autowired;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var SomeService
     */
    private $someService;

    /**
     * @param SomeService $someService
     * @return void
     *
     * @Autowired
     */
    public function setSomeService(SomeService $someService)
    {
        $this->someService = $someService;
    }

    ...

}

Property dependency injection

// services.yml

services:
  Example\HomepageController: ~
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;
use Skrz\Bundle\AutowiringBundle\Annotation\Autowired;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var SomeService
     *
     * @Autowired
     */
    public $someService;

    ...

}

Note: using property dependency injection is quite addictive., (*11)

Property parameter injection

You can also inject container parameters using @Value annotation., (*12)

// services.yml

services:
  Example\HomepageController: ~
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;
use Skrz\Bundle\AutowiringBundle\Annotation\Value;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var string
     *
     * @Value("%kernel.root_dir%")
     */
    public $rootDir;

    ...

}

Pro-Tip: inject always scalar values, do not inject arrays. When you inject scalar values, their presence in container is validated during container compilation., (*13)

Autoscan

Autoscan was a feature of version 1.x of SkrzAutowiringBundle. However, since Symfony 4.0, container supports this feature natively. Therefore, it was removed from the bundle and you should use resource key to import directories of services., (*14)

// services.yml

services:
  Example\:
    resource: "../path/to/controllers/*Controller.php"
namespace Example;

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;
use Skrz\Bundle\AutowiringBundle\Annotation\Autowired;

/**
 * @Controller
 */
class HomepageController
{

    /**
     * @var SomeService
     *
     * @Autowired
     */
    public $someService;

    ...

}

Configuration

# container extension key is "autowiring"
autowiring:

  # these service IDs won't be processed by autowiring
  ignored_services:
    # either specify exact service IDs
    - kernel
    - http_kernel

    # or use regular expressions (they must start with "/")
    - /^debug\./
    - /^file/

  # match interfaces to exact services
  preferred_services:
    Psr\Log\LoggerInterface: logger
    Monolog\Logger: logger
    Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface: session.storage.native

  # if you create your own stereotypes, you must add then here
  fast_annotation_checks: [ @Task, @Widget ]

License

The MIT license. See LICENSE file., (*15)

The Versions

21/02 2018

dev-master

9999999-dev

Annotation-based autowiring for Symfony 4 dependency injection container

  Sources   Download

MIT

The Requires

 

The Development Requires

21/02 2018

v2.0.0

2.0.0.0

Annotation-based autowiring for Symfony 4 dependency injection container

  Sources   Download

MIT

The Requires

 

The Development Requires

05/12 2017

v1.3.0

1.3.0.0

Symfony bundle providing autowiring/autoscanning capabilities to DI container

  Sources   Download

MIT

The Requires

 

The Development Requires

05/12 2017

dev-dev-symfony-3_4

dev-dev-symfony-3_4

Symfony bundle providing autowiring/autoscanning capabilities to DI container

  Sources   Download

MIT

The Requires

 

08/04 2016

v1.2.2

1.2.2.0

Symfony bundle providing autowiring/autoscanning capabilities to DI container

  Sources   Download

MIT

The Requires

 

18/02 2016

v1.2.1

1.2.1.0

Symfony bundle providing autowiring/autoscanning capabilities to DI container

  Sources   Download

MIT

The Requires

 

22/07 2015

v1.2.0

1.2.0.0

Symfony bundle providing autowiring/autoscanning capabilities to DI container

  Sources   Download

MIT

The Requires

 

01/06 2015

v1.1.0

1.1.0.0

Symfony bundle providing autowiring/autoscanning capabilities to DI container

  Sources   Download

MIT

The Requires

 

25/05 2015

v1.0.0

1.0.0.0

Symfony bundle providing autowiring/autoscanning capabilities to DI container

  Sources   Download

MIT

The Requires