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

Little improvements #266

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/AbstractFactory/ConfigAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Zend\ServiceManager\AbstractFactory;

use ArrayObject;
use Psr\Container\ContainerInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Factory\AbstractFactoryInterface;

Expand All @@ -24,12 +25,18 @@ final class ConfigAbstractFactory implements AbstractFactoryInterface
*
* {@inheritdoc}
*/
public function canCreate(\Psr\Container\ContainerInterface $container, $requestedName)
public function canCreate(ContainerInterface $container, $requestedName)
{
if (! $container->has('config') || ! array_key_exists(self::class, $container->get('config'))) {
if (! $container->has('config')) {
return false;
}

$config = $container->get('config');

if (! isset($config[self::class])) {
return false;
}

$dependencies = $config[self::class];

return is_array($dependencies) && array_key_exists($requestedName, $dependencies);
Expand All @@ -38,7 +45,7 @@ public function canCreate(\Psr\Container\ContainerInterface $container, $request
/**
* {@inheritDoc}
*/
public function __invoke(\Psr\Container\ContainerInterface $container, $requestedName, array $options = null)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
if (! $container->has('config')) {
throw new ServiceNotCreatedException('Cannot find a config array in the container');
Expand All @@ -50,14 +57,14 @@ public function __invoke(\Psr\Container\ContainerInterface $container, $requeste
throw new ServiceNotCreatedException('Config must be an array or an instance of ArrayObject');
}

if (! array_key_exists(self::class, $config)) {
if (! isset($config[self::class])) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BC

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not. The value MUST be an array to be usable. As such, isset() is all that's required to test for usable configuration.

throw new ServiceNotCreatedException('Cannot find a `' . self::class . '` key in the config array');
}

$dependencies = $config[self::class];

if (! is_array($dependencies)
|| ! array_key_exists($requestedName, $dependencies)
|| ! isset($dependencies[$requestedName])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BC

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, it's not. In this case, we're looking to see if configuration exists for the given service name. There are essentially no cases where that configuration would be a non-array/array-like value. As such, isset() is sufficient here.

|| ! is_array($dependencies[$requestedName])
) {
throw new ServiceNotCreatedException('Dependencies config must exist and be an array');
Expand Down
2 changes: 0 additions & 2 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,6 @@ private function createDelegatorFromName($name, array $options = null)
};

foreach ($this->delegators[$name] as $index => $delegatorFactory) {
$delegatorFactory = $this->delegators[$name][$index];

if ($delegatorFactory === Proxy\LazyServiceFactory::class) {
$delegatorFactory = $this->createLazyServiceDelegatorFactory();
}
Expand Down