Developed with love by KnpLabs Hire us for your project!
27

autowiring-bundle

by skrz

Autowiring + automatic service discovery for Symfony DI container

Skrz\Bundle\AutowiringBundle

Build Status
Quality Score
Code Coverage
Downloads this Month
Latest stable

Annotation-based autowiring for Symfony 4 dependency injection container

Installation

Add as Composer dependency:

$ composer require skrz/autowiring-bundle

Then add SkrzAutowiringBundle to Symfony Kernel:

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.:

use Skrz\Bundle\AutowiringBundle\Annotation\Controller;

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

Create your own application stereotypes by subclassing @Component.

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.

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.

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:

// 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.

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.

Property parameter injection

You can also inject container parameters using @Value annotation.

// 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.

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.

// 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.

Copyright (c) 2015-2018 Jakub Kulhan <jakub.kulhan@gmail.com>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
  • Merge pull request #10 from jakubkulhan/sf4
    By web-flow, 1 year ago
  • removed name/env setting from @Component annotation as they served only autoscanning
    By , 1 year ago
  • added scalar typehints to methods
    By jakubkulhan, 1 year ago
  • symfony 4.0 support, dropped autoscanning
    By jakubkulhan, 1 year ago
  • Merge pull request #9 from klatys/sf3.3-plus
    By web-flow, 1 year ago
  • drop 5.4 and hhvm support
    By klatys, 1 year ago
  • add SF3.3+ DI changes support
    By klatys, 1 year ago
  • Merge pull request #6 from jakubkulhan/autoscan-private
    By Jan Klat, 3 years ago
  • Merge pull request #5 from jakubkulhan/symfony-3.0
    By mzstic, 3 years ago
  • autoscan: private classes
    By jakubkulhan, 3 years ago
  • symfony 3.0
    By jakubkulhan, 3 years ago
  • Merge pull request #4 from Rastusik/master
    By jakubkulhan, 3 years ago
  • ability to typehint just selected parameters (interfaces) using services.yml, ability to inject private properties
    By , 3 years ago
  • Merge remote-tracking branch 'TomasVotruba/tests' (closes #1, #2)
    By jakubkulhan, 3 years ago
  • cs fixes, allow PHP 5.4
    By TomasVotruba, 3 years ago
  • file docs added
    By TomasVotruba, 3 years ago
  • phpunit: testsuite added
    By TomasVotruba, 3 years ago
  • Tests: @autowired annotation on property test added
    By TomasVotruba, 3 years ago
  • travis: fix coverage path
    By TomasVotruba, 3 years ago
  • scrutinizer added
    By TomasVotruba, 3 years ago
  • travis: PHP 7 works, no need to skip
    By TomasVotruba, 3 years ago
  • travis: cache added
    By TomasVotruba, 3 years ago
  • Readme: Travis badge added
    By TomasVotruba, 3 years ago
  • composer: bump to PHP 5.5, missing HttpKernel added
    By TomasVotruba, 3 years ago
  • Tests: init
    By TomasVotruba, 3 years ago
  • syntax fixed, fqn naming, sprintf
    By TomasVotruba, 3 years ago
  • use standard extension name
    By TomasVotruba, 3 years ago
  • respect Bundle standard: move files to particular dirs
    By TomasVotruba, 3 years ago
  • composer: cleanup
    By TomasVotruba, 3 years ago
  • README: cleanup, syntax highlighting
    By TomasVotruba, 3 years ago