44
55use Micro \Component \DependencyInjection \Exception \ServiceNotRegisteredException ;
66use Micro \Component \DependencyInjection \Exception \ServiceRegistrationException ;
7- use \Closure ;
87use Psr \Container \ContainerInterface ;
98
109class 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}
0 commit comments