Built-in Containers
FastForward Container provides several built-in container classes to cover common use cases and advanced scenarios. Each built-in container has a specific role and can be composed with others for maximum flexibility.
Overview
- AggregateContainer : Aggregates multiple containers into one, resolving services in order.
- AutowireContainer : Builds concrete classes, on its own or alongside a PSR-11 delegate.
- ServiceProviderContainer : Wraps a ServiceProviderInterface as a PSR-11 container.
- ConfigContainer : Wraps a ConfigInterface as a PSR-11 container.
Details
AggregateContainer
Aggregates multiple containers. When you call get($id)
, it queries each container in order until one returns a value. Useful for composing several independent containers or layering features.
use FastForward\Container\AggregateContainer;
$container = new AggregateContainer($containerA, $containerB);
AutowireContainer
Builds concrete classes through constructor injection. Start without registrations, or pass an existing PSR-11 container to give its entries priority over autowiring:
use FastForward\Container\AutowireContainer;
$standalone = new AutowireContainer();
$container = new AutowireContainer($baseContainer);
This priority also applies to nested and optional object dependencies. The delegate
is optional; passing null
is equivalent to omitting it. See Autowire for
complete examples and parameter limits.
ServiceProviderContainer
Wraps a ServiceProviderInterface and exposes its factories and extensions as a PSR-11 container. This is the bridge between providers and the container system.
use FastForward\Container\ServiceProviderContainer;
$container = new ServiceProviderContainer($provider);
ConfigContainer
Note:
ConfigContainer is part of the fast-forward/config
library, not this package. It is fully compatible and can be used together with FastForward Container.
ConfigContainer wraps any object implementing ConfigInterface
and exposes its configuration as services in a PSR-11 container. The config object must implement the methods has($key)
and get($key)
, where keys use dot notation for nested access (e.g., 'database.host'
).
Example:
use FastForward\Config\ArrayConfig;
use FastForward\Config\Container\ConfigContainer;
$config = new ArrayConfig([
'app' => [
'env' => 'dev',
'db' => [
'host' => 'localhost',
'port' => 3306,
],
],
]);
$container = new ConfigContainer($config);
// Access config values as services:
$env = $container->get('config.app.env'); // 'dev'
$host = $container->get('config.app.db.host'); // 'localhost'
The config object must support:
$config->has('app.db.host')// returns true$config->get('app.db.host')// returns 'localhost'
This allows you to use configuration-driven service definitions and inject config values as services in your container setup.
Composing Containers
You can freely compose these containers. For example, you can wrap an AggregateContainer with an AutowireContainer, or use ServiceProviderContainer inside an AggregateContainer. The container()
helper does this automatically for you.