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

Commit f0f2cd3

Browse files
author
Frank Hein
authored
Merge pull request #1 from zendframework/develop
Resync with upstream
2 parents 0c328a7 + fdc7a27 commit f0f2cd3

13 files changed

+585
-323
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cache:
99
env:
1010
global:
1111
- COMPOSER_ARGS="--no-interaction"
12-
- COVERAGE_DEPS="satooshi/php-coveralls"
12+
- COVERAGE_DEPS="php-coveralls/php-coveralls"
1313

1414
matrix:
1515
include:
@@ -51,7 +51,7 @@ script:
5151
- if [[ $CS_CHECK == 'true' ]]; then composer cs-check ; fi
5252

5353
after_script:
54-
- if [[ $TEST_COVERAGE == 'true' ]]; then composer upload-coverage ; fi
54+
- if [[ $TEST_COVERAGE == 'true' ]]; then vendor/bin/php-coveralls -v ; fi
5555

5656
notifications:
5757
email: false

CHANGELOG.md

+4-1
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

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace ZendBench\ServiceManager;
9+
10+
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
11+
use PhpBench\Benchmark\Metadata\Annotations\Revs;
12+
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
13+
use Zend\ServiceManager\ServiceManager;
14+
15+
/**
16+
* @Revs(1000)
17+
* @Iterations(20)
18+
* @Warmup(2)
19+
*/
20+
class HasBench
21+
{
22+
/**
23+
* @var ServiceManager
24+
*/
25+
private $sm;
26+
27+
public function __construct()
28+
{
29+
$this->sm = new ServiceManager([
30+
'factories' => [
31+
'factory1' => BenchAsset\FactoryFoo::class,
32+
],
33+
'invokables' => [
34+
'invokable1' => BenchAsset\Foo::class,
35+
],
36+
'services' => [
37+
'service1' => new \stdClass(),
38+
],
39+
'aliases' => [
40+
'alias1' => 'service1',
41+
'recursiveAlias1' => 'alias1',
42+
'recursiveAlias2' => 'recursiveAlias1',
43+
],
44+
'abstract_factories' => [
45+
BenchAsset\AbstractFactoryFoo::class
46+
]
47+
]);
48+
}
49+
50+
public function benchHasFactory1()
51+
{
52+
// @todo @link https://github.com/phpbench/phpbench/issues/304
53+
$sm = clone $this->sm;
54+
55+
$sm->has('factory1');
56+
}
57+
58+
public function benchHasInvokable1()
59+
{
60+
// @todo @link https://github.com/phpbench/phpbench/issues/304
61+
$sm = clone $this->sm;
62+
63+
$sm->has('invokable1');
64+
}
65+
66+
public function benchHasService1()
67+
{
68+
// @todo @link https://github.com/phpbench/phpbench/issues/304
69+
$sm = clone $this->sm;
70+
71+
$sm->has('service1');
72+
}
73+
74+
public function benchHasAlias1()
75+
{
76+
// @todo @link https://github.com/phpbench/phpbench/issues/304
77+
$sm = clone $this->sm;
78+
79+
$sm->has('alias1');
80+
}
81+
82+
public function benchHasRecursiveAlias1()
83+
{
84+
// @todo @link https://github.com/phpbench/phpbench/issues/304
85+
$sm = clone $this->sm;
86+
87+
$sm->has('recursiveAlias1');
88+
}
89+
90+
public function benchHasRecursiveAlias2()
91+
{
92+
// @todo @link https://github.com/phpbench/phpbench/issues/304
93+
$sm = clone $this->sm;
94+
95+
$sm->has('recursiveAlias2');
96+
}
97+
98+
public function benchHasAbstractFactory()
99+
{
100+
// @todo @link https://github.com/phpbench/phpbench/issues/304
101+
$sm = clone $this->sm;
102+
103+
$sm->has('foo');
104+
}
105+
106+
public function benchHasNot()
107+
{
108+
// @todo @link https://github.com/phpbench/phpbench/issues/304
109+
$sm = clone $this->sm;
110+
111+
$sm->has('42');
112+
}
113+
}

benchmarks/SetNewServicesBench.php

+8
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

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
"cs-check": "phpcs",
7474
"cs-fix": "phpcbf",
7575
"test": "phpunit --colors=always",
76-
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
77-
"upload-coverage": "coveralls -v"
76+
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
7877
}
7978
}

src/AbstractPluginManager.php

+11
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

+13
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

+21-7
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

+29
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)