incompass/injection-bundle

Inject with Annotations for Symfony

Installs: 2 725

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 9

Forks: 0

Type:symfony-bundle

1.4.0 2019-04-10 20:57 UTC

This package is auto-updated.

Last update: 2023-10-11 13:28:04 UTC


README

Build Status Latest Stable Version Scrutinizer Code Quality Code Coverage

Prerequisites

This bundle requires symfony 3.4+ or 4.0+

Installation

Install with composer

composer require incompass/injection-bundle

Enable the bundle

In bundles.php add

return [
    // ...
    Incompass\InjectionBundle\InjectionBundle::class => ['all' => true]
    // ...
];

Usage

By default, the bundle looks in src for services to inject with a base namespace of App. To change that, add a configuration like the following:

$c->loadFromExtension('injection', [
    'paths' => [
        'code' => 'MyApp\\'
    ]
]);

To configure environment groups, add something like to the following do your bundle configuration:

$c->loadFromExtension('injection', [
    'environment_groups' => [
        [
            'group' => 'all',
            'environments' => ['dev', 'staging', 'prod']
        ],
        [
            'group' => 'prod-like',
            'environments' => ['staging', 'prod']
        ],
    ]
]);

Basic Injection

To inject a simple service that does not require any special configuration do the following:

/**
  * @Inject()
  */
class SomeService {
    // ...
}

Service ids

By default, the bundle will use the class name as the service id. If you would like to change the id, use the id parameter in the @Inject annotation:

/**
  * @Inject(id="some_service")
  */
class SomeService {
    // ...
}

Arguments

To add an argument to a service, add an argument property with @Argument annotations. Multiple arguments can be added.

/**
  * @Inject(
  *     arguments={
  *         @Argument(name="constructorParameterName", value="%parameter_name%")
  *     }
  * )
  */
class SomeService {
    // ...
}

To add a reference to another service as an argument value, preface the value with @:

/**
  * @Inject(
  *     arguments={
  *         @Argument(name="constructorParameterName", value="@SomeOtherServiceClass")
  *     }
  * )
  */
class SomeService {
    // ...
}

Tags

To tag a service, add a tag property with @Tag annotations. Multiple tags can be added.

/**
  * @Inject(
  *     tags={
  *         @Tag(name="doctrine.orm.entity_listener", attributes={
  *             "entity"=User::class,
  *             "event"=\Doctrine\ORM\Events::prePersist,
  *             "method"="prePersist"
  *         })
  * )
  */
class SomeService {
    // ...
}

Other Features not documented

  • Factories
  • Method Calls
  • Environments
  • Child Definitions
  • Aliases
  • Abstract Definitions
  • Auto Configuration
  • Auto Wiring
  • Lazy Loading
  • Public/Private
  • Shared

More documentation coming soon