Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 8570930

Browse files
committed
Fixed module containers being unable to get ...
... the composite container as a parent. Modules should be able to create containers that reference the composite container as a parent. This can be achieved by using a pre-configured factory. The factory, however, must be able to obtain the composite container as well. Classes that use `ModularModuleTrait` should now implement `_getInitialContainer()` in such a way so as to use the container argument given to set up the container factory service. When modules use this factory to create their containers, the composite container is assigned as a parent by the factory.
1 parent 4b3dcf5 commit 8570930

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

‎src/Module/ModularModuleTrait.php‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*
1919
* On {@see _run()}, every module's {@see run()} method is invoked with the given container.
2020
*
21+
* It is very important to use a composite container that can work with a {@see Traversable} list of containers that is
22+
* initially empty and populated later. This is the only sane remedy for the chicken-egg problem.
23+
*
2124
* @since [*next-version*]
2225
*/
2326
trait ModularModuleTrait
@@ -40,19 +43,21 @@ trait ModularModuleTrait
4043
*/
4144
protected function _setup()
4245
{
43-
$this->modules = [];
44-
45-
// The containers
46+
// The list of containers that will be added to the composite container
4647
$containers = $this->_createAddCapableList();
47-
// The initial container that will be used to initialize the modules
48-
$moduleInitContainer = $this->_getModuleInitContainer();
48+
// The composite container - the container for this modular module
49+
$composite = $this->_createCompositeContainer($containers);
4950

50-
if ($moduleInitContainer !== null) {
51-
$containers->add($moduleInitContainer);
51+
// The initial container that will be used to initialize the modules
52+
$initContainer = $this->_getInitialContainer($composite);
53+
if ($initContainer !== null) {
54+
$containers->add($initContainer);
5255
}
5356

57+
$this->modules = [];
58+
5459
// Get the modules to set up
55-
$modules = $this->_getModules($moduleInitContainer);
60+
$modules = $this->_getModules($initContainer);
5661
$moduleContainers = [];
5762
foreach ($modules as $_module) {
5863
// Set up the module and collect its container
@@ -73,7 +78,7 @@ protected function _setup()
7378
}
7479

7580
// Construct the master container and return
76-
return $this->_createCompositeContainer($containers);
81+
return $composite;
7782
}
7883

7984
/**
@@ -117,9 +122,11 @@ abstract protected function _getModules(ContainerInterface $container = null);
117122
*
118123
* @since [*next-version*]
119124
*
125+
* @param ContainerInterface|null $parent The parent container, if any.
126+
*
120127
* @return ContainerInterface|null The container instance, if any.
121128
*/
122-
abstract protected function _getModuleInitContainer();
129+
abstract protected function _getInitialContainer(ContainerInterface $parent = null);
123130

124131
/**
125132
* Creates a container instance with the given service definitions.

‎test/functional/Module/ModularModuleTraitTest.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function createInstance($methods = [])
3939
[
4040
'_getModules',
4141
'_getModuleServiceKey',
42-
'_getModuleInitContainer',
42+
'_getInitialContainer',
4343
'_createContainer',
4444
'_createCompositeContainer',
4545
'_createAddCapableList'
@@ -212,10 +212,10 @@ public function testSetup()
212212
->method('_createAddCapableList')
213213
->willReturn($containerList);
214214

215-
$moduleInitContainer = $this->createContainer();
215+
$initContainer = $this->createContainer();
216216
$subject->expects($this->once())
217-
->method('_getModuleInitContainer')
218-
->willReturn($moduleInitContainer);
217+
->method('_getInitialContainer')
218+
->willReturn($initContainer);
219219

220220
$subject->expects($this->exactly(count($modules)))
221221
->method('_getModuleServiceKey')
@@ -224,7 +224,7 @@ public function testSetup()
224224

225225
$subject->expects($this->once())
226226
->method('_getModules')
227-
->with($moduleInitContainer)
227+
->with($initContainer)
228228
->willReturn($modules);
229229

230230
foreach ($modules as $_key => $_module) {
@@ -283,7 +283,7 @@ public function testSetupSomeMissingContainers()
283283
->willReturn($containerList);
284284

285285
$subject->expects($this->once())
286-
->method('_getModuleInitContainer')
286+
->method('_getInitialContainer')
287287
->willReturn(null);
288288

289289
$subject->expects($this->exactly(count($modules)))

0 commit comments

Comments
 (0)