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
Requires
- php: >= 7.1
- doctrine/annotations: ^1.6
- symfony/config: ^3.4 | ^4.0
- symfony/dependency-injection: ^3.4 | ^4.0
- symfony/finder: ^3.4 | ^4.0
- symfony/http-kernel: ^3.4 | ^4.0
Requires (Dev)
- phpunit/phpunit: ^7.4
- roave/security-advisories: dev-master
README
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