giuseppe998e / syringe
Dependency injection library for PHP8
1.0.4
2022-10-25 16:06 UTC
Requires
- php: ^8
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-05-26 15:19:04 UTC
README
Dependency injection library for PHP8.
Usage
The configuration class:
<?php use PDO; use Syringe\Attribute\{Provides, Qualifier}; class DbConfiguration { // bool "primary" - Sets the Provides as primary if more than one of the same type is available. // ?string "name" - Custom name (or qualifier) for the Provides. (Set "null" to use the method name) // bool "singleton" - Denotes that the Provides is a singleton #[Provides(primary: false, name: null, singleton: true)] // Default values public function getMariaDBConnection(): PDO { $dsn = 'mysql:dbname=mariadb;host=127.0.0.1;port=3307'; $user = 'root'; $password = 'password'; return new PDO($dsn, $user, $password); } #[Provides(primary: true)] public function getMySQLConnection(): PDO { $dsn = 'mysql:dbname=mysqldb;host=127.0.0.1;port=3306'; $user = 'root'; $password = 'password'; return new PDO($dsn, $user, $password); } /* public function getCarsRepository( // Without "Qualifier" the parameter will be bound to // "getMySQLConnection" (because it's primary for the PDO class) #[Qualifier("getMariaDBConnection")] PDO $db ): CarsRepository { return new CarsRepository($db); } */ // ... }
The component class:
<?php use Syringe\Attribute\Inject; class TestClass { #[Inject] // Injects "getMySQLConnection" because it's set as primary // #[Inject("getMariaDBConnection")] // or #[Inject(qualifier: "getMySQLConnection")] private PDO $db; public function getUserById(int $id): array { $stmt = $db->prepare('SELECT * FROM users WHERE id=?'); $stmt->execute([$id]); return $stmt->fetch(); } }
Initialize Syringe:
<?php use Syringe\Syringe; use Syringe\Repository\ComponentRepository; $repo = new ComponentRepository(); $repo->addConfiguration(DbConfiguration::class); // $repo->addConfiguration(OtherConfiguration::class); Syringe::initialize($repo); $testClass = Syringe::new(TestClass::class); $user = $testClass->getUserById(1); var_dump($user); // ...