A PSR-11 compatible dependency injection library
- PHP 100%
|
Thomas Ernst
f8d00c5a3b
All checks were successful
ci / ci (push) Successful in 1m20s
|
||
|---|---|---|
| .forgejo/workflows | Allow manual CI runs | |
| .github | Add GitHub mirror README | |
| docs | Update docs tooling | |
| src | The big rename | |
| tests | The big rename | |
| .editorconfig | Sync shared config files | |
| .gitattributes | Update .gitattributes | |
| .gitignore | Update .gitignore | |
| CHANGELOG.md | Update changelog | |
| composer.json | Require dev package v4.2 | |
| LICENSE.md | Update license | |
| mago.toml | Update mago.toml | |
| phpunit.xml.dist | Update coverage config | |
| psalm.xml.dist | Use xml.dist config names | |
| README.md | Update README badges | |
Celemas Container
ci code coverage type coverage psalm level Software License
A PSR-11 compatible dependency injection container.
Entry lifetimes
Container entries use explicit lifetimes:
shared()caches one instance per container (default for added entries)scoped()caches one instance per requesting container scopetransient()never cachesvalue()returns the configured definition as-is (for literals or raw closures)
$container->add('config', ['debug' => true])->value();
$container->add(Service::class)->shared();
$container->add(RequestContext::class)->scoped();
$container->add(Builder::class)->transient();
Scope mode
Use scope() to create an isolated container for one unit of work:
$root = new Container();
$root->add('app-name', 'celemas')->value();
$root->add('global-service', GlobalService::class)->shared();
$root->add('request-service', RequestService::class)->scoped();
$scope = $root->scope();
$scope->add(Request::class, $request)->value();
// ... resolve request-local services
$scope->reset();
After the first scope() call, the root container is sealed and no longer accepts structural mutations. Scope tags can inherit pre-defined root tags while keeping their own local caches.
Services that should be cleaned between scopes can implement Celemas\Container\Resettable and will be reset during $scope->reset().
License
This project is licensed under the MIT license.