21
21
use Zend \ServiceManager \Exception \ServiceNotCreatedException ;
22
22
use Zend \ServiceManager \Exception \ServiceNotFoundException ;
23
23
24
- use function array_keys ;
25
- use function array_merge ;
26
- use function array_merge_recursive ;
27
- use function class_exists ;
28
- use function get_class ;
29
- use function gettype ;
30
- use function is_callable ;
31
- use function is_object ;
32
- use function is_string ;
33
- use function spl_autoload_register ;
34
- use function spl_object_hash ;
35
- use function sprintf ;
36
- use function trigger_error ;
24
+ use function \array_keys ;
25
+ use function \array_merge ;
26
+ use function \array_merge_recursive ;
27
+ use function \class_exists ;
28
+ use function \get_class ;
29
+ use function \gettype ;
30
+ use function \is_callable ;
31
+ use function \is_object ;
32
+ use function \is_string ;
33
+ use function \spl_autoload_register ;
34
+ use function \spl_object_hash ;
35
+ use function \sprintf ;
36
+ use function \trigger_error ;
37
+ use function \in_array ;
37
38
38
39
/**
39
40
* Service Manager.
@@ -105,11 +106,6 @@ class ServiceManager implements ServiceLocatorInterface
105
106
*/
106
107
private $ lazyServicesDelegator ;
107
108
108
- // /**
109
- // * @var string[]
110
- // */
111
- // private $resolvedAliases = [];
112
-
113
109
/**
114
110
* A list of already loaded services (this act as a local cache)
115
111
*
@@ -204,7 +200,7 @@ public function get($name)
204
200
$ object = $ this ->doCreate ($ name );
205
201
206
202
// Cache the object for later, if it is supposed to be shared.
207
- if (( $ sharedService) ) {
203
+ if ($ sharedService ) {
208
204
$ this ->services [$ name ] = $ object ;
209
205
}
210
206
return $ object ;
@@ -229,8 +225,8 @@ public function get($name)
229
225
if (($ sharedService )) {
230
226
$ this ->services [$ resolvedName ] = $ object ;
231
227
}
228
+
232
229
// Also do so for aliases, this allows sharing based on service name used.
233
- // $serviceAvailable is true if and only if we have an alias
234
230
if ($ sharedAlias ) {
235
231
$ this ->services [$ name ] = $ object ;
236
232
}
@@ -351,12 +347,12 @@ public function configure(array $config)
351
347
352
348
if (! empty ($ aliases )) {
353
349
$ config ['aliases ' ] = (isset ($ config ['aliases ' ]))
354
- ? \ array_merge ($ config ['aliases ' ], $ aliases )
350
+ ? array_merge ($ config ['aliases ' ], $ aliases )
355
351
: $ aliases ;
356
352
}
357
353
358
354
$ config ['factories ' ] = (isset ($ config ['factories ' ]))
359
- ? \ array_merge ($ config ['factories ' ], $ factories )
355
+ ? array_merge ($ config ['factories ' ], $ factories )
360
356
: $ factories ;
361
357
}
362
358
@@ -365,7 +361,7 @@ public function configure(array $config)
365
361
}
366
362
367
363
if (isset ($ config ['delegators ' ])) {
368
- $ this ->delegators = \ array_merge_recursive ($ this ->delegators , $ config ['delegators ' ]);
364
+ $ this ->delegators = array_merge_recursive ($ this ->delegators , $ config ['delegators ' ]);
369
365
}
370
366
371
367
if (isset ($ config ['shared ' ])) {
@@ -380,7 +376,7 @@ public function configure(array $config)
380
376
}
381
377
if (! empty ($ aliases )) {
382
378
foreach ($ aliases as $ alias => $ target ) {
383
- $ this ->doSetAlias ($ alias , $ target );
379
+ $ this ->mapAliasToTarget ($ alias , $ target );
384
380
}
385
381
}
386
382
@@ -391,7 +387,7 @@ public function configure(array $config)
391
387
// If lazy service configuration was provided, reset the lazy services
392
388
// delegator factory.
393
389
if (isset ($ config ['lazy_services ' ]) && ! empty ($ config ['lazy_services ' ])) {
394
- $ this ->lazyServices = \ array_merge_recursive ($ this ->lazyServices , $ config ['lazy_services ' ]);
390
+ $ this ->lazyServices = array_merge_recursive ($ this ->lazyServices , $ config ['lazy_services ' ]);
395
391
$ this ->lazyServicesDelegator = null ;
396
392
}
397
393
@@ -401,7 +397,7 @@ public function configure(array $config)
401
397
$ abstractFactories = $ config ['abstract_factories ' ];
402
398
// $key not needed, but foreach faster
403
399
foreach ($ abstractFactories as $ key => $ abstractFactory ) {
404
- $ this ->doAddAbstractFactory ($ abstractFactory );
400
+ $ this ->resolveAbstractFactoryInstance ($ abstractFactory );
405
401
}
406
402
}
407
403
@@ -423,7 +419,7 @@ public function configure(array $config)
423
419
public function setAlias ($ alias , $ target )
424
420
{
425
421
$ this ->validateServiceName ($ alias );
426
- $ this ->doSetAlias ($ alias , $ target );
422
+ $ this ->mapAliasToTarget ($ alias , $ target );
427
423
}
428
424
429
425
/**
@@ -470,7 +466,7 @@ public function mapLazyService($name, $class = null)
470
466
*/
471
467
public function addAbstractFactory ($ factory )
472
468
{
473
- $ this ->doAddAbstractFactory ($ factory );
469
+ $ this ->resolveAbstractFactoryInstance ($ factory );
474
470
}
475
471
476
472
/**
@@ -528,39 +524,35 @@ public function setShared($name, $flag)
528
524
*/
529
525
private function resolveInitializer ($ initializer )
530
526
{
531
- if (\ is_string ($ initializer ) && \ class_exists ($ initializer )) {
527
+ if (is_string ($ initializer ) && class_exists ($ initializer )) {
532
528
$ initializer = new $ initializer ();
533
529
}
534
530
535
- if (\ is_callable ($ initializer )) {
531
+ if (is_callable ($ initializer )) {
536
532
$ this ->initializers [] = $ initializer ;
537
533
return ;
538
534
}
539
535
540
536
// Error condition; let's find out why.
541
537
542
- if (\is_string ($ initializer )) {
543
- throw new InvalidArgumentException (
544
- sprintf (
545
- 'An invalid initializer was registered; resolved to class or function "%s" ' .
546
- 'which does not exist; please provide a valid function name or class ' .
547
- 'name resolving to an implementation of %s ' ,
548
- $ initializer ,
549
- Initializer \InitializerInterface::class
550
- )
551
- );
538
+ if (is_string ($ initializer )) {
539
+ throw new InvalidArgumentException (sprintf (
540
+ 'An invalid initializer was registered; resolved to class or function "%s" '
541
+ . 'which does not exist; please provide a valid function name or class '
542
+ . 'name resolving to an implementation of %s ' ,
543
+ $ initializer ,
544
+ Initializer \InitializerInterface::class
545
+ ));
552
546
}
553
547
554
548
// Otherwise, we have an invalid type.
555
- throw new InvalidArgumentException (
556
- sprintf (
557
- 'An invalid initializer was registered. Expected a callable, or an instance of ' .
558
- '(or string class name resolving to) "%s", ' .
559
- 'but "%s" was received ' ,
560
- Initializer \InitializerInterface::class,
561
- (is_object ($ initializer ) ? get_class ($ initializer ) : gettype ($ initializer ))
562
- )
563
- );
549
+ throw new InvalidArgumentException (sprintf (
550
+ 'An invalid initializer was registered. Expected a callable, or an instance of '
551
+ . '(or string class name resolving to) "%s", '
552
+ . 'but "%s" was received ' ,
553
+ Initializer \InitializerInterface::class,
554
+ (is_object ($ initializer ) ? get_class ($ initializer ) : gettype ($ initializer ))
555
+ ));
564
556
}
565
557
566
558
/**
@@ -589,12 +581,12 @@ private function getFactory($name)
589
581
$ factory = $ this ->factories [$ name ] ?? null ;
590
582
591
583
$ lazyLoaded = false ;
592
- if (\ is_string ($ factory ) && \ class_exists ($ factory )) {
584
+ if (is_string ($ factory ) && class_exists ($ factory )) {
593
585
$ factory = new $ factory ();
594
586
$ lazyLoaded = true ;
595
587
}
596
588
597
- if (\ is_callable ($ factory )) {
589
+ if (is_callable ($ factory )) {
598
590
if ($ lazyLoaded ) {
599
591
$ this ->factories [$ name ] = $ factory ;
600
592
}
@@ -635,13 +627,13 @@ private function createDelegatorFromName($name, array $options = null)
635
627
$ delegatorFactory = $ this ->createLazyServiceDelegatorFactory ();
636
628
}
637
629
638
- if (\ is_string ($ delegatorFactory ) && \ class_exists ($ delegatorFactory )) {
630
+ if (is_string ($ delegatorFactory ) && class_exists ($ delegatorFactory )) {
639
631
$ delegatorFactory = new $ delegatorFactory ();
640
632
}
641
633
642
- if (! \ is_callable ($ delegatorFactory )) {
643
- if (\ is_string ($ delegatorFactory )) {
644
- throw new ServiceNotCreatedException (\ sprintf (
634
+ if (! is_callable ($ delegatorFactory )) {
635
+ if (is_string ($ delegatorFactory )) {
636
+ throw new ServiceNotCreatedException (sprintf (
645
637
'An invalid delegator factory was registered; resolved to class or function "%s" '
646
638
. 'which does not exist; please provide a valid function name or class name resolving '
647
639
. 'to an implementation of %s ' ,
@@ -650,9 +642,9 @@ private function createDelegatorFromName($name, array $options = null)
650
642
));
651
643
}
652
644
653
- throw new ServiceNotCreatedException (\ sprintf (
645
+ throw new ServiceNotCreatedException (sprintf (
654
646
'A non-callable delegator, "%s", was provided; expected a callable or instance of "%s" ' ,
655
- \ is_object ($ delegatorFactory ) ? \ get_class ($ delegatorFactory ) : \ gettype ($ delegatorFactory ),
647
+ is_object ($ delegatorFactory ) ? get_class ($ delegatorFactory ) : gettype ($ delegatorFactory ),
656
648
DelegatorFactoryInterface::class
657
649
));
658
650
}
@@ -745,7 +737,7 @@ private function createLazyServiceDelegatorFactory()
745
737
));
746
738
}
747
739
748
- \ spl_autoload_register ($ factoryConfig ->getProxyAutoloader ());
740
+ spl_autoload_register ($ factoryConfig ->getProxyAutoloader ());
749
741
750
742
$ this ->lazyServicesDelegator = new Proxy \LazyServiceFactory (
751
743
new LazyLoadingValueHolderFactory ($ factoryConfig ),
@@ -818,15 +810,10 @@ private function validateServiceName($service)
818
810
// Important: Next three lines must kept equal to the three
819
811
// lines of validateServiceNameArray (see below) which are marked as code
820
812
// duplicate!
821
- if (! isset ($ this ->services [$ service ]) ?: $ this ->allowOverride ) {
813
+ if (! isset ($ this ->services [$ service ]) || $ this ->allowOverride ) {
822
814
return ;
823
815
}
824
- throw new ContainerModificationsNotAllowedException (sprintf (
825
- 'The container does not allow to replace/update a service '
826
- . ' with existing instances; the following '
827
- . 'already exist in the container: %s ' ,
828
- $ service
829
- ));
816
+ throw new ContainerModificationsNotAllowedException ($ service );
830
817
}
831
818
832
819
/**
@@ -845,8 +832,7 @@ private function validateServiceName($service)
845
832
*/
846
833
private function validateServiceNameArray (array $ services )
847
834
{
848
- $ keys = \array_keys ($ services );
849
- foreach ($ keys as $ service ) {
835
+ foreach (array_keys ($ services ) as $ service ) {
850
836
// This is a code duplication from validateServiceName (see above).
851
837
// validateServiceName is almost a one liner, so we reproduce it
852
838
// here for the sake of performance of aggregated service
@@ -858,12 +844,7 @@ private function validateServiceNameArray(array $services)
858
844
if (! isset ($ this ->services [$ service ]) ?: $ this ->allowOverride ) {
859
845
return ;
860
846
}
861
- throw new ContainerModificationsNotAllowedException (sprintf (
862
- 'The container does not allow to replace/update a service '
863
- . ' with existing instances; the following '
864
- . 'already exist in the container: %s ' ,
865
- $ service
866
- ));
847
+ throw new ContainerModificationsNotAllowedException ($ service );
867
848
}
868
849
}
869
850
@@ -906,12 +887,11 @@ private function validateServiceNameConfig(array $config)
906
887
* @param string $alias
907
888
* @param string $target
908
889
*/
909
- private function doSetAlias ($ alias , $ target )
890
+ private function mapAliasToTarget ($ alias , $ target )
910
891
{
911
892
// $target is either an alias or something else
912
893
// if it is an alias, resolve it
913
- $ this ->aliases [$ alias ] =
914
- isset ($ this ->aliases [$ target ]) ? $ this ->aliases [$ target ] : $ target ;
894
+ $ this ->aliases [$ alias ] = $ this ->aliases [$ target ] ?? $ target ;
915
895
916
896
// a self-referencing alias indicates a cycle
917
897
if ($ alias === $ this ->aliases [$ alias ]) {
@@ -920,7 +900,7 @@ private function doSetAlias($alias, $target)
920
900
921
901
// finally we have to check if existing incomplete alias definitions
922
902
// exist which can get resolved by the new alias
923
- if (in_array ($ alias , $ this ->aliases )) {
903
+ if (in_array ($ alias , $ this ->aliases , true )) {
924
904
$ r = array_intersect ($ this ->aliases , [ $ alias ]);
925
905
// found some, resolve them
926
906
foreach ($ r as $ name => $ service ) {
@@ -930,16 +910,16 @@ private function doSetAlias($alias, $target)
930
910
}
931
911
932
912
/**
933
- * Instantiate abstract factories for to avoid checks during service construction.
913
+ * Instantiate abstract factories in order to avoid checks during service construction.
934
914
*
935
915
* @param string[]|Factory\AbstractFactoryInterface[] $abstractFactories
936
916
*
937
917
* @return void
938
918
*/
939
- private function doAddAbstractFactory ($ abstractFactory )
919
+ private function resolveAbstractFactoryInstance ($ abstractFactory )
940
920
{
941
- if (\ is_string ($ abstractFactory ) && \ class_exists ($ abstractFactory )) {
942
- //Cached string
921
+ if (is_string ($ abstractFactory ) && class_exists ($ abstractFactory )) {
922
+ // cached string
943
923
if (! isset ($ this ->cachedAbstractFactories [$ abstractFactory ])) {
944
924
$ this ->cachedAbstractFactories [$ abstractFactory ] = new $ abstractFactory ();
945
925
}
@@ -948,34 +928,30 @@ private function doAddAbstractFactory($abstractFactory)
948
928
}
949
929
950
930
if ($ abstractFactory instanceof Factory \AbstractFactoryInterface) {
951
- $ abstractFactoryObjHash = \ spl_object_hash ($ abstractFactory );
931
+ $ abstractFactoryObjHash = spl_object_hash ($ abstractFactory );
952
932
$ this ->abstractFactories [$ abstractFactoryObjHash ] = $ abstractFactory ;
953
933
return ;
954
934
}
955
935
956
936
// Error condition; let's find out why.
957
937
958
938
// If we still have a string, we have a class name that does not resolve
959
- if (\is_string ($ abstractFactory )) {
960
- throw new InvalidArgumentException (
961
- sprintf (
962
- 'An invalid abstract factory was registered; resolved to class "%s" ' .
963
- 'which does not exist; please provide a valid class name resolving ' .
964
- 'to an implementation of %s ' ,
965
- $ abstractFactory ,
966
- AbstractFactoryInterface::class
967
- )
968
- );
939
+ if (is_string ($ abstractFactory )) {
940
+ throw new InvalidArgumentException (sprintf (
941
+ 'An invalid abstract factory was registered; resolved to class "%s" '
942
+ . 'which does not exist; please provide a valid class name resolving '
943
+ . 'to an implementation of %s ' ,
944
+ $ abstractFactory ,
945
+ AbstractFactoryInterface::class
946
+ ));
969
947
}
970
948
971
949
// Otherwise, we have an invalid type.
972
- throw new InvalidArgumentException (
973
- sprintf (
974
- 'An invalid abstract factory was registered. Expected an instance of "%s", ' .
975
- 'but "%s" was received ' ,
976
- AbstractFactoryInterface::class,
977
- (is_object ($ abstractFactory ) ? get_class ($ abstractFactory ) : gettype ($ abstractFactory ))
978
- )
979
- );
950
+ throw new InvalidArgumentException (sprintf (
951
+ 'An invalid abstract factory was registered. Expected an instance of "%s", '
952
+ . 'but "%s" was received ' ,
953
+ AbstractFactoryInterface::class,
954
+ (is_object ($ abstractFactory ) ? get_class ($ abstractFactory ) : gettype ($ abstractFactory ))
955
+ ));
980
956
}
981
957
}
0 commit comments