Skip to content

Commit 8b1260c

Browse files
committed
Add Dependency Injection example to the readme
split: 6634791472b3054c9fefdaa37789dc72013ba1e9
1 parent dfe164a commit 8b1260c

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
Phug Formatter
3-
========
2+
Phug Dependency Injection
3+
=========================
44

55
What is Phug Dependency Injection?
6-
-----------------
6+
----------------------------------
77

88
This project allow to provide helpers functions and values,
99
to require them and to dump all required dependencies as
@@ -20,3 +20,31 @@ composer require phug/dependency-injection
2020

2121
Usage
2222
-----
23+
24+
```php
25+
use Phug\DependencyInjection;
26+
27+
$dependencies = new DependencyInjection();
28+
$dependencies->register('limit', 42);
29+
$dependencies->provider('clock', static function () {
30+
return new Clock();
31+
});
32+
33+
$dependencies->provider('expiration', ['clock', 'limit', static function (ClockInterface $clock, $limit) {
34+
return static function ($margin) use ($clock, $limit) {
35+
$delta = $limit - $margin;
36+
37+
return $clock->now()->modify("$delta days");
38+
};
39+
}]);
40+
41+
$expiration = $dependencies->call('expiration'); // return new DateTimeImmutable('now + 42 days')
42+
$expiration = $dependencies->call('expiration', 20); // return new DateTimeImmutable('now + 22 days')
43+
```
44+
45+
Security contact information
46+
----------------------------
47+
48+
To report a security vulnerability, please use the
49+
[Tidelift security contact](https://tidelift.com/security).
50+
Tidelift will coordinate the fix and disclosure.

0 commit comments

Comments
 (0)