Skip to content

Latest commit

 

History

History
64 lines (47 loc) · 2.2 KB

dependency-injection.md

File metadata and controls

64 lines (47 loc) · 2.2 KB

Dependency injection

All services in this package are stored into a service container that enables dependency injection. This makes it relatively easy to add more services or configure existing services.

Configuration

The library provides service configuration files. With those files, the resulting service container is configured in a way that it's usable across the whole library and potential external libraries. The following files are configured:

Extending the service configuration

You can also extend the service container by additional service configuration files. This can be achieved by adding a services array to your assets configuration file:

{
    "frontend-assets": [
        // ...
    ],
    "services": [
        "/path/to/custom/services.yaml",
        "/path/to/custom/services.php"
    ]
}

Note

Only yaml and php files are supported. Take a look at the official Symfony documentation to get an overview about service configuration.

Service container

All services are stored in a service container that is built and cached during runtime. In order to manually build the container, you can run the following:

use CPSIT\FrontendAssetHandler\DependencyInjection;

// Declare config file
$configFile = '/path/to/assets.json';

// Get services from container
$containerFactory = new DependencyInjection\ContainerFactory($configFile);
$container = $containerFactory->get();

Warning

You must specify the path to a valid assets configuration file. Otherwise, a failsafe container will be created which can only be used for some low-level commands (see the following section).

Failsafe container

If the config file parameter is omitted, a failsafe container is built. This container is mainly used in the command application and testing environment. It should not be used anywhere else.