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

Commit

Permalink
Merge branch 'hotfix/allow-override'
Browse files Browse the repository at this point in the history
Close #31
Fixes #30
  • Loading branch information
weierophinney committed Feb 28, 2016
2 parents cfcdf32 + 040bc97 commit a2c3af1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

## 2.7.1 - TBD
## 2.7.1 - 2016-02-27

### Added

Expand All @@ -18,7 +18,10 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#31](https://github.com/zendframework/zend-modulemanager/pull/31) updates the
`ServiceListener:onLoadModulesPost()` workflow to override existing services
on a given service/plugin manager instance when configuring it. Since the
listener operates as part of bootstrapping, this is a requirement.

## 2.7.0 - 2016-02-25

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"zendframework/zend-console": "^2.6",
"zendframework/zend-di": "^2.6",
"zendframework/zend-loader": "^2.5",
"zendframework/zend-servicemanager": "^3.0.3",
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
},
Expand Down
8 changes: 8 additions & 0 deletions src/Listener/ServiceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,15 @@ public function onLoadModulesPost(ModuleEvent $e)
}

$serviceConfig = new ServiceConfig($smConfig);

// The service listener is meant to operate during bootstrap, and, as such,
// needs to be able to override existing configuration.
$allowOverride = $sm['service_manager']->getAllowOverride();
$sm['service_manager']->setAllowOverride(true);

$serviceConfig->configureServiceManager($sm['service_manager']);

$sm['service_manager']->setAllowOverride($allowOverride);
}
}

Expand Down
40 changes: 40 additions & 0 deletions test/Listener/ServiceListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,44 @@ public function testCanDetachListeners(array $dependencies)
$listeners = $this->getArrayOfListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES_POST, $events);
$this->assertCount(0, $listeners);
}

public function testListenerCanOverrideServicesInServiceManagers()
{
$services = new ServiceManager();
$services->setService('config', []);
$services->setFactory('foo', function ($services) {
return $services;
});
$listener = new ServiceListener($services);
$listener->addServiceManager(
$services,
'service_manager',
ServiceProviderInterface::class,
'getServiceConfig'
);

$module = new TestAsset\ServiceProviderModule([
'services' => [
'config' => [ 'foo' => 'bar'],
],
'factories' => [
'foo' => function ($services) {
return new stdClass();
},
],
]);

$event = new ModuleEvent();
$configListener = new ConfigListener();
$event->setConfigListener($configListener);

$event->setModule($module);
$listener->onLoadModule($event);
$listener->onLoadModulesPost($event);

$this->assertTrue($services->has('config'));
$this->assertTrue($services->has('foo'));
$this->assertEquals(['foo' => 'bar'], $services->get('config'), 'Config service was not overridden');
$this->assertInstanceOf(stdClass::class, $services->get('foo'), 'Foo service was not overridden');
}
}

0 comments on commit a2c3af1

Please sign in to comment.