Skip to content

Commit c8f5fd3

Browse files
authored
Update README.md
1 parent a912403 commit c8f5fd3

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@
2121
- Autowiring — automatic dependencies resolution
2222
- Full PHP 8.0+ features support for auto-wiring (e.g. union types)
2323

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+
2453
Usage
2554
-----
2655

@@ -205,18 +234,18 @@ But defining new scope variables won't modify the parent scope. That's it.
205234
$project = new CascadeContainer();
206235
$project->set('configuration', $config);
207236

208-
$module = $project->cascade(); // MAGIC! ✨
237+
$environment = $project->cascade(); // MAGIC! ✨
209238

210239
// Override existing services. It does not affect 'configuration' service in the parent container.
211-
$module->set('configuration', $moduleConfig);
240+
$environment->set('configuration', $moduleConfig);
212241

213242
// Define new services. They'll only exist on the current layer.
214-
$module->factory('request', function () {
243+
$environment->factory('request', function () {
215244
// ...
216245
});
217246
// and so on
218247

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
220249
```
221250

222251

0 commit comments

Comments
 (0)