PHPackages                             aatis/dependency-injection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. aatis/dependency-injection

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

aatis/dependency-injection
==========================

Dependency Injection of Aatis

2.0.0(9mo ago)03014PHPPHP &gt;=8.2

Since Nov 11Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/BatMaxou/aatis-dependency-injection)[ Packagist](https://packagist.org/packages/aatis/dependency-injection)[ RSS](/packages/aatis-dependency-injection/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (20)Used By (4)

Aatis Dependency Injection
==========================

[](#aatis-dependency-injection)

Installation
------------

[](#installation)

```
composer require aatis/dependency-injection
```

Dependencies
------------

[](#dependencies)

- `aatis/parameter-bag` ()
- `aatis/tag` ()

Usage
-----

[](#usage)

### Requirements

[](#requirements)

Set the environment variable `APP_ENV` to the name of the environment you want to use.

Create the container builder with the context of your app (`$_SERVER`).

```
(new ContainerBuilder($ctx))->build();
```

### Exclude files

[](#exclude-files)

Precise the files that are not services.

```
# In config/services.yaml file :

exclude_paths:
  - "/Folder"
  - "/OtherFolder/file.txt"
  -
```

```
// Directly in PHP code when building the container :

(new ContainerBuilder($ctx))
    ->excludePaths('/Folder')
    ->excludePaths('/OtherFolder/file.txt')
    ->build();
```

### Service config

[](#service-config)

You can manage in which environment your service must be loaded and the arguments to pass to the constructor.

Finally, you can give extra tags to any service.

```
# In config/services.yaml file :

services:
    Namespace\Of\Service:
        environment:
            - "env_name1"
            - "env_name2"
            -
        arguments:
            variable_name: "value"
        tags:
            - "tag_name_1"
            - { tag: "tag_name_2", priority: 10 }
            -
```

```
// Directly in PHP code when building the container :

(new ContainerBuilder($ctx))
    ->register(Service::class, [
        'environment' => ['env_name1', 'env_name2'],
        'arguments' => [
            'variable_name' => 'value',
        ],
        'tags' => [
            'tag_name_1',
            ['tag' => 'tag_name_2', 'priority' => 10],
        ],
    ])
    ->build();
```

Note

The key of an argument must have the same name as in the constructor

Note

Tags have priority set to 0 by default. You can set it to any number you want. Services will be sorted by highest priority first when the `Container` return multiple services.

Note

It is also possible to define a configuration for an abstract class. This configuration will be used for all the services extending this class, and will be merged with the configuration of the service itself if provided.

### Interface into constructor

[](#interface-into-constructor)

When an interface is requested into the constructor of a service, the `Container` will try to find a service implementing this interface into your app.

Note

If multiple services implement the interface, the `Container` will pick the one with the highest priority. If many services implementing the interface share the highest priority, the `Container` will priorise an already instancied service. Otherwise, it will pick the first one found.

If you want to use a specific service, don't forget to declare it into the configuration of the service.

```
# In config/services.yaml file :

services:
    Namespace\Of\Service:
        arguments:
            variable_name: Namespace\Of\Service\Wanted\With\The\Interface
```

Warning

If your want to use a specific service of the vendor, do the previous step and precise it into the `includes_services` part of the config.

```
# In config/services.yaml file :

include_services:
    - Namespace\Of\The\Vendor\Service\Implementing\The\Interface
```

### Env variable into constructor

[](#env-variable-into-constructor)

You can request for a env variable directly into the constructor of a service.

```
public function __construct(string $_env_var)
{
    // ...
}
```

Note

The name of the variable must start with $\_ and be followed by the env variable name in lowercase.

### Container uses

[](#container-uses)

#### Get and Set

[](#get-and-set)

With the container, you can get and set any service / env variable (prefixed by `@_`) you want with the methods `get()` and `set()`.

However, to set a service, you must give an instance of the `Service` class. You can create it with the `ServiceFactory` service.

```
// Env Variable
$container->get('@_ENV_VAR_NAME');
$container->set('@_ENV_VAR_NAME', 'value');

// Service
$container->get(Service::class);

$service = $container->get(ServiceFactory::class)->create(Service::class);
$container->set(Service::class, $service);
```

#### Get by tag

[](#get-by-tag)

You can get services by tag using the `ServiceTagBuilder`:

```
$tagBuilder = $container->get(ServiceTagBuilder::class);

// returns the instance of the services tagged
$taggedServiceInstances = $container->get($tagBuilder->buildFromName('tag_name_1'));

// returns the Service component instances of the services tagged
$taggedServices = $container->get($tagBuilder->buildFromName('tag_name_1', [ServiceTagOption::SERVICE_TARGETED]));
```

#### Get by interface

[](#get-by-interface)

You can easily get services implementing an interface using `ServiceTagBuilder` with the `buildFromInterface()` method.

```
$tagBuilder = $container->get(ServiceTagBuilder::class);
$taggedServiceInstances = $container->get($tagBuilder->buildFromInterface(Interface::class));
```

#### Get single service

[](#get-single-service)

You can get the `Service` of a single service using `ServiceTagOption::FROM_CLASS` and `ServiceTagOption::SERVICE_TARGETED` options.

```
$tagBuilder = $container->get(ServiceTagBuilder::class);

// returns the Service component instance of the service targeted
$service = $container->get($tagBuilder->buildFromName(Service::class, [ServiceTagOption::FROM_CLASS, ServiceTagOption::SERVICE_TARGETED]));

// returns the instance of the service targeted (same as $container->get(Service::class))
$serviceInstance = $container->get($tagBuilder->buildFromName(Service::class, [ServiceTagOption::FROM_CLASS]));
```

### AsDefaultTaggedService

[](#asdefaulttaggedservice)

As said before, the priority of a tag is set to 0 by default, but when attaching the attribute `AsDefaultTaggedService` to a service, you can set the priority to some or all of his tags to 1.

```
