walnut / lib_container
There is no license information available for the latest version (dev-main) of this package.
dev-main
2024-01-21 16:48 UTC
Requires
- php: >=8.1.0
- psr/container: *
Requires (Dev)
- phpunit/phpunit: ^9.5.20
- vimeo/psalm: ^4.23.0
This package is not auto-updated.
Last update: 2024-10-27 20:13:30 UTC
README
A very easy to use DI-container implementation.
Example:
final class DbUserRepository implements UserRepository { public function __construct( private /*readonly*/ QueryExecutor $queryExecutor ) {} //... } $mapping = [ UserRepository::class => DbUserRepository::class, QueryExecutor::class => PdoQueryExecutor::class, PdoQueryExecutor::class => [ 'dsn' => 'mysql:host=localhost;dbname=demo', 'user' => 'root', 'pwd' => 'root' ], UserService::class => fn(UserRepository $userRepository) => new UserService($userRepository, [ 'tokenValidityInMinutes' => 180 ] ), LoggerInterface::class => FileLogger::class, FileLogger::class => LocalFileLogger::class ]; $container = new ContainerAdapter(new Container($mapping)); $container->get(UserService::class); //returns UserService with injected DbUserRepository $container->get(LoggerInterface::class); //returns LocalFileLogger