Skip to content

Commit 7e7fb1f

Browse files
authored
V1.6.1 fixed comments for recorate (#9)
* v1.6.1 - update comment for ContainerDecoratorInterface::decorate * Update README
1 parent c2b1fc7 commit 7e7fb1f

File tree

3 files changed

+9
-145
lines changed

3 files changed

+9
-145
lines changed

README.md

Lines changed: 2 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,3 @@
1-
>**:heavy_exclamation_mark: Basic container implementation with the ability to inject dependencies**
1+
# Micro Framework
22

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 :)

src/ContainerDecoratorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
interface ContainerDecoratorInterface
1515
{
1616
/**
17-
* @template T
17+
* @template T of object
1818
*
1919
* @param class-string<T> $id
20-
* @param callable(): T $service
20+
* @param callable(mixed ...$parameters): T $service
2121
*/
2222
public function decorate(string $id, callable $service, int $priority = 0): void;
2323
}

tests/Unit/ContainerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,23 @@ public function testDecoratorsWithSamePriority(): void
104104
{
105105
$container = new Container();
106106

107-
$container->register(NamedInterface::class, function () {
107+
$container->register(NamedInterface::class, function (): NamedInterface {
108108
return new NamedService('A');
109109
});
110110

111-
$container->decorate(NamedInterface::class, function (NamedInterface $decorated) {
111+
$container->decorate(NamedInterface::class, function (NamedInterface $decorated): NamedInterface {
112112
return new NamedServiceDecorator($decorated, 'B');
113113
}, 10);
114114

115-
$container->decorate(NamedInterface::class, function (NamedInterface $decorated) {
115+
$container->decorate(NamedInterface::class, function (NamedInterface $decorated): NamedInterface {
116116
return new NamedServiceDecorator($decorated, 'D');
117117
});
118118

119-
$container->decorate(NamedInterface::class, function (NamedInterface $decorated) {
119+
$container->decorate(NamedInterface::class, function (NamedInterface $decorated): NamedInterface {
120120
return new NamedServiceDecorator($decorated, 'E');
121121
});
122122

123-
$container->decorate(NamedInterface::class, function (NamedInterface $decorated) {
123+
$container->decorate(NamedInterface::class, function (NamedInterface $decorated): NamedInterface {
124124
return new NamedServiceDecorator($decorated, 'C');
125125
}, 10);
126126

0 commit comments

Comments
 (0)