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

Commit 20cb47d

Browse files
committed
Merge branch 'feature/22' into develop
Close #22
2 parents ad62f74 + e282b3d commit 20cb47d

File tree

2 files changed

+82
-50
lines changed

2 files changed

+82
-50
lines changed

src/ServiceManager.php

Lines changed: 81 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -275,74 +275,106 @@ protected function configure(array $config)
275275
// For abstract factories and initializers, we always directly
276276
// instantiate them to avoid checks during service construction.
277277
if (isset($config['abstract_factories'])) {
278-
foreach ($config['abstract_factories'] as $abstractFactory) {
279-
if (is_string($abstractFactory) && class_exists($abstractFactory)) {
280-
$abstractFactory = new $abstractFactory();
281-
}
278+
$this->resolveAbstractFactories($config['abstract_factories']);
279+
}
282280

283-
if ($abstractFactory instanceof AbstractFactoryInterface) {
284-
$this->abstractFactories[] = $abstractFactory;
285-
continue;
286-
}
281+
if (isset($config['initializers'])) {
282+
$this->resolveInitializers($config['initializers']);
283+
}
287284

288-
// Error condition; let's find out why.
285+
$this->resolveAliases();
286+
}
289287

290-
// If we still have a string, we have a class name that does not resolve
291-
if (is_string($abstractFactory)) {
292-
throw new InvalidArgumentException(sprintf(
293-
'An invalid abstract factory was registered; resolved to class "%s" '
294-
. 'which does not exist; please provide a valid class name resolving '
295-
. 'to an implementation of %s',
288+
/**
289+
* Instantiate abstract factories for to avoid checks during service construction.
290+
*
291+
* @param string[]|AbstractFactoryInterface[] $abstractFactories
292+
*
293+
* @return void
294+
*/
295+
private function resolveAbstractFactories(array $abstractFactories)
296+
{
297+
foreach ($abstractFactories as $abstractFactory) {
298+
if (is_string($abstractFactory) && class_exists($abstractFactory)) {
299+
$abstractFactory = new $abstractFactory();
300+
}
301+
302+
if ($abstractFactory instanceof AbstractFactoryInterface) {
303+
$this->abstractFactories[] = $abstractFactory;
304+
continue;
305+
}
306+
307+
// Error condition; let's find out why.
308+
309+
// If we still have a string, we have a class name that does not resolve
310+
if (is_string($abstractFactory)) {
311+
throw new InvalidArgumentException(
312+
sprintf(
313+
'An invalid abstract factory was registered; resolved to class "%s" ' .
314+
'which does not exist; please provide a valid class name resolving ' .
315+
'to an implementation of %s',
296316
$abstractFactory,
297317
AbstractFactoryInterface::class
298-
));
299-
}
318+
)
319+
);
320+
}
300321

301-
// Otherwise, we have an invalid type.
302-
throw new InvalidArgumentException(sprintf(
303-
'An invalid abstract factory was registered. Expected an instance of "%s", '
304-
. 'but "%s" was received',
322+
// Otherwise, we have an invalid type.
323+
throw new InvalidArgumentException(
324+
sprintf(
325+
'An invalid abstract factory was registered. Expected an instance of "%s", ' .
326+
'but "%s" was received',
305327
AbstractFactoryInterface::class,
306328
(is_object($abstractFactory) ? get_class($abstractFactory) : gettype($abstractFactory))
307-
));
308-
}
329+
)
330+
);
309331
}
332+
}
310333

311-
if (isset($config['initializers'])) {
312-
foreach ($config['initializers'] as $initializer) {
313-
if (is_string($initializer) && class_exists($initializer)) {
314-
$initializer = new $initializer();
315-
}
334+
/**
335+
* Instantiate initializers for to avoid checks during service construction.
336+
*
337+
* @param string[]|callable[]|InitializerInterface[] $initializers
338+
*
339+
* @return void
340+
*/
341+
private function resolveInitializers(array $initializers)
342+
{
343+
foreach ($initializers as $initializer) {
344+
if (is_string($initializer) && class_exists($initializer)) {
345+
$initializer = new $initializer();
346+
}
316347

317-
if (is_callable($initializer)) {
318-
$this->initializers[] = $initializer;
319-
continue;
320-
}
348+
if (is_callable($initializer)) {
349+
$this->initializers[] = $initializer;
350+
continue;
351+
}
321352

322-
// Error condition; let's find out why.
353+
// Error condition; let's find out why.
323354

324-
if (is_string($initializer)) {
325-
throw new InvalidArgumentException(sprintf(
326-
'An invalid initializer was registered; resolved to class or function "%s" '
327-
. 'which does not exist; please provide a valid function name or class '
328-
. 'name resolving to an implementation of %s',
355+
if (is_string($initializer)) {
356+
throw new InvalidArgumentException(
357+
sprintf(
358+
'An invalid initializer was registered; resolved to class or function "%s" ' .
359+
'which does not exist; please provide a valid function name or class ' .
360+
'name resolving to an implementation of %s',
329361
$initializer,
330362
InitializerInterface::class
331-
));
332-
}
363+
)
364+
);
365+
}
333366

334-
// Otherwise, we have an invalid type.
335-
throw new InvalidArgumentException(sprintf(
336-
'An invalid initializer was registered. Expected a callable, or an instance of '
337-
. '(or string class name resolving to) "%s", '
338-
. 'but "%s" was received',
367+
// Otherwise, we have an invalid type.
368+
throw new InvalidArgumentException(
369+
sprintf(
370+
'An invalid initializer was registered. Expected a callable, or an instance of ' .
371+
'(or string class name resolving to) "%s", ' .
372+
'but "%s" was received',
339373
InitializerInterface::class,
340374
(is_object($initializer) ? get_class($initializer) : gettype($initializer))
341-
));
342-
}
375+
)
376+
);
343377
}
344-
345-
$this->resolveAliases();
346378
}
347379

348380
/**

test/LazyServiceIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use ZendTest\ServiceManager\TestAsset\InvokableObject;
2525

2626
/**
27-
* @covers \Zend\ServiceManager\ServiceManager
27+
* @covers \Zend\ServiceManager\Proxy\LazyServiceFactory
2828
*/
2929
class LazyServiceIntegrationTest extends TestCase
3030
{

0 commit comments

Comments
 (0)