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

Commit c43e871

Browse files
committed
More specific exceptions
Per feedback, this patch makes exception throwing more consistent, and more specific. Each of abstract factories, initializers, and delegator factories now throw messages in the same manner, and differentiate between inability to resolve a class name and non-callable/invalid type factories.
1 parent b450853 commit c43e871

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

src/ServiceManager.php

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,25 @@ protected function configure(array $config)
274274
continue;
275275
}
276276

277+
// Error condition; let's find out why.
278+
279+
// If we still have a string, we have a class name that does not resolve
280+
if (is_string($abstractFactory)) {
281+
throw new InvalidArgumentException(sprintf(
282+
'An invalid abstract factory was registered; resolved to class "%s" '
283+
. 'which does not exist; please provide a valid class name resolving '
284+
. 'to an implementation of %s',
285+
$abstractFactory,
286+
AbstractFactoryInterface::class
287+
));
288+
}
289+
290+
// Otherwise, we have an invalid type.
277291
throw new InvalidArgumentException(sprintf(
278-
'An invalid abstract factory was registered. An instance of "%s" was expected, '
292+
'An invalid abstract factory was registered. Expected an instance of "%s", '
279293
. 'but "%s" was received',
280294
AbstractFactoryInterface::class,
281-
is_object($abstractFactory) ? get_class($abstractFactory) : gettype($abstractFactory)
295+
(is_object($abstractFactory) ? get_class($abstractFactory) : gettype($abstractFactory))
282296
));
283297
}
284298
}
@@ -294,11 +308,25 @@ protected function configure(array $config)
294308
continue;
295309
}
296310

311+
// Error condition; let's find out why.
312+
313+
if (is_string($initializer)) {
314+
throw new InvalidArgumentException(sprintf(
315+
'An invalid initializer was registered; resolved to class or function "%s" '
316+
. 'which does not exist; please provide a valid function name or class '
317+
. 'name resolving to an implementation of %s',
318+
$initializer,
319+
InitializerInterface::class
320+
));
321+
}
322+
323+
// Otherwise, we have an invalid type.
297324
throw new InvalidArgumentException(sprintf(
298-
'An invalid initializer was registered. A callable or an instance of "%s" was expected, but
299-
"%s" was received',
325+
'An invalid initializer was registered. Expected a callable, or an instance of '
326+
. '(or string class name resolving to) "%s", '
327+
. 'but "%s" was received',
300328
InitializerInterface::class,
301-
is_object($initializer) ? get_class($initializer) : gettype($initializer)
329+
(is_object($initializer) ? get_class($initializer) : gettype($initializer))
302330
));
303331
}
304332
}
@@ -391,20 +419,21 @@ private function createDelegatorFromName($name, array $options = null)
391419
$delegatorFactory = $this->createLazyServiceDelegatorFactory();
392420
}
393421

394-
if (is_string($delegatorFactory) && ! class_exists($delegatorFactory)) {
395-
throw new ServiceNotCreatedException(sprintf(
396-
'An invalid delegator was provided. A callable or an instance of "%s" was expected, '
397-
. 'but "%s" was received',
398-
DelegatorFactoryInterface::class,
399-
$delegatorFactory
400-
));
401-
}
402-
403-
if (is_string($delegatorFactory)) {
404-
$delegatorFactory = $this->delegators[$name][$index] = new $delegatorFactory();
422+
if (is_string($delegatorFactory) && class_exists($delegatorFactory)) {
423+
$delegatorFactory = new $delegatorFactory();
405424
}
406425

407426
if (! is_callable($delegatorFactory)) {
427+
if (is_string($delegatorFactory)) {
428+
throw new ServiceNotCreatedException(sprintf(
429+
'An invalid delegator factory was registered; resolved to class or function "%s" '
430+
. 'which does not exist; please provide a valid function name or class name resolving '
431+
. 'to an implementation of %s',
432+
$delegatorFactory,
433+
DelegatorFactoryInterface::class
434+
));
435+
}
436+
408437
throw new ServiceNotCreatedException(sprintf(
409438
'A non-callable delegator, "%s", was provided; expected a callable or '
410439
. 'instance of "%s"',
@@ -413,6 +442,8 @@ private function createDelegatorFromName($name, array $options = null)
413442
));
414443
}
415444

445+
$this->delegators[$name][$index] = $delegatorFactory;
446+
416447
$creationCallback = function () use ($delegatorFactory, $name, $creationCallback, $options) {
417448
return $delegatorFactory($this->creationContext, $name, $creationCallback, $options);
418449
};

0 commit comments

Comments
 (0)