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

Commit 0d1b3aa

Browse files
committed
Incorporate additional feedback
- Implement container-interop exceptions for exceptions thrown during `get()` operations. - Ensure a string is a valid class before attempting to instantiate it (abstract factories, initializers, factories). - Do not assign an instantiated factory until we know it's callable. - Document `@throws` annotations.
1 parent c4f1e6d commit 0d1b3aa

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/Exception/ServiceNotCreatedException.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
namespace Zend\ServiceManager\Exception;
1111

12+
use Interop\Container\Exception\ContainerException;
1213
use RuntimeException as SplRuntimeException;
1314

1415
/**
1516
* This exception is thrown when the service locator do not manage to create the service (factory that has an error...)
1617
*/
17-
class ServiceNotCreatedException extends SplRuntimeException implements ExceptionInterface
18+
class ServiceNotCreatedException extends SplRuntimeException implements
19+
ContainerException,
20+
ExceptionInterface
1821
{
1922
}

src/Exception/ServiceNotFoundException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
/**
1616
* This exception is thrown when the service locator do not manage to find a valid factory to create a service
1717
*/
18-
class ServiceNotFoundException extends SplInvalidArgumentException implements ExceptionInterface, NotFoundException
18+
class ServiceNotFoundException extends SplInvalidArgumentException implements
19+
ExceptionInterface,
20+
NotFoundException
1921
{
2022
}

src/ServiceManager.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,17 @@ protected function configure(array $config)
258258
// instantiate them to avoid checks during service construction.
259259
if (isset($config['abstract_factories'])) {
260260
foreach ($config['abstract_factories'] as $abstractFactory) {
261-
if (is_string($abstractFactory)) {
261+
if (is_string($abstractFactory) && class_exists($abstractFactory)) {
262262
$abstractFactory = new $abstractFactory();
263263
}
264264

265-
if (is_callable($abstractFactory)) {
265+
if ($abstractFactory instanceof AbstractFactoryInterface) {
266266
$this->abstractFactories[] = $abstractFactory;
267267
continue;
268268
}
269269

270270
throw new InvalidArgumentException(sprintf(
271-
'An invalid abstract factory was registered. A callable or an instance of "%s" was expected, '
271+
'An invalid abstract factory was registered. An instance of "%s" was expected, '
272272
. 'but "%s" was received',
273273
AbstractFactoryInterface::class,
274274
is_object($abstractFactory) ? get_class($abstractFactory) : gettype($abstractFactory)
@@ -278,7 +278,7 @@ protected function configure(array $config)
278278

279279
if (isset($config['initializers'])) {
280280
foreach ($config['initializers'] as $initializer) {
281-
if (is_string($initializer)) {
281+
if (is_string($initializer) && class_exists($initializer)) {
282282
$initializer = new $initializer();
283283
}
284284

@@ -326,11 +326,16 @@ private function getFactory($name)
326326
{
327327
$factory = isset($this->factories[$name]) ? $this->factories[$name] : null;
328328

329-
if (is_string($factory)) {
330-
$this->factories[$name] = $factory = new $factory();
329+
$lazyLoaded = false;
330+
if (is_string($factory) && class_exists($factory)) {
331+
$factory = new $factory();
332+
$lazyLoaded = true;
331333
}
332334

333335
if (is_callable($factory)) {
336+
if ($lazyLoaded) {
337+
$this->factories[$name] = $factory;
338+
}
334339
return $factory;
335340
}
336341

@@ -406,6 +411,9 @@ private function createDelegatorFromName($name, array $options = null)
406411
* @param string $resolvedName
407412
* @param null|array $options
408413
* @return mixed
414+
* @throws ServiceNotFoundException if unable to resolve the service.
415+
* @throws ServiceNotCreatedException if an exception is raised when
416+
* creating a service.
409417
*/
410418
private function doCreate($resolvedName, array $options = null)
411419
{

0 commit comments

Comments
 (0)