@@ -30,7 +30,7 @@ After adding the library to your project, include the file autoload.php found in
3030include 'vendor/autoload.php';
3131```
3232
33- Simple usage:
33+ #### Simple usage:
3434``` php
3535use \Micro\Component\DependencyInjection\Container;
3636
@@ -54,6 +54,58 @@ $container->register(Mailer::class, function(Container $container) {
5454$mailer = $container->get(Mailer::class);
5555```
5656
57+ #### Service decoration.
58+
59+ ``` php
60+ interface HelloWorldFacadeInterface
61+ {
62+ public function hello(string $name): string;
63+ }
64+
65+ class HelloWorldFacade implements HelloWorldFacadeInterface
66+ {
67+ public function hello(string $name): string
68+ {
69+ return "Hello, {$name}.";
70+ }
71+ }
72+
73+
74+ class HelloWorldDecorator implements HelloWorldFacadeInterface
75+ {
76+ public function __construct(
77+ private readonly HelloWorldFacadeInterface $decoratedService,
78+ )
79+ {
80+ }
81+
82+ public function hello(string $name): string
83+ {
84+ $result = $this->decoratedService->hello($name);
85+
86+ return $result . ' I\'m glad to see you';
87+ }
88+ }
89+
90+ $container = new Container();
91+
92+ $container->register(HelloWorldFacadeInterface::class, function () {
93+ return new HelloWorldFacade();
94+ });
95+
96+ $container->decorate(HelloWorldFacadeInterface::class, function(
97+ HelloWorldFacadeInterface $serviceForDecoration
98+ ) {
99+ return new HelloWorldLoggerAwareDecorator($serviceForDecoration);
100+ });
101+
102+ echo $container->get(HelloWorldFacadeInterface::class)->hello('Stas');
103+ // Output: Hello, Stas. I'm glad to see you
104+
105+
106+ ```
107+
108+
57109### Sample code for:
58110
59111- [ PSR-11] ( https://www.php-fig.org/psr/psr-11/ )
0 commit comments