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

Commit 90b8833

Browse files
committed
Merge branch 'develop'
2 parents f89f7d9 + b4ad11e commit 90b8833

File tree

8 files changed

+202
-15
lines changed

8 files changed

+202
-15
lines changed

.coveralls.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage_clover: clover.xml
22
json_path: coveralls-upload.json
3-
src_dir: src

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ matrix:
3535
- php: 7
3636
- php: hhvm
3737
allow_failures:
38-
- php: 7
3938
- php: hhvm
4039

4140
notifications:
@@ -45,10 +44,10 @@ notifications:
4544
before_install:
4645
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
4746
- composer self-update
48-
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls ; fi
47+
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls:^1.0 ; fi
4948

5049
install:
51-
- travis_retry composer install --no-interaction --ignore-platform-reqs
50+
- travis_retry composer install --no-interaction
5251

5352
script:
5453
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi
@@ -60,4 +59,4 @@ after_success:
6059
- if [[ $DEPLOY_DOCS == "true" ]]; then echo "Preparing to build and deploy documentation" ; ./zf-mkdoc-theme/deploy.sh ; echo "Completed deploying documentation" ; fi
6160

6261
after_script:
63-
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls ; fi
62+
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls -v ; fi

CHANGELOG.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,37 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 3.1.0 - 2016-06-01
6+
7+
### Added
8+
9+
- [#103](https://github.com/zendframework/zend-servicemanager/pull/103) Allowing
10+
installation of `ocramius/proxy-manager` `^2.0` together with
11+
`zendframework/zend-servicemanager`.
12+
- [#103](https://github.com/zendframework/zend-servicemanager/pull/103) Disallowing
13+
test failures when running tests against PHP `7.0.*`.
14+
- [#113](https://github.com/zendframework/zend-servicemanager/pull/113) Improved performance
15+
when dealing with registering aliases and factories via `ServiceManager#setFactory()` and
16+
`ServiceManager#setAlias()`
17+
- [#120](https://github.com/zendframework/zend-servicemanager/pull/120) The
18+
`zendframework/zend-servicemanager` component now provides a
19+
`container-interop/container-interop-implementation` implementation
20+
21+
### Deprecated
22+
23+
- Nothing.
24+
25+
### Removed
26+
27+
- Nothing.
28+
29+
### Fixed
30+
31+
- [#97](https://github.com/zendframework/zend-servicemanager/pull/97) Typo corrections
32+
in the delegator factories documentation.
33+
- [#98](https://github.com/zendframework/zend-servicemanager/pull/98) Using coveralls ^1.0
34+
for tracking test code coverage changes.
35+
536
## 3.0.4 - TBD
637

738
### Added

benchmarks/SetNewServicesBench.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace ZendBench\ServiceManager;
4+
5+
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
6+
use PhpBench\Benchmark\Metadata\Annotations\Revs;
7+
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
8+
use Zend\ServiceManager\ServiceManager;
9+
10+
/**
11+
* @Revs(1000)
12+
* @Iterations(10)
13+
* @Warmup(2)
14+
*/
15+
class SetNewServicesBench
16+
{
17+
const NUM_SERVICES = 100;
18+
19+
/**
20+
* @var ServiceManager
21+
*/
22+
private $sm;
23+
24+
public function __construct()
25+
{
26+
$config = [
27+
'factories' => [
28+
'factory1' => BenchAsset\FactoryFoo::class,
29+
],
30+
'invokables' => [
31+
'invokable1' => BenchAsset\Foo::class,
32+
],
33+
'services' => [
34+
'service1' => new \stdClass(),
35+
],
36+
'aliases' => [
37+
'factoryAlias1' => 'factory1',
38+
'recursiveFactoryAlias1' => 'factoryAlias1',
39+
'recursiveFactoryAlias2' => 'recursiveFactoryAlias1',
40+
],
41+
'abstract_factories' => [
42+
BenchAsset\AbstractFactoryFoo::class
43+
],
44+
];
45+
46+
for ($i = 0; $i <= self::NUM_SERVICES; $i++) {
47+
$config['factories']["factory_$i"] = BenchAsset\FactoryFoo::class;
48+
$config['aliases']["alias_$i"] = "service_$i";
49+
}
50+
51+
$this->sm = new ServiceManager($config);
52+
}
53+
54+
public function benchSetFactory()
55+
{
56+
// @todo @link https://github.com/phpbench/phpbench/issues/304
57+
$sm = clone $this->sm;
58+
59+
$sm->setFactory('factory2', BenchAsset\FactoryFoo::class);
60+
}
61+
62+
public function benchSetAlias()
63+
{
64+
// @todo @link https://github.com/phpbench/phpbench/issues/304
65+
$sm = clone $this->sm;
66+
67+
$sm->setAlias('factoryAlias2', 'factory1');
68+
}
69+
70+
public function benchSetAliasOverrided()
71+
{
72+
// @todo @link https://github.com/phpbench/phpbench/issues/304
73+
$sm = clone $this->sm;
74+
75+
$sm->setAlias('recursiveFactoryAlias1', 'factory1');
76+
}
77+
}

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
"container-interop/container-interop": "~1.0"
1919
},
2020
"require-dev": {
21-
"phpunit/phpunit": "~4.6",
22-
"ocramius/proxy-manager": "~1.0",
23-
"squizlabs/php_codesniffer": "^2.0@dev",
21+
"phpunit/phpunit": "^4.6 || ^5.2.10",
22+
"ocramius/proxy-manager": "^1.0 || ^2.0",
23+
"squizlabs/php_codesniffer": "^2.5.1",
2424
"phpbench/phpbench": "^0.10.0"
2525
},
2626
"suggest": {
@@ -40,5 +40,8 @@
4040
"ZendTest\\ServiceManager\\": "test/",
4141
"ZendBench\\ServiceManager\\": "benchmarks/"
4242
}
43+
},
44+
"provide": {
45+
"container-interop/container-interop-implementation": "^1.1"
4346
}
4447
}

doc/book/delegators.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ The parameters passed to the delegator factory are the following:
2929
- `$container` is the service locator that is used while creating the delegator
3030
for the requested service.
3131
- `$name` is the name of the service being requested.
32-
- `$callback` is a
33-
- [callable](http://www.php.net/manual/en/language.types.callable.php) that is
32+
- `$callback` is a [callable](http://www.php.net/manual/en/language.types.callable.php) that is
3433
responsible for instantiating the delegated service (the real service instance).
3534
- `$options` is an array of options to use when creating the instance; these are
3635
typically used only during `build()` operations.

src/ServiceManager.php

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Interop\Container\Exception\ContainerException;
1515
use ProxyManager\Configuration as ProxyConfiguration;
1616
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
17+
use ProxyManager\FileLocator\FileLocator;
1718
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
19+
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
1820
use Zend\ServiceManager\Exception\ContainerModificationsNotAllowedException;
1921
use Zend\ServiceManager\Exception\CyclicAliasException;
2022
use Zend\ServiceManager\Exception\InvalidArgumentException;
@@ -341,10 +343,8 @@ public function configure(array $config)
341343
}
342344

343345
if (isset($config['aliases'])) {
344-
$this->aliases = $config['aliases'] + $this->aliases;
345-
}
346-
347-
if (! empty($this->aliases)) {
346+
$this->configureAliases($config['aliases']);
347+
} elseif (! $this->configured && ! empty($this->aliases)) {
348348
$this->resolveAliases($this->aliases);
349349
}
350350

@@ -374,6 +374,35 @@ public function configure(array $config)
374374
return $this;
375375
}
376376

377+
/**
378+
* @param string[] $aliases
379+
*
380+
* @return void
381+
*/
382+
private function configureAliases(array $aliases)
383+
{
384+
if (! $this->configured) {
385+
$this->aliases = $aliases + $this->aliases;
386+
387+
$this->resolveAliases($this->aliases);
388+
389+
return;
390+
}
391+
392+
// Performance optimization. If there are no collisions, then we don't need to recompute loops
393+
$intersecting = $this->aliases && \array_intersect_key($this->aliases, $aliases);
394+
$this->aliases = $this->aliases ? \array_merge($this->aliases, $aliases) : $aliases;
395+
396+
if ($intersecting) {
397+
$this->resolveAliases($this->aliases);
398+
399+
return;
400+
}
401+
402+
$this->resolveAliases($aliases);
403+
$this->resolveNewAliasesWithPreviouslyResolvedAliases($aliases);
404+
}
405+
377406
/**
378407
* Add an alias.
379408
*
@@ -568,7 +597,11 @@ private function resolveInitializers(array $initializers)
568597
}
569598

570599
/**
571-
* Resolve all aliases to their canonical service names.
600+
* Resolve aliases to their canonical service names.
601+
*
602+
* @param string[] $aliases
603+
*
604+
* @returns void
572605
*/
573606
private function resolveAliases(array $aliases)
574607
{
@@ -589,6 +622,23 @@ private function resolveAliases(array $aliases)
589622
}
590623
}
591624

625+
/**
626+
* Rewrites the map of aliases by resolving the given $aliases with the existing resolved ones.
627+
* This is mostly done for performance reasons.
628+
*
629+
* @param string[] $aliases
630+
*
631+
* @return void
632+
*/
633+
private function resolveNewAliasesWithPreviouslyResolvedAliases(array $aliases)
634+
{
635+
foreach ($this->resolvedAliases as $name => $target) {
636+
if (isset($aliases[$target])) {
637+
$this->resolvedAliases[$name] = $this->resolvedAliases[$target];
638+
}
639+
}
640+
}
641+
592642
/**
593643
* Get a factory for the given service name
594644
*
@@ -750,6 +800,10 @@ private function createLazyServiceDelegatorFactory()
750800

751801
if (! isset($this->lazyServices['write_proxy_files']) || ! $this->lazyServices['write_proxy_files']) {
752802
$factoryConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
803+
} else {
804+
$factoryConfig->setGeneratorStrategy(new FileWriterGeneratorStrategy(
805+
new FileLocator($factoryConfig->getProxiesTargetDir())
806+
));
753807
}
754808

755809
spl_autoload_register($factoryConfig->getProxyAutoloader());

test/ServiceManagerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,29 @@ public function testAliasToAnExplicitServiceShouldWork()
236236

237237
$this->assertSame($service, $alias);
238238
}
239+
240+
/**
241+
* @depends testAliasToAnExplicitServiceShouldWork
242+
*/
243+
public function testSetAliasShouldWorkWithRecursiveAlias()
244+
{
245+
$config = [
246+
'aliases' => [
247+
'Alias' => 'TailInvokable',
248+
],
249+
'services' => [
250+
InvokableObject::class => new InvokableObject(),
251+
],
252+
];
253+
$serviceManager = new ServiceManager($config);
254+
$serviceManager->setAlias('HeadAlias', 'Alias');
255+
$serviceManager->setAlias('TailInvokable', InvokableObject::class);
256+
257+
$service = $serviceManager->get(InvokableObject::class);
258+
$alias = $serviceManager->get('Alias');
259+
$headAlias = $serviceManager->get('HeadAlias');
260+
261+
$this->assertSame($service, $alias);
262+
$this->assertSame($service, $headAlias);
263+
}
239264
}

0 commit comments

Comments
 (0)