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

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Jun 1, 2016
2 parents f89f7d9 + b4ad11e commit 90b8833
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 15 deletions.
1 change: 0 additions & 1 deletion .coveralls.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
coverage_clover: clover.xml
json_path: coveralls-upload.json
src_dir: src
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ matrix:
- php: 7
- php: hhvm
allow_failures:
- php: 7
- php: hhvm

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

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs
- travis_retry composer install --no-interaction

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

after_script:
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls ; fi
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls -v ; fi
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@

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

## 3.1.0 - 2016-06-01

### Added

- [#103](https://github.com/zendframework/zend-servicemanager/pull/103) Allowing
installation of `ocramius/proxy-manager` `^2.0` together with
`zendframework/zend-servicemanager`.
- [#103](https://github.com/zendframework/zend-servicemanager/pull/103) Disallowing
test failures when running tests against PHP `7.0.*`.
- [#113](https://github.com/zendframework/zend-servicemanager/pull/113) Improved performance
when dealing with registering aliases and factories via `ServiceManager#setFactory()` and
`ServiceManager#setAlias()`
- [#120](https://github.com/zendframework/zend-servicemanager/pull/120) The
`zendframework/zend-servicemanager` component now provides a
`container-interop/container-interop-implementation` implementation

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- [#97](https://github.com/zendframework/zend-servicemanager/pull/97) Typo corrections
in the delegator factories documentation.
- [#98](https://github.com/zendframework/zend-servicemanager/pull/98) Using coveralls ^1.0
for tracking test code coverage changes.

## 3.0.4 - TBD

### Added
Expand Down
77 changes: 77 additions & 0 deletions benchmarks/SetNewServicesBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace ZendBench\ServiceManager;

use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use Zend\ServiceManager\ServiceManager;

/**
* @Revs(1000)
* @Iterations(10)
* @Warmup(2)
*/
class SetNewServicesBench
{
const NUM_SERVICES = 100;

/**
* @var ServiceManager
*/
private $sm;

public function __construct()
{
$config = [
'factories' => [
'factory1' => BenchAsset\FactoryFoo::class,
],
'invokables' => [
'invokable1' => BenchAsset\Foo::class,
],
'services' => [
'service1' => new \stdClass(),
],
'aliases' => [
'factoryAlias1' => 'factory1',
'recursiveFactoryAlias1' => 'factoryAlias1',
'recursiveFactoryAlias2' => 'recursiveFactoryAlias1',
],
'abstract_factories' => [
BenchAsset\AbstractFactoryFoo::class
],
];

for ($i = 0; $i <= self::NUM_SERVICES; $i++) {
$config['factories']["factory_$i"] = BenchAsset\FactoryFoo::class;
$config['aliases']["alias_$i"] = "service_$i";
}

$this->sm = new ServiceManager($config);
}

public function benchSetFactory()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->setFactory('factory2', BenchAsset\FactoryFoo::class);
}

public function benchSetAlias()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->setAlias('factoryAlias2', 'factory1');
}

public function benchSetAliasOverrided()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->setAlias('recursiveFactoryAlias1', 'factory1');
}
}
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"container-interop/container-interop": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.6",
"ocramius/proxy-manager": "~1.0",
"squizlabs/php_codesniffer": "^2.0@dev",
"phpunit/phpunit": "^4.6 || ^5.2.10",
"ocramius/proxy-manager": "^1.0 || ^2.0",
"squizlabs/php_codesniffer": "^2.5.1",
"phpbench/phpbench": "^0.10.0"
},
"suggest": {
Expand All @@ -40,5 +40,8 @@
"ZendTest\\ServiceManager\\": "test/",
"ZendBench\\ServiceManager\\": "benchmarks/"
}
},
"provide": {
"container-interop/container-interop-implementation": "^1.1"
}
}
3 changes: 1 addition & 2 deletions doc/book/delegators.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ The parameters passed to the delegator factory are the following:
- `$container` is the service locator that is used while creating the delegator
for the requested service.
- `$name` is the name of the service being requested.
- `$callback` is a
- [callable](http://www.php.net/manual/en/language.types.callable.php) that is
- `$callback` is a [callable](http://www.php.net/manual/en/language.types.callable.php) that is
responsible for instantiating the delegated service (the real service instance).
- `$options` is an array of options to use when creating the instance; these are
typically used only during `build()` operations.
Expand Down
64 changes: 59 additions & 5 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use Interop\Container\Exception\ContainerException;
use ProxyManager\Configuration as ProxyConfiguration;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\FileLocator\FileLocator;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
use Zend\ServiceManager\Exception\ContainerModificationsNotAllowedException;
use Zend\ServiceManager\Exception\CyclicAliasException;
use Zend\ServiceManager\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -341,10 +343,8 @@ public function configure(array $config)
}

if (isset($config['aliases'])) {
$this->aliases = $config['aliases'] + $this->aliases;
}

if (! empty($this->aliases)) {
$this->configureAliases($config['aliases']);
} elseif (! $this->configured && ! empty($this->aliases)) {
$this->resolveAliases($this->aliases);
}

Expand Down Expand Up @@ -374,6 +374,35 @@ public function configure(array $config)
return $this;
}

/**
* @param string[] $aliases
*
* @return void
*/
private function configureAliases(array $aliases)
{
if (! $this->configured) {
$this->aliases = $aliases + $this->aliases;

$this->resolveAliases($this->aliases);

return;
}

// Performance optimization. If there are no collisions, then we don't need to recompute loops
$intersecting = $this->aliases && \array_intersect_key($this->aliases, $aliases);
$this->aliases = $this->aliases ? \array_merge($this->aliases, $aliases) : $aliases;

if ($intersecting) {
$this->resolveAliases($this->aliases);

return;
}

$this->resolveAliases($aliases);
$this->resolveNewAliasesWithPreviouslyResolvedAliases($aliases);
}

/**
* Add an alias.
*
Expand Down Expand Up @@ -568,7 +597,11 @@ private function resolveInitializers(array $initializers)
}

/**
* Resolve all aliases to their canonical service names.
* Resolve aliases to their canonical service names.
*
* @param string[] $aliases
*
* @returns void
*/
private function resolveAliases(array $aliases)
{
Expand All @@ -589,6 +622,23 @@ private function resolveAliases(array $aliases)
}
}

/**
* Rewrites the map of aliases by resolving the given $aliases with the existing resolved ones.
* This is mostly done for performance reasons.
*
* @param string[] $aliases
*
* @return void
*/
private function resolveNewAliasesWithPreviouslyResolvedAliases(array $aliases)
{
foreach ($this->resolvedAliases as $name => $target) {
if (isset($aliases[$target])) {
$this->resolvedAliases[$name] = $this->resolvedAliases[$target];
}
}
}

/**
* Get a factory for the given service name
*
Expand Down Expand Up @@ -750,6 +800,10 @@ private function createLazyServiceDelegatorFactory()

if (! isset($this->lazyServices['write_proxy_files']) || ! $this->lazyServices['write_proxy_files']) {
$factoryConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
} else {
$factoryConfig->setGeneratorStrategy(new FileWriterGeneratorStrategy(
new FileLocator($factoryConfig->getProxiesTargetDir())
));
}

spl_autoload_register($factoryConfig->getProxyAutoloader());
Expand Down
25 changes: 25 additions & 0 deletions test/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,29 @@ public function testAliasToAnExplicitServiceShouldWork()

$this->assertSame($service, $alias);
}

/**
* @depends testAliasToAnExplicitServiceShouldWork
*/
public function testSetAliasShouldWorkWithRecursiveAlias()
{
$config = [
'aliases' => [
'Alias' => 'TailInvokable',
],
'services' => [
InvokableObject::class => new InvokableObject(),
],
];
$serviceManager = new ServiceManager($config);
$serviceManager->setAlias('HeadAlias', 'Alias');
$serviceManager->setAlias('TailInvokable', InvokableObject::class);

$service = $serviceManager->get(InvokableObject::class);
$alias = $serviceManager->get('Alias');
$headAlias = $serviceManager->get('HeadAlias');

$this->assertSame($service, $alias);
$this->assertSame($service, $headAlias);
}
}

0 comments on commit 90b8833

Please sign in to comment.