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

Commit 4b3dcf5

Browse files
committed
Modular modules now use an "add capable" list ...
... for creating composite containers.
1 parent c0bd6c6 commit 4b3dcf5

6 files changed

Lines changed: 110 additions & 37 deletions

File tree

‎.idea/modular.iml‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/php.xml‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"dhii/normalization-helper-base": "^0.1-alpha2",
2929
"dhii/data-container-interface": "0.2.x-dev",
3030
"dhii/container-helper-base": "^0.1-alpha5",
31-
"dhii/i18n-helper-base": "^0.1"
31+
"dhii/i18n-helper-base": "^0.1",
32+
"dhii/collections-interface": "^0.2-alpha4"
3233
},
3334
"require-dev": {
3435
"phpunit/phpunit": "^4.8",

‎composer.lock‎

Lines changed: 55 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Module/ModularModuleTrait.php‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RebelCode\Modular\Module;
44

55
use ArrayAccess;
6+
use Dhii\Collection\AddCapableInterface;
67
use Dhii\Modular\Module\ModuleInterface;
78
use Psr\Container\ContainerInterface;
89
use stdClass;
@@ -39,30 +40,36 @@ trait ModularModuleTrait
3940
*/
4041
protected function _setup()
4142
{
43+
$this->modules = [];
44+
45+
// The containers
46+
$containers = $this->_createAddCapableList();
47+
// The initial container that will be used to initialize the modules
4248
$moduleInitContainer = $this->_getModuleInitContainer();
43-
$modules = $this->_getModules($moduleInitContainer);
4449

45-
// Setup all modules and collect their containers
46-
$this->modules = [];
47-
$containers = [];
48-
foreach ($modules as $_module) {
49-
$_container = $_module->setup();
50+
if ($moduleInitContainer !== null) {
51+
$containers->add($moduleInitContainer);
52+
}
5053

51-
if ($_container !== null) {
52-
$containers[] = $_container;
54+
// Get the modules to set up
55+
$modules = $this->_getModules($moduleInitContainer);
56+
$moduleContainers = [];
57+
foreach ($modules as $_module) {
58+
// Set up the module and collect its container
59+
if ($_container = $_module->setup()) {
60+
$moduleContainers[] = $_container;
5361
}
5462

55-
$_moduleServiceKey = $this->_getModuleServiceKey($_module);
56-
$this->modules[$_moduleServiceKey] = $_module;
63+
// Save module instance
64+
$this->modules[$this->_getModuleServiceKey($_module)] = $_module;
5765
}
5866

59-
// Prepend a container with all module instances
60-
$modulesContainer = $this->_createContainer($this->modules);
61-
array_unshift($containers, $modulesContainer);
67+
// Create a container that has the module instances as services, and add it to the list
68+
$containers->add($this->_createContainer($this->modules));
6269

63-
// Prepend the container that was used to initialize modules
64-
if ($moduleInitContainer !== null) {
65-
array_unshift($containers, $moduleInitContainer);
70+
// Add the containers retrieved from the modules, to the list
71+
foreach ($moduleContainers as $_moduleContainer) {
72+
$containers->add($_moduleContainer);
6673
}
6774

6875
// Construct the master container and return
@@ -136,4 +143,13 @@ abstract protected function _createContainer($definitions = [], ContainerInterfa
136143
* @return ContainerInterface The created composite container instance.
137144
*/
138145
abstract protected function _createCompositeContainer($containers);
146+
147+
/**
148+
* Creates a new, modifiable list.
149+
*
150+
* @since [*next-version*]
151+
*
152+
* @return AddCapableInterface|Traversable The traversable, modifiable list.
153+
*/
154+
abstract protected function _createAddCapableList();
139155
}

‎test/functional/Module/ModularModuleTraitTest.php‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function createInstance($methods = [])
4242
'_getModuleInitContainer',
4343
'_createContainer',
4444
'_createCompositeContainer',
45+
'_createAddCapableList'
4546
]
4647
);
4748

@@ -203,6 +204,14 @@ public function testSetup()
203204
$key3 => $this->createContainer(),
204205
];
205206

207+
$containerList = $this->mockClassAndInterfaces('stdClass', [
208+
'Iterator',
209+
'Dhii\Collection\AddCapableInterface'
210+
]);
211+
$subject->expects($this->once())
212+
->method('_createAddCapableList')
213+
->willReturn($containerList);
214+
206215
$moduleInitContainer = $this->createContainer();
207216
$subject->expects($this->once())
208217
->method('_getModuleInitContainer')
@@ -231,16 +240,9 @@ public function testSetup()
231240
->willReturn($modulesContainer);
232241

233242
$compositeContainer = $this->createContainer();
234-
$childrenContainers = [
235-
$moduleInitContainer,
236-
$modulesContainer,
237-
$containers[$key1],
238-
$containers[$key2],
239-
$containers[$key3],
240-
];
241243
$subject->expects($this->once())
242244
->method('_createCompositeContainer')
243-
->with($childrenContainers)
245+
->with($containerList)
244246
->willReturn($compositeContainer);
245247

246248
$this->assertSame(
@@ -272,6 +274,14 @@ public function testSetupSomeMissingContainers()
272274
$key3 => $this->createContainer(),
273275
];
274276

277+
$containerList = $this->mockClassAndInterfaces('stdClass', [
278+
'Iterator',
279+
'Dhii\Collection\AddCapableInterface'
280+
]);
281+
$subject->expects($this->once())
282+
->method('_createAddCapableList')
283+
->willReturn($containerList);
284+
275285
$subject->expects($this->once())
276286
->method('_getModuleInitContainer')
277287
->willReturn(null);
@@ -299,14 +309,9 @@ public function testSetupSomeMissingContainers()
299309
->willReturn($modulesContainer);
300310

301311
$compositeContainer = $this->createContainer();
302-
$childrenContainers = [
303-
$modulesContainer,
304-
$containers[$key1],
305-
$containers[$key3],
306-
];
307312
$subject->expects($this->once())
308313
->method('_createCompositeContainer')
309-
->with($childrenContainers)
314+
->with($containerList)
310315
->willReturn($compositeContainer);
311316

312317
$this->assertSame(

0 commit comments

Comments
 (0)