|
1 | | ->**:heavy_exclamation_mark: Basic container implementation with the ability to inject dependencies** |
| 1 | +# Micro Framework |
2 | 2 |
|
3 | | -### Requirements |
4 | | - |
5 | | -PHP >= 8.2 |
6 | | - |
7 | | -### How to use the library |
8 | | - |
9 | | -Add the latest version of MicroDependencyInjection into your project by using Composer or manually: |
10 | | - |
11 | | -__Using Composer (Recommended)__ |
12 | | - |
13 | | -Either run the following command in the root directory of your project: |
14 | | -``` |
15 | | -composer require micro/dependency-injection |
16 | | -``` |
17 | | - |
18 | | -Or require the Checkout.com package inside the composer.json of your project: |
19 | | -``` |
20 | | -"require": { |
21 | | - "php": ">=8.0", |
22 | | - "micro/dependency-injection": "dev-master" |
23 | | -}, |
24 | | -``` |
25 | | - |
26 | | -### Example |
27 | | - |
28 | | -After adding the library to your project, include the file autoload.php found in root of the library. |
29 | | -```html |
30 | | -include 'vendor/autoload.php'; |
31 | | -``` |
32 | | - |
33 | | -#### Simple usage: |
34 | | -```php |
35 | | -use \Micro\Component\DependencyInjection\Container; |
36 | | - |
37 | | -class Logger implements LoggerInterface { |
38 | | -} |
39 | | - |
40 | | -class Mailer { |
41 | | - public function __construct(private Logger $logger) {} |
42 | | -} |
43 | | - |
44 | | -$container = new Container(); |
45 | | - |
46 | | -$container->register(LoggerInterface::class, function(Container $container) { |
47 | | - return new Logger(); |
48 | | -}); |
49 | | - |
50 | | -$container->register('logger.doctrine', function(Container $container) { |
51 | | - return new Logger('doctrine-channel'); |
52 | | -}); |
53 | | - |
54 | | -$container->register(Mailer::class, function(Container $container) { |
55 | | - return new Mailer($container->get(Logger::class)); |
56 | | -}); |
57 | | - |
58 | | -$mailer = $container->get(Mailer::class); |
59 | | -``` |
60 | | - |
61 | | -#### Service decoration. |
62 | | - |
63 | | -```php |
64 | | -interface HelloWorldFacadeInterface |
65 | | -{ |
66 | | - public function hello(string $name): string; |
67 | | -} |
68 | | - |
69 | | -class HelloWorldFacade implements HelloWorldFacadeInterface |
70 | | -{ |
71 | | - public function hello(string $name): string |
72 | | - { |
73 | | - return "Hello, {$name}."; |
74 | | - } |
75 | | -} |
76 | | - |
77 | | -class NiceHelloWorldDecorator implements HelloWorldFacadeInterface |
78 | | -{ |
79 | | - public function __construct( |
80 | | - private readonly HelloWorldFacadeInterface $decoratedService |
81 | | - ) { |
82 | | - } |
83 | | - |
84 | | - public function hello(string $name): string |
85 | | - { |
86 | | - $result = $this->decoratedService->hello($name); |
87 | | - |
88 | | - return $result . ' I\'m glad to see you'; |
89 | | - } |
90 | | -} |
91 | | - |
92 | | -class HelloWorldLoggerAwareDecorator implements HelloWorldFacadeInterface |
93 | | -{ |
94 | | - public function __construct( |
95 | | - private readonly HelloWorldFacadeInterface $decoratedService, |
96 | | - private readonly LoggerInterface $logger |
97 | | - ) { |
98 | | - } |
99 | | - |
100 | | - public function hello(string $name): string |
101 | | - { |
102 | | - $result = $this->decoratedService->hello($name); |
103 | | - |
104 | | - $this->logger->info->info($result); |
105 | | - |
106 | | - return $result; |
107 | | - } |
108 | | -} |
109 | | - |
110 | | -$container = new Container(); |
111 | | - |
112 | | -$container->register(HelloWorldFacadeInterface::class, function () { |
113 | | - return new HelloWorldFacade(); |
114 | | -}); |
115 | | - |
116 | | -$container->register(HelloWorldFacadeInterface::class, function ( |
117 | | - HelloWorldFacadeInterface $decorated |
118 | | -) { |
119 | | - return new NiceHelloWorldDecorator($decorated); |
120 | | -}); |
121 | | - |
122 | | -$container->decorate(HelloWorldFacadeInterface::class, function( |
123 | | - HelloWorldFacadeInterface $decorated, |
124 | | - Container $container |
125 | | -) { |
126 | | - return new HelloWorldLoggerAwareDecorator( |
127 | | - $decorated, |
128 | | - $container->get(LoggerInterface::class) |
129 | | - ); |
130 | | -}); |
131 | | - |
132 | | -echo $container->get(HelloWorldFacadeInterface::class)->hello('Stas'); |
133 | | -// Output: Hello, Stas. I'm glad to see you |
134 | | - |
135 | | -``` |
136 | | - |
137 | | -### Sample code for: |
138 | | - |
139 | | -- [PSR-11](https://www.php-fig.org/psr/psr-11/) |
| 3 | +Documentation is available [here](https://micro-php.net/docs). If not, we will be grateful if you can become its author :) |
0 commit comments