Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 49bfc47

Browse files
author
fhein
committed
Corrected overall formatting of sprintfs. Reviewed an fixed all imports
of global functions. Renamed members as requested. Introduced constructor for ContainerModificationsNotAllowedException.
1 parent 021acbc commit 49bfc47

File tree

2 files changed

+86
-100
lines changed

2 files changed

+86
-100
lines changed

src/Exception/ContainerModificationsNotAllowedException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@
1414
*/
1515
class ContainerModificationsNotAllowedException extends DomainException implements ExceptionInterface
1616
{
17+
18+
public function __construct($service)
19+
{
20+
parent::__construct(sprintf(
21+
'The container does not allow to replace/update a service'
22+
. ' with existing instances; the following '
23+
. 'already exist in the container: %s',
24+
$service
25+
));
26+
}
1727
}

src/ServiceManager.php

Lines changed: 76 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@
2121
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
2222
use Zend\ServiceManager\Exception\ServiceNotFoundException;
2323

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;
3738

3839
/**
3940
* Service Manager.
@@ -105,11 +106,6 @@ class ServiceManager implements ServiceLocatorInterface
105106
*/
106107
private $lazyServicesDelegator;
107108

108-
// /**
109-
// * @var string[]
110-
// */
111-
// private $resolvedAliases = [];
112-
113109
/**
114110
* A list of already loaded services (this act as a local cache)
115111
*
@@ -204,7 +200,7 @@ public function get($name)
204200
$object = $this->doCreate($name);
205201

206202
// Cache the object for later, if it is supposed to be shared.
207-
if (($sharedService)) {
203+
if ($sharedService) {
208204
$this->services[$name] = $object;
209205
}
210206
return $object;
@@ -229,8 +225,8 @@ public function get($name)
229225
if (($sharedService)) {
230226
$this->services[$resolvedName] = $object;
231227
}
228+
232229
// 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
234230
if ($sharedAlias) {
235231
$this->services[$name] = $object;
236232
}
@@ -351,12 +347,12 @@ public function configure(array $config)
351347

352348
if (! empty($aliases)) {
353349
$config['aliases'] = (isset($config['aliases']))
354-
? \array_merge($config['aliases'], $aliases)
350+
? array_merge($config['aliases'], $aliases)
355351
: $aliases;
356352
}
357353

358354
$config['factories'] = (isset($config['factories']))
359-
? \array_merge($config['factories'], $factories)
355+
? array_merge($config['factories'], $factories)
360356
: $factories;
361357
}
362358

@@ -365,7 +361,7 @@ public function configure(array $config)
365361
}
366362

367363
if (isset($config['delegators'])) {
368-
$this->delegators = \array_merge_recursive($this->delegators, $config['delegators']);
364+
$this->delegators = array_merge_recursive($this->delegators, $config['delegators']);
369365
}
370366

371367
if (isset($config['shared'])) {
@@ -380,7 +376,7 @@ public function configure(array $config)
380376
}
381377
if (! empty($aliases)) {
382378
foreach ($aliases as $alias => $target) {
383-
$this->doSetAlias($alias, $target);
379+
$this->mapAliasToTarget($alias, $target);
384380
}
385381
}
386382

@@ -391,7 +387,7 @@ public function configure(array $config)
391387
// If lazy service configuration was provided, reset the lazy services
392388
// delegator factory.
393389
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']);
395391
$this->lazyServicesDelegator = null;
396392
}
397393

@@ -401,7 +397,7 @@ public function configure(array $config)
401397
$abstractFactories = $config['abstract_factories'];
402398
// $key not needed, but foreach faster
403399
foreach ($abstractFactories as $key => $abstractFactory) {
404-
$this->doAddAbstractFactory($abstractFactory);
400+
$this->resolveAbstractFactoryInstance($abstractFactory);
405401
}
406402
}
407403

@@ -423,7 +419,7 @@ public function configure(array $config)
423419
public function setAlias($alias, $target)
424420
{
425421
$this->validateServiceName($alias);
426-
$this->doSetAlias($alias, $target);
422+
$this->mapAliasToTarget($alias, $target);
427423
}
428424

429425
/**
@@ -470,7 +466,7 @@ public function mapLazyService($name, $class = null)
470466
*/
471467
public function addAbstractFactory($factory)
472468
{
473-
$this->doAddAbstractFactory($factory);
469+
$this->resolveAbstractFactoryInstance($factory);
474470
}
475471

476472
/**
@@ -528,39 +524,35 @@ public function setShared($name, $flag)
528524
*/
529525
private function resolveInitializer($initializer)
530526
{
531-
if (\is_string($initializer) && \class_exists($initializer)) {
527+
if (is_string($initializer) && class_exists($initializer)) {
532528
$initializer = new $initializer();
533529
}
534530

535-
if (\is_callable($initializer)) {
531+
if (is_callable($initializer)) {
536532
$this->initializers[] = $initializer;
537533
return;
538534
}
539535

540536
// Error condition; let's find out why.
541537

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+
));
552546
}
553547

554548
// 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+
));
564556
}
565557

566558
/**
@@ -589,12 +581,12 @@ private function getFactory($name)
589581
$factory = $this->factories[$name] ?? null;
590582

591583
$lazyLoaded = false;
592-
if (\is_string($factory) && \class_exists($factory)) {
584+
if (is_string($factory) && class_exists($factory)) {
593585
$factory = new $factory();
594586
$lazyLoaded = true;
595587
}
596588

597-
if (\is_callable($factory)) {
589+
if (is_callable($factory)) {
598590
if ($lazyLoaded) {
599591
$this->factories[$name] = $factory;
600592
}
@@ -635,13 +627,13 @@ private function createDelegatorFromName($name, array $options = null)
635627
$delegatorFactory = $this->createLazyServiceDelegatorFactory();
636628
}
637629

638-
if (\is_string($delegatorFactory) && \class_exists($delegatorFactory)) {
630+
if (is_string($delegatorFactory) && class_exists($delegatorFactory)) {
639631
$delegatorFactory = new $delegatorFactory();
640632
}
641633

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(
645637
'An invalid delegator factory was registered; resolved to class or function "%s" '
646638
. 'which does not exist; please provide a valid function name or class name resolving '
647639
. 'to an implementation of %s',
@@ -650,9 +642,9 @@ private function createDelegatorFromName($name, array $options = null)
650642
));
651643
}
652644

653-
throw new ServiceNotCreatedException(\sprintf(
645+
throw new ServiceNotCreatedException(sprintf(
654646
'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),
656648
DelegatorFactoryInterface::class
657649
));
658650
}
@@ -745,7 +737,7 @@ private function createLazyServiceDelegatorFactory()
745737
));
746738
}
747739

748-
\spl_autoload_register($factoryConfig->getProxyAutoloader());
740+
spl_autoload_register($factoryConfig->getProxyAutoloader());
749741

750742
$this->lazyServicesDelegator = new Proxy\LazyServiceFactory(
751743
new LazyLoadingValueHolderFactory($factoryConfig),
@@ -818,15 +810,10 @@ private function validateServiceName($service)
818810
// Important: Next three lines must kept equal to the three
819811
// lines of validateServiceNameArray (see below) which are marked as code
820812
// duplicate!
821-
if (! isset($this->services[$service]) ?: $this->allowOverride) {
813+
if (! isset($this->services[$service]) || $this->allowOverride) {
822814
return;
823815
}
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);
830817
}
831818

832819
/**
@@ -845,8 +832,7 @@ private function validateServiceName($service)
845832
*/
846833
private function validateServiceNameArray(array $services)
847834
{
848-
$keys = \array_keys($services);
849-
foreach ($keys as $service) {
835+
foreach (array_keys($services) as $service) {
850836
// This is a code duplication from validateServiceName (see above).
851837
// validateServiceName is almost a one liner, so we reproduce it
852838
// here for the sake of performance of aggregated service
@@ -858,12 +844,7 @@ private function validateServiceNameArray(array $services)
858844
if (! isset($this->services[$service]) ?: $this->allowOverride) {
859845
return;
860846
}
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);
867848
}
868849
}
869850

@@ -906,12 +887,11 @@ private function validateServiceNameConfig(array $config)
906887
* @param string $alias
907888
* @param string $target
908889
*/
909-
private function doSetAlias($alias, $target)
890+
private function mapAliasToTarget($alias, $target)
910891
{
911892
// $target is either an alias or something else
912893
// 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;
915895

916896
// a self-referencing alias indicates a cycle
917897
if ($alias === $this->aliases[$alias]) {
@@ -920,7 +900,7 @@ private function doSetAlias($alias, $target)
920900

921901
// finally we have to check if existing incomplete alias definitions
922902
// exist which can get resolved by the new alias
923-
if (in_array($alias, $this->aliases)) {
903+
if (in_array($alias, $this->aliases, true)) {
924904
$r = array_intersect($this->aliases, [ $alias ]);
925905
// found some, resolve them
926906
foreach ($r as $name => $service) {
@@ -930,16 +910,16 @@ private function doSetAlias($alias, $target)
930910
}
931911

932912
/**
933-
* Instantiate abstract factories for to avoid checks during service construction.
913+
* Instantiate abstract factories in order to avoid checks during service construction.
934914
*
935915
* @param string[]|Factory\AbstractFactoryInterface[] $abstractFactories
936916
*
937917
* @return void
938918
*/
939-
private function doAddAbstractFactory($abstractFactory)
919+
private function resolveAbstractFactoryInstance($abstractFactory)
940920
{
941-
if (\is_string($abstractFactory) && \class_exists($abstractFactory)) {
942-
//Cached string
921+
if (is_string($abstractFactory) && class_exists($abstractFactory)) {
922+
// cached string
943923
if (! isset($this->cachedAbstractFactories[$abstractFactory])) {
944924
$this->cachedAbstractFactories[$abstractFactory] = new $abstractFactory();
945925
}
@@ -948,34 +928,30 @@ private function doAddAbstractFactory($abstractFactory)
948928
}
949929

950930
if ($abstractFactory instanceof Factory\AbstractFactoryInterface) {
951-
$abstractFactoryObjHash = \spl_object_hash($abstractFactory);
931+
$abstractFactoryObjHash = spl_object_hash($abstractFactory);
952932
$this->abstractFactories[$abstractFactoryObjHash] = $abstractFactory;
953933
return;
954934
}
955935

956936
// Error condition; let's find out why.
957937

958938
// 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+
));
969947
}
970948

971949
// 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+
));
980956
}
981957
}

0 commit comments

Comments
 (0)