@@ -183,17 +183,17 @@ public function getServiceLocator()
183
183
*/
184
184
public function get ($ name )
185
185
{
186
- // We start by checking if we have cached the requested service (this
187
- // is the fastest method) .
186
+ // We start by checking if we have cached the requested service;
187
+ // this is the fastest method.
188
188
if (isset ($ this ->services [$ name ])) {
189
189
return $ this ->services [$ name ];
190
190
}
191
191
192
- // Determine if the service should be shared
192
+ // Determine if the service should be shared.
193
193
$ sharedService = isset ($ this ->shared [$ name ]) ? $ this ->shared [$ name ] : $ this ->sharedByDefault ;
194
194
195
195
// We achieve better performance if we can let all alias
196
- // considerations out
196
+ // considerations out.
197
197
if (! $ this ->aliases ) {
198
198
$ object = $ this ->doCreate ($ name );
199
199
@@ -204,27 +204,29 @@ public function get($name)
204
204
return $ object ;
205
205
}
206
206
207
- // Here we have to deal with requests which may be aliases
207
+ // We now deal with requests which may be aliases.
208
208
$ resolvedName = isset ($ this ->aliases [$ name ]) ? $ this ->aliases [$ name ] : $ name ;
209
209
210
- // Can only become true, if the requested service is an shared alias
210
+ // The following is only true if the requested service is a shared alias.
211
211
$ sharedAlias = $ sharedService && isset ($ this ->services [$ resolvedName ]);
212
- // If the alias is configured as shared service, we are done.
212
+
213
+ // If the alias is configured as a shared service, we are done.
213
214
if ($ sharedAlias ) {
214
215
$ this ->services [$ name ] = $ this ->services [$ resolvedName ];
215
216
return $ this ->services [$ resolvedName ];
216
217
}
217
218
218
- // At this point we have to create the object. We use the
219
- // resolved name for that.
219
+ // At this point, we have to create the object.
220
+ // We use the resolved name for that.
220
221
$ object = $ this ->doCreate ($ resolvedName );
221
222
222
223
// Cache the object for later, if it is supposed to be shared.
223
224
if ($ sharedService ) {
224
225
$ this ->services [$ resolvedName ] = $ object ;
225
226
}
226
227
227
- // Also do so for aliases, this allows sharing based on service name used.
228
+ // Also cache under the alias name; this allows sharing based on the
229
+ // service name used.
228
230
if ($ sharedAlias ) {
229
231
$ this ->services [$ name ] = $ object ;
230
232
}
@@ -237,7 +239,7 @@ public function get($name)
237
239
*/
238
240
public function build ($ name , array $ options = null )
239
241
{
240
- // We never cache when using "build"
242
+ // We never cache when using "build".
241
243
$ name = $ this ->aliases [$ name ] ?? $ name ;
242
244
return $ this ->doCreate ($ name , $ options );
243
245
}
@@ -247,30 +249,30 @@ public function build($name, array $options = null)
247
249
*/
248
250
public function has ($ name )
249
251
{
250
- // Check services and factories first to speedup the most common requests
252
+ // Check services and factories first to speedup the most common requests.
251
253
if (isset ($ this ->services [$ name ]) || isset ($ this ->factories [$ name ])) {
252
254
return true ;
253
255
}
254
256
255
- // Check abstract factories next
257
+ // Check abstract factories next.
256
258
foreach ($ this ->abstractFactories as $ abstractFactory ) {
257
259
if ($ abstractFactory ->canCreate ($ this ->creationContext , $ name )) {
258
260
return true ;
259
261
}
260
262
}
261
263
262
- // If $name is no alias, we are done
264
+ // If $name is not an alias, we are done.
263
265
if (! isset ($ this ->aliases [$ name ])) {
264
266
return false ;
265
267
}
266
268
267
- // Finally check aliases
269
+ // Check aliases.
268
270
$ resolvedName = $ this ->aliases [$ name ];
269
271
if (isset ($ this ->services [$ resolvedName ]) || isset ($ this ->factories [$ resolvedName ])) {
270
272
return true ;
271
273
}
272
274
273
- // Check abstract factories on $resolvedName also
275
+ // Check abstract factories on the $resolvedName as well.
274
276
foreach ($ this ->abstractFactories as $ abstractFactory ) {
275
277
if ($ abstractFactory ->canCreate ($ this ->creationContext , $ resolvedName )) {
276
278
return true ;
@@ -340,8 +342,8 @@ public function getAllowOverride()
340
342
*/
341
343
public function configure (array $ config )
342
344
{
343
- // This is a bulk update/initial configuration
344
- // So we check all definitions upfront
345
+ // This is a bulk update/initial configuration,
346
+ // so we check all definitions up front.
345
347
$ this ->validateServiceNames ($ config );
346
348
347
349
if (isset ($ config ['services ' ])) {
@@ -386,7 +388,7 @@ public function configure(array $config)
386
388
// instantiate them to avoid checks during service construction.
387
389
if (isset ($ config ['abstract_factories ' ])) {
388
390
$ abstractFactories = $ config ['abstract_factories ' ];
389
- // $key not needed, but foreach faster
391
+ // $key not needed, but foreach is faster than foreach + array_values.
390
392
foreach ($ abstractFactories as $ key => $ abstractFactory ) {
391
393
$ this ->resolveAbstractFactoryInstance ($ abstractFactory );
392
394
}
@@ -406,14 +408,16 @@ public function configure(array $config)
406
408
*
407
409
* @param string $alias
408
410
* @param string $target
411
+ * @throws ContainerModificationsNotAllowedException if $alias already
412
+ * exists as a service and overrides are disallowed.
409
413
*/
410
414
public function setAlias ($ alias , $ target )
411
415
{
412
- if (! isset ($ this ->services [$ alias ]) || $ this ->allowOverride ) {
413
- $ this ->mapAliasToTarget ($ alias , $ target );
414
- return ;
416
+ if (isset ($ this ->services [$ alias ]) && ! $ this ->allowOverride ) {
417
+ throw ContainerModificationsNotAllowedException::fromExistingService ($ alias );
415
418
}
416
- throw ContainerModificationsNotAllowedException::fromExistingService ($ alias );
419
+
420
+ $ this ->mapAliasToTarget ($ alias , $ target );
417
421
}
418
422
419
423
/**
@@ -422,14 +426,16 @@ public function setAlias($alias, $target)
422
426
* @param string $name Service name
423
427
* @param null|string $class Class to which to map; if omitted, $name is
424
428
* assumed.
429
+ * @throws ContainerModificationsNotAllowedException if $name already
430
+ * exists as a service and overrides are disallowed.
425
431
*/
426
432
public function setInvokableClass ($ name , $ class = null )
427
433
{
428
- if (! isset ($ this ->services [$ name ]) || $ this ->allowOverride ) {
429
- $ this ->createAliasesAndFactoriesForInvokables ([$ name => $ class ?? $ name ]);
430
- return ;
434
+ if (isset ($ this ->services [$ name ]) && ! $ this ->allowOverride ) {
435
+ throw ContainerModificationsNotAllowedException::fromExistingService ($ name );
431
436
}
432
- throw ContainerModificationsNotAllowedException::fromExistingService ($ name );
437
+
438
+ $ this ->createAliasesAndFactoriesForInvokables ([$ name => $ class ?? $ name ]);
433
439
}
434
440
435
441
/**
@@ -438,14 +444,16 @@ public function setInvokableClass($name, $class = null)
438
444
* @param string $name Service name
439
445
* @param string|callable|Factory\FactoryInterface $factory Factory to which
440
446
* to map.
447
+ * @throws ContainerModificationsNotAllowedException if $name already
448
+ * exists as a service and overrides are disallowed.
441
449
*/
442
450
public function setFactory ($ name , $ factory )
443
451
{
444
- if (! isset ($ this ->services [$ name ]) || $ this ->allowOverride ) {
445
- $ this ->factories [$ name ] = $ factory ;
446
- return ;
452
+ if (isset ($ this ->services [$ name ]) && ! $ this ->allowOverride ) {
453
+ throw ContainerModificationsNotAllowedException::fromExistingService ($ name );
447
454
}
448
- throw ContainerModificationsNotAllowedException::fromExistingService ($ name );
455
+
456
+ $ this ->factories [$ name ] = $ factory ;
449
457
}
450
458
451
459
/**
@@ -463,7 +471,8 @@ public function mapLazyService($name, $class = null)
463
471
/**
464
472
* Add an abstract factory for resolving services.
465
473
*
466
- * @param string|Factory\AbstractFactoryInterface $factory Service name
474
+ * @param string|Factory\AbstractFactoryInterface $factory Abstract factory
475
+ * instance or class name.
467
476
*/
468
477
public function addAbstractFactory ($ factory )
469
478
{
@@ -497,29 +506,32 @@ public function addInitializer($initializer)
497
506
*
498
507
* @param string $name Service name
499
508
* @param array|object $service
509
+ * @throws ContainerModificationsNotAllowedException if $name already
510
+ * exists as a service and overrides are disallowed.
500
511
*/
501
512
public function setService ($ name , $ service )
502
513
{
503
- if (! isset ($ this ->services [$ name ]) || $ this ->allowOverride ) {
504
- $ this ->services [$ name ] = $ service ;
505
- return ;
514
+ if (isset ($ this ->services [$ name ]) && ! $ this ->allowOverride ) {
515
+ throw ContainerModificationsNotAllowedException::fromExistingService ($ name );
506
516
}
507
- throw ContainerModificationsNotAllowedException:: fromExistingService ( $ name) ;
517
+ $ this -> services [ $ name] = $ service ;
508
518
}
509
519
510
520
/**
511
521
* Add a service sharing rule.
512
522
*
513
523
* @param string $name Service name
514
524
* @param boolean $flag Whether or not the service should be shared.
525
+ * @throws ContainerModificationsNotAllowedException if $name already
526
+ * exists as a service and overrides are disallowed.
515
527
*/
516
528
public function setShared ($ name , $ flag )
517
529
{
518
- if (! isset ($ this ->services [$ name ]) || $ this ->allowOverride ) {
519
- $ this ->shared [$ name ] = (bool ) $ flag ;
520
- return ;
530
+ if (isset ($ this ->services [$ name ]) && ! $ this ->allowOverride ) {
531
+ throw ContainerModificationsNotAllowedException::fromExistingService ($ name );
521
532
}
522
- throw ContainerModificationsNotAllowedException::fromExistingService ($ name );
533
+
534
+ $ this ->shared [$ name ] = (bool ) $ flag ;
523
535
}
524
536
525
537
/**
@@ -608,9 +620,9 @@ private function createDelegatorFromName($name, array $options = null)
608
620
if (! is_callable ($ delegatorFactory )) {
609
621
if (is_string ($ delegatorFactory )) {
610
622
throw new ServiceNotCreatedException (sprintf (
611
- 'An invalid delegator factory was registered; resolved to class or function "%s" '
612
- . 'which does not exist; please provide a valid function name or class name resolving '
613
- . 'to an implementation of %s ' ,
623
+ 'An invalid delegator factory was registered; resolved to class or function "%s" '
624
+ . ' which does not exist; please provide a valid function name or class name resolving '
625
+ . ' to an implementation of %s ' ,
614
626
$ delegatorFactory ,
615
627
DelegatorFactoryInterface::class
616
628
));
@@ -762,64 +774,57 @@ private function validateServiceNames(array $config)
762
774
763
775
if (isset ($ config ['services ' ])) {
764
776
foreach ($ config ['services ' ] as $ service => $ _ ) {
765
- if (! isset ($ this ->services [$ service ]) || $ this ->allowOverride ) {
766
- continue ;
777
+ if (isset ($ this ->services [$ service ]) && ! $ this ->allowOverride ) {
778
+ throw ContainerModificationsNotAllowedException:: fromExistingService ( $ service ) ;
767
779
}
768
- throw ContainerModificationsNotAllowedException::fromExistingService ($ service );
769
780
}
770
781
}
771
782
772
783
if (isset ($ config ['aliases ' ])) {
773
784
foreach ($ config ['aliases ' ] as $ service => $ _ ) {
774
- if (! isset ($ this ->services [$ service ]) || $ this ->allowOverride ) {
775
- continue ;
785
+ if (isset ($ this ->services [$ service ]) && ! $ this ->allowOverride ) {
786
+ throw ContainerModificationsNotAllowedException:: fromExistingService ( $ service ) ;
776
787
}
777
- throw ContainerModificationsNotAllowedException::fromExistingService ($ service );
778
788
}
779
789
}
780
790
781
791
if (isset ($ config ['invokables ' ])) {
782
792
foreach ($ config ['invokables ' ] as $ service => $ _ ) {
783
- if (! isset ($ this ->services [$ service ]) || $ this ->allowOverride ) {
784
- continue ;
793
+ if (isset ($ this ->services [$ service ]) && ! $ this ->allowOverride ) {
794
+ throw ContainerModificationsNotAllowedException:: fromExistingService ( $ service ) ;
785
795
}
786
- throw ContainerModificationsNotAllowedException::fromExistingService ($ service );
787
796
}
788
797
}
789
798
790
799
if (isset ($ config ['factories ' ])) {
791
800
foreach ($ config ['factories ' ] as $ service => $ _ ) {
792
- if (! isset ($ this ->services [$ service ]) || $ this ->allowOverride ) {
793
- continue ;
801
+ if (isset ($ this ->services [$ service ]) && ! $ this ->allowOverride ) {
802
+ throw ContainerModificationsNotAllowedException:: fromExistingService ( $ service ) ;
794
803
}
795
- throw ContainerModificationsNotAllowedException::fromExistingService ($ service );
796
804
}
797
805
}
798
806
799
807
if (isset ($ config ['delegators ' ])) {
800
808
foreach ($ config ['delegators ' ] as $ service => $ _ ) {
801
- if (! isset ($ this ->services [$ service ]) || $ this ->allowOverride ) {
802
- continue ;
809
+ if (isset ($ this ->services [$ service ]) && ! $ this ->allowOverride ) {
810
+ throw ContainerModificationsNotAllowedException:: fromExistingService ( $ service ) ;
803
811
}
804
- throw ContainerModificationsNotAllowedException::fromExistingService ($ service );
805
812
}
806
813
}
807
814
808
815
if (isset ($ config ['shared ' ])) {
809
816
foreach ($ config ['shared ' ] as $ service => $ _ ) {
810
- if (! isset ($ this ->services [$ service ]) || $ this ->allowOverride ) {
811
- continue ;
817
+ if (isset ($ this ->services [$ service ]) && ! $ this ->allowOverride ) {
818
+ throw ContainerModificationsNotAllowedException:: fromExistingService ( $ service ) ;
812
819
}
813
- throw ContainerModificationsNotAllowedException::fromExistingService ($ service );
814
820
}
815
821
}
816
822
817
823
if (isset ($ config ['lazy_services ' ]['class_map ' ])) {
818
824
foreach ($ config ['lazy_services ' ]['class_map ' ] as $ service => $ _ ) {
819
- if (! isset ($ this ->services [$ service ]) || $ this ->allowOverride ) {
820
- continue ;
825
+ if (isset ($ this ->services [$ service ]) && ! $ this ->allowOverride ) {
826
+ throw ContainerModificationsNotAllowedException:: fromExistingService ( $ service ) ;
821
827
}
822
- throw ContainerModificationsNotAllowedException::fromExistingService ($ service );
823
828
}
824
829
}
825
830
}
@@ -900,20 +905,19 @@ private function mapAliasesToTargets()
900
905
private function resolveAbstractFactoryInstance ($ abstractFactory )
901
906
{
902
907
if (is_string ($ abstractFactory ) && class_exists ($ abstractFactory )) {
903
- // cached string
908
+ // Cached string factory name
904
909
if (! isset ($ this ->cachedAbstractFactories [$ abstractFactory ])) {
905
910
$ this ->cachedAbstractFactories [$ abstractFactory ] = new $ abstractFactory ();
906
911
}
907
912
908
913
$ abstractFactory = $ this ->cachedAbstractFactories [$ abstractFactory ];
909
914
}
910
915
911
- if ($ abstractFactory instanceof Factory \AbstractFactoryInterface) {
912
- $ abstractFactoryObjHash = spl_object_hash ($ abstractFactory );
913
- $ this ->abstractFactories [$ abstractFactoryObjHash ] = $ abstractFactory ;
914
- return ;
916
+ if (! $ abstractFactory instanceof Factory \AbstractFactoryInterface) {
917
+ throw InvalidArgumentException::fromInvalidAbstractFactory ($ abstractFactory );
915
918
}
916
919
917
- throw InvalidArgumentException::fromInvalidAbstractFactory ($ abstractFactory );
920
+ $ abstractFactoryObjHash = spl_object_hash ($ abstractFactory );
921
+ $ this ->abstractFactories [$ abstractFactoryObjHash ] = $ abstractFactory ;
918
922
}
919
923
}
0 commit comments