Skip to content

Commit 48081ea

Browse files
committed
Removing build() method
1 parent 3f39828 commit 48081ea

File tree

2 files changed

+58
-69
lines changed

2 files changed

+58
-69
lines changed

lib/Container.php

Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -32,70 +32,7 @@ public function __construct(array $extensionClasses = [], array $userConfig = []
3232
{
3333
$this->extensionClasses = $extensionClasses;
3434
$this->userConfig = $userConfig;
35-
}
36-
37-
/**
38-
* Configure the container. This method will call the `configure()` method
39-
* on each extension. Extensions must use this opportunity to register their
40-
* services and define any default config.
41-
*
42-
* This method must be called before `build()`.
43-
*/
44-
public function init()
45-
{
46-
$extensions = [];
47-
48-
if (empty($this->extensionClasses) && empty($this->userConfig)) {
49-
return;
50-
}
51-
52-
foreach ($this->extensionClasses as $extensionClass) {
53-
if (!class_exists($extensionClass)) {
54-
throw new \InvalidArgumentException(sprintf(
55-
'Extension class "%s" does not exist',
56-
$extensionClass
57-
));
58-
}
59-
60-
$extension = new $extensionClass();
61-
62-
if (!$extension instanceof ExtensionInterface) {
63-
throw new \InvalidArgumentException(sprintf(
64-
// add any manually specified extensions
65-
'Extension "%s" must implement the PhpBench\\Extension interface',
66-
get_class($extension)
67-
));
68-
}
69-
70-
$extensions[] = $extension;
71-
72-
$this->config = array_merge(
73-
$this->config,
74-
$extension->getDefaultConfig()
75-
);
76-
}
77-
78-
$diff = array_diff(array_keys($this->userConfig), array_keys($this->config));
79-
80-
if ($diff) {
81-
throw new \InvalidArgumentException(sprintf(
82-
'Unknown configuration keys: "%s". Permitted keys: "%s"',
83-
implode('", "', $diff), implode('", "', array_keys($this->config))
84-
));
85-
}
86-
87-
$this->config = array_merge(
88-
$this->config,
89-
$this->userConfig
90-
);
91-
92-
foreach ($extensions as $extension) {
93-
$extension->load($this);
94-
}
95-
96-
foreach ($extensions as $extension) {
97-
$extension->build($this);
98-
}
35+
$this->init();
9936
}
10037

10138
/**
@@ -240,4 +177,61 @@ public function hasParameter($name)
240177
{
241178
return array_key_exists($name, $this->config);
242179
}
180+
181+
private function init()
182+
{
183+
$extensions = [];
184+
185+
if (empty($this->extensionClasses) && empty($this->userConfig)) {
186+
return;
187+
}
188+
189+
foreach ($this->extensionClasses as $extensionClass) {
190+
if (!class_exists($extensionClass)) {
191+
throw new \InvalidArgumentException(sprintf(
192+
'Extension class "%s" does not exist',
193+
$extensionClass
194+
));
195+
}
196+
197+
$extension = new $extensionClass();
198+
199+
if (!$extension instanceof ExtensionInterface) {
200+
throw new \InvalidArgumentException(sprintf(
201+
// add any manually specified extensions
202+
'Extension "%s" must implement the PhpBench\\Extension interface',
203+
get_class($extension)
204+
));
205+
}
206+
207+
$extensions[] = $extension;
208+
209+
$this->config = array_merge(
210+
$this->config,
211+
$extension->getDefaultConfig()
212+
);
213+
}
214+
215+
$diff = array_diff(array_keys($this->userConfig), array_keys($this->config));
216+
217+
if ($diff) {
218+
throw new \InvalidArgumentException(sprintf(
219+
'Unknown configuration keys: "%s". Permitted keys: "%s"',
220+
implode('", "', $diff), implode('", "', array_keys($this->config))
221+
));
222+
}
223+
224+
$this->config = array_merge(
225+
$this->config,
226+
$this->userConfig
227+
);
228+
229+
foreach ($extensions as $extension) {
230+
$extension->load($this);
231+
}
232+
233+
foreach ($extensions as $extension) {
234+
$extension->build($this);
235+
}
236+
}
243237
}

tests/Unit/ContainerTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public function testRegisterExtension()
103103
__NAMESPACE__ . '\\TestExtension',
104104
]);
105105

106-
$container->init();
107106
$object = $container->get('foobar');
108107
$this->assertInstanceOf('stdClass', $object);
109108
$this->assertEquals('bar', $object->foobar);
@@ -124,7 +123,6 @@ public function testRegisterExtensionWithUserConfig()
124123
]
125124
);
126125

127-
$container->init();
128126
$object = $container->get('foobar');
129127
$this->assertInstanceOf('stdClass', $object);
130128
$this->assertEquals('bazz', $object->foobar);
@@ -165,7 +163,6 @@ public function testMergeParameterNonArray()
165163
public function testRegisterNotExistingExtension()
166164
{
167165
$container = new Container(['NotExistingExtension']);
168-
$container->init();
169166
}
170167

171168
/**
@@ -178,7 +175,6 @@ public function testRegisterNotExistingExtension()
178175
public function testRegisterNotImplementingExtension()
179176
{
180177
$container = new Container(['stdClass']);
181-
$container->init();
182178
}
183179

184180
/**
@@ -192,7 +188,6 @@ public function testUnknownUserConfig()
192188
$container = new Container([], [
193189
'not' => 'existing',
194190
]);
195-
$container->init();
196191
}
197192

198193
/**

0 commit comments

Comments
 (0)