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

Commit fdc7a27

Browse files
committed
Merge branch 'feature/221' into develop
Close #221
2 parents c7fd498 + 6fd10b3 commit fdc7a27

11 files changed

+448
-332
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ All notable changes to this project will be documented in this file, in reverse
1010

1111
### Changed
1212

13-
- Nothing.
13+
- [#221](https://github.com/zendframework/zend-servicemanager/pull/221) provides
14+
enormous performance improvements for each of the various mutator methods
15+
(`setAlias()`, `setFactory()`, etc.), `has()` lookups, and initial
16+
container configuration.
1417

1518
### Deprecated
1619

benchmarks/HasBench.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ public function __construct()
4545
BenchAsset\AbstractFactoryFoo::class
4646
]
4747
]);
48-
49-
// forcing initialization of all the services
50-
$this->sm->get('factory1');
51-
$this->sm->get('invokable1');
52-
$this->sm->get('service1');
53-
$this->sm->get('alias1');
54-
$this->sm->get('recursiveAlias1');
55-
$this->sm->get('recursiveAlias2');
5648
}
5749

5850
public function benchHasFactory1()

benchmarks/SetNewServicesBench.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public function __construct()
5656
$this->sm = new ServiceManager($config);
5757
}
5858

59+
public function benchSetService()
60+
{
61+
// @todo @link https://github.com/phpbench/phpbench/issues/304
62+
$sm = clone $this->sm;
63+
64+
$sm->setService('service2', new \stdClass());
65+
}
66+
5967
public function benchSetFactory()
6068
{
6169
// @todo @link https://github.com/phpbench/phpbench/issues/304

src/AbstractPluginManager.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ public function configure(array $config)
120120
return $this;
121121
}
122122

123+
/**
124+
* Override setService for additional plugin validation.
125+
*
126+
* {@inheritDoc}
127+
*/
128+
public function setService($name, $service)
129+
{
130+
$this->validate($service);
131+
parent::setService($name, $service);
132+
}
133+
123134
/**
124135
* {@inheritDoc}
125136
*

src/Exception/ContainerModificationsNotAllowedException.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,17 @@
1414
*/
1515
class ContainerModificationsNotAllowedException extends DomainException implements ExceptionInterface
1616
{
17+
/**
18+
* @param string $service Name of service that already exists.
19+
* @return self
20+
*/
21+
public static function fromExistingService($service)
22+
{
23+
return new self(sprintf(
24+
'The container does not allow replacing or updating a service'
25+
. ' with existing instances; the following service'
26+
. ' already exists in the container: %s',
27+
$service
28+
));
29+
}
1730
}

src/Exception/CyclicAliasException.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,29 @@
1919

2020
class CyclicAliasException extends InvalidArgumentException
2121
{
22+
/**
23+
* @param string $alias conflicting alias key
24+
* @param string[] $aliases map of referenced services, indexed by alias name (string)
25+
* @return self
26+
*/
27+
public static function fromCyclicAlias($alias, array $aliases)
28+
{
29+
$cycle = $alias;
30+
$cursor = $alias;
31+
while (isset($aliases[$cursor]) && $aliases[$cursor] !== $alias) {
32+
$cursor = $aliases[$cursor];
33+
$cycle .= ' -> '. $cursor;
34+
}
35+
$cycle .= ' -> ' . $alias . "\n";
36+
37+
return new self(sprintf(
38+
"A cycle was detected within the aliases definitions:\n%s",
39+
$cycle
40+
));
41+
}
42+
2243
/**
2344
* @param string[] $aliases map of referenced services, indexed by alias name (string)
24-
*
2545
* @return self
2646
*/
2747
public static function fromAliasesMap(array $aliases)
@@ -53,7 +73,6 @@ function ($alias) use ($aliases) {
5373
*
5474
* @param string[] $aliases
5575
* @param string $alias
56-
*
5776
* @return array|null
5877
*/
5978
private static function getCycleFor(array $aliases, $alias)
@@ -67,7 +86,6 @@ private static function getCycleFor(array $aliases, $alias)
6786
}
6887

6988
$cycleCandidate[$targetName] = true;
70-
7189
$targetName = $aliases[$targetName];
7290
}
7391

@@ -76,7 +94,6 @@ private static function getCycleFor(array $aliases, $alias)
7694

7795
/**
7896
* @param string[] $aliases
79-
*
8097
* @return string
8198
*/
8299
private static function printReferencesMap(array $aliases)
@@ -92,7 +109,6 @@ private static function printReferencesMap(array $aliases)
92109

93110
/**
94111
* @param string[][] $detectedCycles
95-
*
96112
* @return string
97113
*/
98114
private static function printCycles(array $detectedCycles)
@@ -102,7 +118,6 @@ private static function printCycles(array $detectedCycles)
102118

103119
/**
104120
* @param string[] $detectedCycle
105-
*
106121
* @return string
107122
*/
108123
private static function printCycle(array $detectedCycle)
@@ -123,7 +138,6 @@ function ($cycle) {
123138

124139
/**
125140
* @param bool[][] $detectedCycles
126-
*
127141
* @return bool[][] de-duplicated
128142
*/
129143
private static function deDuplicateDetectedCycles(array $detectedCycles)

src/Exception/InvalidArgumentException.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,39 @@
88
namespace Zend\ServiceManager\Exception;
99

1010
use InvalidArgumentException as SplInvalidArgumentException;
11+
use Zend\ServiceManager\AbstractFactoryInterface;
12+
use Zend\ServiceManager\Initializer\InitializerInterface;
1113

1214
/**
1315
* @inheritDoc
1416
*/
1517
class InvalidArgumentException extends SplInvalidArgumentException implements ExceptionInterface
1618
{
19+
/**
20+
* @param mixed $initializer
21+
* @return self
22+
*/
23+
public static function fromInvalidInitializer($initializer)
24+
{
25+
return new self(sprintf(
26+
'An invalid initializer was registered. Expected a callable or an'
27+
. ' instance of "%s"; received "%s"',
28+
InitializerInterface::class,
29+
is_object($initializer) ? get_class($initializer) : gettype($initializer)
30+
));
31+
}
32+
33+
/**
34+
* @param mixed $abstractFactory
35+
* @return self
36+
*/
37+
public static function fromInvalidAbstractFactory($abstractFactory)
38+
{
39+
return new self(sprintf(
40+
'An invalid abstract factory was registered. Expected an instance of or a valid'
41+
. ' class name resolving to an implementation of "%s", but "%s" was received.',
42+
AbstractFactoryInterface::class,
43+
is_object($abstractFactory) ? get_class($abstractFactory) : gettype($abstractFactory)
44+
));
45+
}
1746
}

0 commit comments

Comments
 (0)