phpatom / di
An expressive and flexible dependency injection container for PHP
v0.3.1
2021-04-01 14:19 UTC
Requires
- php: ^7.4|8.*
- psr/container: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.2
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2025-03-11 19:12:41 UTC
README
Atom DI
An expressive and flexible dependency injection container for PHP
📝 Table of Contents
Prerequisites
- PHP 8.2 +
- Composer
Installing
The recommended way to install is via Composer:
composer require phpatom/di
Testing Installing
composer test
Coding style
./vendor/bin/phpcs
Getting Started
Instantiate the container
use Atom\DI\Container; $container = new Container();
Basic usage
use Atom\DI\Definition; use Atom\DI\Definition; require_once "vendor/autoload.php"; class User { public function __construct(public readonly string $name) { } } class UserRepository { public function __construct(private User $user) { } public function getUser(): User { return $this->user; } } class AuthService { public function __construct(private UserRepository $userRepository) { } public function getUser(): User { return $this->userRepository->getUser(); } } class Controller { public function index(AuthService $authService): void { echo sprintf("Hello %s\n", $authService->getUser()->name); } } use Atom\DI\Container; $container = new Container(); $definition = $container->bind(AuthService::class) ->toNewInstance() ->withClass( UserRepository::class, Definition::newInstanceOf(UserRepository::class) ->withParameter('user', new User(name: "daniel")) ); // output: hello daniel $container->callMethod(Controller::class, 'index');
Factories
use Atom\DI\Container; use Atom\DI\Definition; class UserFactory { public function __construct( private int $previousId = 0 ) { } public function makeUser(): User { $user = new User( sprintf("User #%d", $this->previousId), id: $this->previousId, ); $this->previousId++; return $user; } } /** * the definition is either a prototype or a singleton, singleton is the default */ $container->bind(UserFactory::class)->singleton(); // output: hello daniel $container->bind( User::class, Definition::callTo("makeUser") ->method() ->on(Definition::get(UserFactory::class)) ) ->prototype(); // without this, the instance will be cached // output: int(0) var_dump($container->get(User::class)->id); // output: int(1) var_dump($container->get(User::class)->id);
Values
use Atom\DI\Container; use Atom\DI\Definition; $container = new Container(); $container->bind('foo', "bar"); //or $container->bind('daniel') ->toValue(new User(name: "Nghokeng Daniel", id: 28)); //output: bar echo $container->get('foo'), "\n";
Array access
use Atom\DI\Container; use Atom\DI\Definition; $container = new Container(); function makeDaniel() { return new User("daniel", 28); } $container['foo'] = "bar"; $container['daniel'] = Definition::callTo("makeDaniel") ->function(); //output: bar echo $container['foo'], "\n"; //output: daniel echo $container['daniel']->name, "\n";
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
✍️ Author
- @dani-gouken - Idea & Initial work