thiagocordeiro / tiny-container
PHP 适用于无服务器项目的 tiny 容器
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 是一个 psr/container
实现,它允许在需要时才实例化服务,没有花哨或复杂的特性。因此,我建议使用 https://php-di.org/。目标是让非常小的项目在 psr 接口下具备容器功能,甚至不需要框架。
安装
composer require thiagocordeiro/tiny-container
如何使用
使用此工具并没有太多秘密。
$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();
测试
composer run tests
贡献
欢迎提出问题和提交 PR