Skip to content

Commit

Permalink
Aligned with PR zendframework#242. Adds test to make sure the preconf…
Browse files Browse the repository at this point in the history
…igured settings

are processed. Resolves bug which could cause invokables to overwrite
delegators and factories. Removed tests which explicitly tested that
invokables were tranformed to aliases and factories (they are now stored
in a member array).
  • Loading branch information
fhein committed Feb 2, 2018
1 parent 3581ee8 commit 535c7df
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class InvalidArgumentException extends SplInvalidArgumentException implements Ex
public static function fromInvalidInitializer($initializer)
{
return new self(sprintf(
'An invalid initializer was registered. Expected a callable or an'
. ' instance of "%s"; received "%s"',
'An invalid initializer was registered. Expected a valid function name or '
. 'class name or a callable or an instance of "%s"; received "%s"',
InitializerInterface::class,
is_object($initializer) ? get_class($initializer) : gettype($initializer)
));
Expand Down
22 changes: 15 additions & 7 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use function spl_object_hash;
use function sprintf;
use function trigger_error;
use Zend\ServiceManager\Factory\InvokableFactory;

/**
* Service Manager.
Expand Down Expand Up @@ -89,6 +90,13 @@ class ServiceManager implements ServiceLocatorInterface
*/
protected $factories = [];

/**
* A list of invokable classes
*
* @var string[]|callable[]
*/
protected $invokables = [];

/**
* @var Initializer\InitializerInterface[]|callable[]
*/
Expand Down Expand Up @@ -171,9 +179,6 @@ public function __construct(array $config = [])
$this->resolveAbstractFactories(null);
}

if (! empty($this->invokables)) {
$this->createAliasesAndFactoriesForInvokables($this->invokables);
}
$this->configure($config);
}

Expand Down Expand Up @@ -268,7 +273,7 @@ public function has($name)
{
$resolvedName = $this->aliases[$name] ?? $name;
// Check services and factories first to speedup the most common requests.
if (isset($this->services[$resolvedName]) || isset($this->factories[$resolvedName])) {
if (isset($this->services[$resolvedName]) || isset($this->factories[$resolvedName]) || isset($this->invokables[$resolvedName])) {
return true;
}

Expand Down Expand Up @@ -352,7 +357,7 @@ public function configure(array $config)
}

if (! empty($config['invokables'])) {
$this->createAliasesAndFactoriesForInvokables($config['invokables']);
$this->invokables = $config['invokables'] + $this->invokables;
}

if (! empty($config['factories'])) {
Expand Down Expand Up @@ -425,7 +430,7 @@ public function setInvokableClass($name, $class = null)
if (isset($this->services[$name]) && ! $this->allowOverride) {
throw ContainerModificationsNotAllowedException::fromExistingService($name);
}
$this->createAliasesAndFactoriesForInvokables([$name => $class ?? $name]);
$this->invokables[$name] = $class ?? $name;
}

/**
Expand Down Expand Up @@ -564,7 +569,7 @@ private function resolveInitializers(array $initializers = null)
* @return callable
* @throws ServiceNotFoundException
*/
private function getFactory($name)
private function getFactory(&$name)
{
$factory = $this->factories[$name] ?? null;

Expand All @@ -580,6 +585,9 @@ private function getFactory($name)
}

return $factory;
} elseif (isset($this->invokables[$name])) {
$name = $this->invokables[$name];
return new InvokableFactory();
}

// Check abstract factories
Expand Down

0 comments on commit 535c7df

Please sign in to comment.