A blazingly fast, zeroβbloat PSRβ11 container with a compile step for maximum performance. Built with modern PHP features and designed for highβthroughput applications. Follows the same compileβtime optimization philosophy as Atomic HTTP Kernel and Router.
Atomic Container delivers exceptional performance via compileβtime preparation and lazy caching:
- 3.2Mβ3.4M ops/sec β
get()for instances/singletons/aliases andhas()hits - ~1.8M ops/sec β
get()for nonβshared factory entries - Compile is fast and scales with definitions
Container Benchmark:
benchGetInstance : 3,409,676 ops/sec
benchGetSingleton : 3,419,738 ops/sec
benchGetFactory : 1,877,929 ops/sec
benchGetAlias : 3,254,276 ops/sec
benchHasHit : 3,323,349 ops/sec
benchHasMiss : 3,132,513 ops/sec
benchCompile5 : 450,885 ops/sec
benchCompile20 : 139,451 ops/sec
Run them locally with composer benchmark.
- Zeroβbloat, PSRβ11 compliant container
- Compileβtime alias resolution for O(1) lookups
- Lazy singleton caching; factories for nonβshared creation
- Immutable compiled container; safe to share everywhere
- Fully typeβsafe codebase with Psalm and strict types
- Builtβin benchmarks and comprehensive tests
composer require atomic/containerRequirements:
- PHP 8.4 or higher
- PSRβ11
psr/container
- PSRβ11: Container interface and exception interfaces
<?php
use Atomic\Container\ContainerBuilder;
use Psr\Container\ContainerInterface;
$builder = new ContainerBuilder();
// 1) Instances/values
$builder->set('config', ['env' => 'prod']);
// 2) Shared service (singleton)
$builder->set('logger', function (ContainerInterface $c) {
return new Logger($c->get('config'));
}, shared: true);
// 3) Non-shared factory
$builder->factory('uuid', fn () => Ramsey\Uuid\Uuid::uuid7()->toString());
// 4) Alias
$builder->alias(LoggerInterface::class, 'logger');
// Compile once at boot for fast lookups
$container = $builder->compile();
$logger = $container->get(LoggerInterface::class);
$uuid1 = $container->get('uuid');
$uuid2 = $container->get('uuid'); // differentNote: This package is frameworkβagnostic and can be used with any PSRβ11 consumer.
ββββββββββββββββββββββ βββββββββββββββββββββββββ
β ContainerBuilder βββββΆβ CompiledContainer β
β (mutable, setup) β β (immutable, runtime) β
β instances/factories β β O(1) lookup, caching β
ββββββββββββββββββββββ βββββββββββββββββββββββββ
- Boot Time: Builder collects instances, factories and aliases and compiles them into an immutable container.
- Runtime:
get()performs O(1) lookup with alias pre-resolution and lazy singleton caching. - Caching: Shared factories are resolved once and cached on first use.
// This happens ONCE at boot:
$container = $builder->compile();
// This happens MILLIONS of times at runtime:
$service = $container->get('service'); // β‘ Fast pathalias(id)β resolve to final target (preβresolved at compile)- direct instance/value β return asβis
- shared factory β lazily create + cache; subsequent calls return same instance
- nonβshared factory β create a new value each call
- otherwise β
NotFoundException
Mutable registration builder; produces an immutable PSRβ11 container.
final class ContainerBuilder
{
public function set(string $id, callable|object|scalar|array|null $value, bool $shared = true): void;
public function factory(string $id, callable $factory): void; // non-shared
public function alias(string $id, string $targetId): void;
public function compile(): Psr\Container\ContainerInterface; // optimized container
}Highβperformance PSRβ11 container for runtime use.
final class CompiledContainer implements Psr\Container\ContainerInterface
{
public function get(string $id): mixed; // may throw NotFoundException|ContainerException
public function has(string $id): bool;
}Atomic\Container\Exceptions\NotFoundException(implementsPsr\Container\NotFoundExceptionInterface)Atomic\Container\Exceptions\ContainerException(implementsPsr\Container\ContainerExceptionInterface)
Run the comprehensive test suite:
# Run all tests
composer test
# Run tests with coverage
composer test-coverage
# Static analysis & CS
composer psalm
composer cs-checkcomposer test-coverage generates coverage.xml (Clover) used by CI; the workflow uploads it to Codecov for the badge/report.
Measure performance with the built-in benchmark suite:
# Run all benchmarks
composer benchmark
# View detailed performance metrics
php benchmarks/run-benchmarks.phpContainer Benchmark:
benchGetInstance : 3,409,676 ops/sec (0.000 ms/op)
benchGetSingleton : 3,419,738 ops/sec (0.000 ms/op)
benchGetFactory : 1,877,929 ops/sec (0.001 ms/op)
benchGetAlias : 3,254,276 ops/sec (0.000 ms/op)
benchHasHit : 3,323,349 ops/sec (0.000 ms/op)
benchHasMiss : 3,132,513 ops/sec (0.000 ms/op)
Compile Benchmark:
benchCompile5 : 450,885 ops/sec (0.002 ms/op)
benchCompile20 : 139,451 ops/sec (0.007 ms/op)
Maintain code quality with included tools:
# Check code style
composer cs-check
# Fix code style
composer cs-fix
# Run static analysis
composer psalm
# Run all quality checks
composer qaWe welcome contributions! Please see CONTRIBUTING.md for details.
See CHANGELOG.md for release notes and version history.
This project is licensed under the MIT License β see LICENSE.
- Registrations and aliasing (including chains and cycle detection)
- Singleton caching and factory creation
- Error propagation (wrapping into ContainerException)
- Immutability of compiled container