Skip to content

Commit dc893c3

Browse files
committed
Chore: Enhancement
- Fixed tests after removing AutowiredContainer; - Enhanced unit tests; - Added arguments to decorated closure: actual decorated service and current container; - Dropped support for php 8.1; - PHP Code Sniffer fixes; - Updated example in README.md; Signed-off-by: Oleksii Bulba <oleksii_bulba@epam.com>
1 parent 52ffbb2 commit dc893c3

File tree

10 files changed

+119
-108
lines changed

10 files changed

+119
-108
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
# normal, highest, non-dev installs
16-
php-version: [ '8.1', '8.2' ]
16+
php-version: [ '8.2' ]
1717
dependency-versions: [ 'highest' ]
1818
include:
1919
# testing lowest PHP version with the lowest dependencies
20-
- php-version: '8.1'
20+
- php-version: '8.2'
2121
dependency-versions: 'lowest'
2222

2323
# testing dev versions with the highest PHP

README.md

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
### Requirements
44

5-
PHP >= 8.0.0
5+
PHP >= 8.2
66

77
### How to use the library
88

@@ -34,7 +34,7 @@ include 'vendor/autoload.php';
3434
```php
3535
use \Micro\Component\DependencyInjection\Container;
3636

37-
class Logger {
37+
class Logger implements LoggerInterface {
3838
}
3939

4040
class Mailer {
@@ -43,10 +43,14 @@ class Mailer {
4343

4444
$container = new Container();
4545

46-
$container->register(Logger::class, function(Container $container) {
46+
$container->register(LoggerInterface::class, function(Container $container) {
4747
return new Logger();
4848
});
4949

50+
$container->register('logger.doctrine', function(Container $container) {
51+
return new Logger('doctrine-channel');
52+
});
53+
5054
$container->register(Mailer::class, function(Container $container) {
5155
return new Mailer($container->get(Logger::class));
5256
});
@@ -70,13 +74,11 @@ class HelloWorldFacade implements HelloWorldFacadeInterface
7074
}
7175
}
7276

73-
74-
class HelloWorldDecorator implements HelloWorldFacadeInterface
77+
class NiceHelloWorldDecorator implements HelloWorldFacadeInterface
7578
{
7679
public function __construct(
77-
private readonly HelloWorldFacadeInterface $decoratedService,
78-
)
79-
{
80+
private readonly HelloWorldFacadeInterface $decoratedService
81+
) {
8082
}
8183

8284
public function hello(string $name): string
@@ -87,25 +89,51 @@ class HelloWorldDecorator implements HelloWorldFacadeInterface
8789
}
8890
}
8991

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+
90110
$container = new Container();
91111

92112
$container->register(HelloWorldFacadeInterface::class, function () {
93113
return new HelloWorldFacade();
94114
});
95115

116+
$container->register(HelloWorldFacadeInterface::class, function (
117+
HelloWorldFacadeInterface $decorated
118+
) {
119+
return new NiceHelloWorldDecorator($decorated);
120+
});
121+
96122
$container->decorate(HelloWorldFacadeInterface::class, function(
97-
HelloWorldFacadeInterface $serviceForDecoration
123+
HelloWorldFacadeInterface $decorated,
124+
Container $container
98125
) {
99-
return new HelloWorldLoggerAwareDecorator($serviceForDecoration);
126+
return new HelloWorldLoggerAwareDecorator(
127+
$decorated,
128+
$container->get(LoggerInterface::class)
129+
);
100130
});
101131

102132
echo $container->get(HelloWorldFacadeInterface::class)->hello('Stas');
103133
// Output: Hello, Stas. I'm glad to see you
104134

105-
106135
```
107136

108-
109137
### Sample code for:
110138

111139
- [PSR-11](https://www.php-fig.org/psr/psr-11/)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^8.1 || ^8.2",
13+
"php": "^8.2",
1414
"psr/container": "^2.0"
1515
},
1616
"require-dev": {

phpcs.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
<rule ref="Generic.ControlStructures.InlineControlStructure"/>
1515
<rule ref="Generic.Files.ByteOrderMark"/>
1616
<rule ref="Generic.Files.LineEndings"/>
17-
<rule ref="Generic.Files.LineLength"/>
17+
<rule ref="Generic.Files.LineLength">
18+
<properties>
19+
<property name="lineLimit" value="120"/>
20+
<property name="absoluteLineLimit" value="120"/>
21+
</properties>
22+
</rule>
1823
<rule ref="Generic.Formatting.DisallowMultipleStatements"/>
1924
<rule ref="Generic.Formatting.MultipleStatementAlignment"/>
2025
<rule ref="Generic.Formatting.NoSpaceAfterCast"/>

src/Container.php

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,25 @@
44

55
use Micro\Component\DependencyInjection\Exception\ServiceNotRegisteredException;
66
use Micro\Component\DependencyInjection\Exception\ServiceRegistrationException;
7-
use \Closure;
87
use Psr\Container\ContainerInterface;
98

109
class Container implements ContainerInterface, ContainerRegistryInterface, ContainerDecoratorInterface
1110
{
1211
/**
1312
* @var array<string, object>
1413
*/
15-
private array $services;
14+
private array $services = [];
1615

1716
/**
18-
* @var array<string, Closure|string>
17+
* @var array<string, \Closure|string>
1918
*/
20-
private array $servicesRaw;
19+
private array $servicesRaw = [];
2120

2221
/**
23-
* @var array<string, array<Closure, int>>
22+
* @var array<string, array<int, \Closure>>
2423
*/
2524
private array $decorators = [];
2625

27-
public function __construct(
28-
)
29-
{
30-
$this->services = [];
31-
$this->servicesRaw = [];
32-
}
33-
3426
/**
3527
* @template T
3628
*
@@ -54,7 +46,7 @@ public function has(string $id): bool
5446
/**
5547
* {@inheritDoc}
5648
*/
57-
public function register(string $id, Closure $service): void
49+
public function register(string $id, \Closure $service): void
5850
{
5951
if($this->has($id)) {
6052
throw new ServiceRegistrationException(sprintf('Service "%s" already registered', $id));
@@ -66,13 +58,13 @@ public function register(string $id, Closure $service): void
6658
/**
6759
* {@inheritDoc}
6860
*/
69-
public function decorate(string $id, Closure $service, int $priority = 0): void
61+
public function decorate(string $id, \Closure $service, int $priority = 0): void
7062
{
7163
if(!array_key_exists($id, $this->decorators)) {
7264
$this->decorators[$id] = [];
7365
}
7466

75-
$this->decorators[$id][] = [$service, $priority];
67+
$this->decorators[$id][$priority] = $service;
7668
}
7769

7870
/**
@@ -93,16 +85,15 @@ private function lookup(string $id): object
9385

9486
/**
9587
* @param string $serviceId
96-
* @return object
9788
*/
9889
protected function initializeService(string $serviceId): void
9990
{
10091
if(empty($this->servicesRaw[$serviceId])) {
10192
throw new ServiceNotRegisteredException($serviceId);
10293
}
10394

104-
$raw = $this->servicesRaw[$serviceId];
105-
$service = $raw($this);
95+
$raw = $this->servicesRaw[$serviceId];
96+
$service = $raw($this);
10697
$this->services[$serviceId] = $service;
10798

10899
if(!array_key_exists($serviceId, $this->decorators)) {
@@ -111,19 +102,10 @@ protected function initializeService(string $serviceId): void
111102

112103
$decorators = $this->decorators[$serviceId];
113104

114-
usort($decorators, function(array $left, array $right): int {
115-
$l = $left[1];
116-
$r = $right[1];
117-
if($l === $r) {
118-
return 0;
119-
}
120-
121-
return $left[1] > $right[1] ? 1 : -1;
122-
});
105+
ksort($decorators);
123106

124-
/** @var array<Closure, int> $decorator */
125107
foreach ($decorators as $decorator) {
126-
$this->services[$serviceId] = $decorator[0]();
108+
$this->services[$serviceId] = $decorator($this->services[$serviceId], $this);
127109
}
128110
}
129111
}

src/ContainerDecoratorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ interface ContainerDecoratorInterface
1212
* @return void
1313
*/
1414
public function decorate(string $id, \Closure $service, int $priority = 0): void;
15-
}
15+
}

src/ContainerRegistryInterface.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Micro\Component\DependencyInjection;
44

5-
65
interface ContainerRegistryInterface
76
{
87
/**

src/Exception/ServiceNotRegisteredException.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
use Psr\Container\NotFoundExceptionInterface;
66

7-
class ServiceNotRegisteredException extends \RuntimeException
8-
implements NotFoundExceptionInterface
7+
class ServiceNotRegisteredException extends \RuntimeException implements NotFoundExceptionInterface
98
{
109
private string $serviceId;
1110

src/Exception/ServiceRegistrationException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use Psr\Container\ContainerExceptionInterface;
66

7-
class ServiceRegistrationException extends \RuntimeException
8-
implements ContainerExceptionInterface
7+
class ServiceRegistrationException extends \RuntimeException implements ContainerExceptionInterface
98
{
10-
119
}

0 commit comments

Comments
 (0)