Skip to content

Commit c5fa39c

Browse files
committed
docs(readme): add readme
1 parent 0b8bf3a commit c5fa39c

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

README.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# FlightPHP Container
2+
3+
A lightweight Dependency Injection Container (DIC) for PHP, to manage and streamline object dependencies effectively.
4+
5+
## Features
6+
7+
- **Lightweight and Efficient:** Manage dependencies with ease.
8+
- **Flexible Configuration:** Easily integrate with various PHP applications.
9+
- **PSR-11 Compliance:** Ensures interoperability between containers.
10+
11+
## Requirements
12+
13+
- PHP 7.4 or higher
14+
- Composer installed on your system
15+
16+
## Installation
17+
18+
To include the FlightPHP Container in your project, you can use Composer:
19+
20+
```bash
21+
composer require flightphp/container
22+
```
23+
24+
## Simple usage
25+
26+
To use the FlightPHP Container, you can create a new instance of the container and bind your dependencies:
27+
28+
```php
29+
<?php
30+
31+
require 'vendor/autoload.php';
32+
33+
use Flight\Container;
34+
35+
$container = new Container;
36+
37+
$container->set(PDO::class, fn(): PDO => new PDO(
38+
'mysql:host=localhost;dbname',
39+
'username',
40+
'password'
41+
));
42+
43+
$pdo = $container->get(PDO::class);
44+
45+
var_dump($pdo);
46+
47+
/*
48+
object(PDO)#3 (0) {
49+
}
50+
*/
51+
```
52+
53+
## Usage in FlightPHP Framework
54+
55+
You can use the FlightPHP Container in your FlightPHP application by setting the container instance:
56+
57+
```php
58+
<?php
59+
60+
require 'vendor/autoload.php';
61+
62+
use Flight\Container;
63+
64+
$container = new Container;
65+
66+
$container->set(PDO::class, fn(): PDO => new PDO(
67+
'mysql:host=localhost;dbname',
68+
'username',
69+
'password'
70+
));
71+
72+
Flight::registerContainerHandler($container);
73+
74+
class TestController {
75+
function __construct(private PDO $pdo) {}
76+
77+
function index() {
78+
var_dump($this->pdo);
79+
}
80+
}
81+
82+
Flight::route('GET /', [TestController::class, 'index']);
83+
84+
Flight::start();
85+
```
86+
87+
Go and visit that route in your dev server and you'll see something like:
88+
89+
```
90+
object(PDO)#3 (0) {
91+
}
92+
```
93+
94+
## Advance usage
95+
96+
FlightPHP Container can resolve dependencies recursively, allowing you to bind complex objects and dependencies:
97+
98+
```php
99+
<?php
100+
101+
require 'vendor/autoload.php';
102+
103+
use Flight\Container;
104+
105+
class User {}
106+
107+
interface UserRepository {
108+
function find(int $id): ?User;
109+
}
110+
111+
class PdoUserRepository implements UserRepository {
112+
private PDO $pdo;
113+
114+
function __construct(PDO $pdo) {
115+
$this->pdo = $pdo;
116+
}
117+
118+
function find(int $id): ?User {
119+
// Implementation ...
120+
}
121+
}
122+
123+
$container = new Container;
124+
125+
$container->set(PDO::class, static fn(): PDO => new PDO(
126+
'mysql:host=localhost;dbname=test',
127+
'username',
128+
'password'
129+
));
130+
131+
$container->set(UserRepository::class, PdoUserRepository::class);
132+
133+
$userRepository = $container->get(UserRepository::class);
134+
135+
var_dump($userRepository);
136+
137+
/*
138+
object(PdoUserRepository)#4 (1) {
139+
["pdo":"PdoUserRepository":private]=>
140+
object(PDO)#3 (0) {
141+
}
142+
}
143+
*/
144+
```
145+
146+
## License
147+
148+
FlightPHP Container is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

0 commit comments

Comments
 (0)