| name | acc-create-psr11-container |
| description | Generates PSR-11 Container implementation for PHP 8.5. Creates ContainerInterface with service resolution, autowiring support, and exceptions. Includes unit tests. |
PSR-11 Container Generator
Overview
Generates PSR-11 compliant dependency injection container implementations.
When to Use
- Building lightweight DI container
- Creating service locator
- Simple service resolution needs
- Testing with mock containers
Generated Components
| Component | Description | Location |
|---|
| Container | Container implementation | src/Infrastructure/Container/ |
| Exceptions | PSR-11 exceptions | src/Infrastructure/Container/ |
| Unit Tests | PHPUnit tests | tests/Unit/Infrastructure/Container/ |
Template: Simple Container
<?php
declare(strict_types=1);
namespace App\Infrastructure\Container;
use Closure;
use Psr\Container\ContainerInterface;
final class Container implements ContainerInterface
{
private array $services = [];
private array $factories = [];
public function get(string $id): mixed
{
if (isset($this->services[$id])) {
return $this->services[$id];
}
if (isset($this->factories[$id])) {
$this->services[$id] = ($this->factories[$id])($this);
return $this->services[$id];
}
throw new NotFoundException("Service not found: {$id}");
}
public function has(string $id): bool
{
return isset($this->services[$id]) || isset($this->factories[$id]);
}
public function set(string $id, mixed $service): void
{
$this->services[$id] = $service;
}
public function factory(string $id, Closure $factory): void
{
$this->factories[$id] = $factory;
}
}
Template: Autowiring Container
<?php
declare(strict_types=1);
namespace App\Infrastructure\Container;
use Closure;
use Psr\Container\ContainerInterface;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionParameter;
final class AutowiringContainer implements ContainerInterface
{
private array $services = [];
private array $factories = [];
private array $aliases = [];
public function get(string $id): mixed
{
$id = $this->resolveAlias($id);
if (isset(->services[])) {
->services[];
}
((->factories[])) {
->services[] = (->factories[])();
->services[];
}
(()) {
->services[] = ->();
->services[];
}
();
}
{
= ->();
(->services[])
|| (->factories[])
|| ();
}
{
->services[] = ;
}
{
->factories[] = ;
}
{
->aliases[] = ;
}
{
->aliases[] ?? ;
}
{
= ();
(!->()) {
();
}
= ->();
( === ) {
();
}
= ->();
= (
fn(ReflectionParameter ) => ->(),
,
);
(...);
}
{
= ->();
( === ) {
(->()) {
->();
}
(
,
);
}
(! ReflectionNamedType || ->()) {
(->()) {
->();
}
(
,
);
}
->(->());
}
}
Template: Exceptions
<?php
declare(strict_types=1);
namespace App\Infrastructure\Container;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
final class ContainerException extends Exception implements ContainerExceptionInterface
{
}
final class NotFoundException extends Exception implements NotFoundExceptionInterface
{
}
Template: Unit Test
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Infrastructure\Container;
use App\Infrastructure\Container\AutowiringContainer;
use App\Infrastructure\Container\NotFoundException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[Group('unit')]
#[CoversClass(AutowiringContainer::class)]
final class AutowiringContainerTest extends TestCase
{
private AutowiringContainer $container;
protected function ():
{
->container = ();
}
{
= \();
->container->(, );
::(, ->container->());
}
{
->container->(, fn() => \());
= ->container->();
= ->container->();
::(, );
}
{
= ->container->(::);
::(::, );
}
{
->(::);
->container->();
}
{
->container->(, \());
->container->(, );
::(
->container->(),
->container->(),
);
}
}
{
}
Usage Example
<?php
use App\Infrastructure\Container\AutowiringContainer;
$container = new AutowiringContainer();
$container->set('config', ['db' => 'mysql://localhost/app']);
$container->factory(LoggerInterface::class, fn($c) => new FileLogger('/var/log/app.log'));
$container->alias(UserRepositoryInterface::class, DoctrineUserRepository::class);
$logger = $container->get(LoggerInterface::class);
$repository = $container->get(UserRepositoryInterface::class);
$handler = $container->get(CreateUserHandler::class);
Requirements
{
"require": {
"psr/container": "^2.0"
}
}
See Also
references/templates.md - Additional container patterns
references/examples.md - Integration examples