thiagocordeiro / tiny-container
PHP tiny container for serverless like projects
v0.3.2
2022-12-26 19:38 UTC
Requires
- php: ^8.1
- ext-json: *
- psr/container: ^1.0
Requires (Dev)
- phpstan/phpstan: ^0.12.29
- phpstan/phpstan-phpunit: ^0.12.11
- phpunit/phpunit: ^9.2
- slevomat/coding-standard: ^6.3
- symfony/var-dumper: ^6.2.1
README
Tiny container is a psr/container
implementation which allows registering services to be instantiated only when needed, there is no fancy or complex features, for this I would recommend using https://php-di.org/.
The goal is to have container capabilities under psr interface for very tiny projects, when we don't even need a framework.
Installing
composer require thiagocordeiro/tiny-container
How to use
There is not much secret in using this tool.
$config = [ UserRepositoryInterface::class => fn(ContainerInterface $container) => new DoctrineUserRepository(), CacheInterface::class => fn(ContainerInterface $container) => new RedisCache(), 'http.api-client' => fn(ContainerInterface $container) => new GuzzleClient([]), MyService::class => fn(ContainerInterface $container) => new MyService( $container->get(UserRepositoryInterface::class), $container->get(CacheInterface::class), $container->get('http.api-client'), ), ]; $container = \TinyContainer\TinyContainer($config); $service = $container->get(MyService::class); $service->doTheThing();
Testing
composer run tests
Contributing
Feel free to open issues and submit PRs