|
21 | 21 | - Autowiring — automatic dependencies resolution |
22 | 22 | - Full PHP 8.0+ features support for auto-wiring (e.g. union types) |
23 | 23 |
|
| 24 | + |
| 25 | +```php |
| 26 | +use Technically\CascadeContainer\CascadeContainer; |
| 27 | + |
| 28 | +$container = new CascadeContainer(); |
| 29 | + |
| 30 | +$container->set('config', $config); |
| 31 | + |
| 32 | +// Lazy-evaluated services |
| 33 | +$container->deferred('mailer', function () { |
| 34 | + // lazily initialize mailer service here |
| 35 | + $mailer = /* ... */; |
| 36 | + |
| 37 | + return $mailer; |
| 38 | +}); |
| 39 | + |
| 40 | +// On-demand object factories (executes every time 'request' is obtained from the container) |
| 41 | +$container->factory('request', fn () => $requestFactory->createRequest()); |
| 42 | + |
| 43 | +// ✨ CASCADING LAYERS ✨ |
| 44 | + |
| 45 | +// Fork the container into an isolated layer, inheriting everything from above. |
| 46 | +// Override services or define new ones. The changes won't affect the parent $container instance. |
| 47 | +$environment = $container->cascade(); |
| 48 | + |
| 49 | +// For example, we want to use a different mailer implementation in the tests environment: |
| 50 | +$environment->deferred('mailer', fn () => new NullMailer()); |
| 51 | +``` |
| 52 | + |
24 | 53 | Usage |
25 | 54 | ----- |
26 | 55 |
|
@@ -205,18 +234,18 @@ But defining new scope variables won't modify the parent scope. That's it. |
205 | 234 | $project = new CascadeContainer(); |
206 | 235 | $project->set('configuration', $config); |
207 | 236 |
|
208 | | -$module = $project->cascade(); // MAGIC! ✨ |
| 237 | +$environment = $project->cascade(); // MAGIC! ✨ |
209 | 238 |
|
210 | 239 | // Override existing services. It does not affect 'configuration' service in the parent container. |
211 | | -$module->set('configuration', $moduleConfig); |
| 240 | +$environment->set('configuration', $moduleConfig); |
212 | 241 |
|
213 | 242 | // Define new services. They'll only exist on the current layer. |
214 | | -$module->factory('request', function () { |
| 243 | +$environment->factory('request', function () { |
215 | 244 | // ... |
216 | 245 | }); |
217 | 246 | // and so on |
218 | 247 |
|
219 | | -assert($project->get('configuration') !== $module->get('configuration')); // Parent service "configuration" instance remained unchanged |
| 248 | +assert($project->get('configuration') !== $environment->get('configuration')); // Parent service "configuration" instance remained unchanged |
220 | 249 | ``` |
221 | 250 |
|
222 | 251 |
|
|
0 commit comments