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

Commit 7c0e8dd

Browse files
author
fhein
committed
Optimized performance for setInvokableClass and handling of invokables
in configure. Removed obsolete $this->configured. Was set in configure() but only checked in trivial cases, where it was not meaningful.
1 parent 3fe8411 commit 7c0e8dd

File tree

1 file changed

+15
-57
lines changed

1 file changed

+15
-57
lines changed

src/ServiceManager.php

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,6 @@ class ServiceManager implements ServiceLocatorInterface
133133
*/
134134
protected $sharedByDefault = true;
135135

136-
/**
137-
* Service manager was already configured?
138-
*
139-
* @var bool
140-
*/
141-
protected $configured = false;
142-
143136
/**
144137
* Cached abstract factories from string.
145138
*
@@ -350,18 +343,7 @@ public function configure(array $config)
350343
}
351344

352345
if (isset($config['invokables']) && ! empty($config['invokables'])) {
353-
$aliases = $this->createAliasesForInvokables($config['invokables']);
354-
$factories = $this->createFactoriesForInvokables($config['invokables']);
355-
356-
if (! empty($aliases)) {
357-
$config['aliases'] = (isset($config['aliases']))
358-
? array_merge($config['aliases'], $aliases)
359-
: $aliases;
360-
}
361-
362-
$config['factories'] = (isset($config['factories']))
363-
? array_merge($config['factories'], $factories)
364-
: $factories;
346+
$this->createAliasesAndFactoriesForInvokables($config['invokables']);
365347
}
366348

367349
if (isset($config['factories'])) {
@@ -378,8 +360,9 @@ public function configure(array $config)
378360

379361
if (isset($config['aliases'])) {
380362
$this->aliases = $config['aliases'] + $this->aliases;
381-
$this->mapAliasesToTargets();
382-
} elseif (! $this->configured && ! empty($this->aliases)) {
363+
}
364+
365+
if (! empty($this->aliases)) {
383366
$this->mapAliasesToTargets();
384367
}
385368

@@ -408,8 +391,6 @@ public function configure(array $config)
408391
$this->resolveInitializers($config['initializers']);
409392
}
410393

411-
$this->configured = true;
412-
413394
return $this;
414395
}
415396

@@ -437,7 +418,11 @@ public function setAlias($alias, $target)
437418
*/
438419
public function setInvokableClass($name, $class = null)
439420
{
440-
$this->configure(['invokables' => [$name => $class ?: $name]]);
421+
if (! isset($this->services[$name]) || $this->allowOverride) {
422+
$this->createAliasesAndFactoriesForInvokables([$name => $class ?? $name]);
423+
return;
424+
}
425+
throw new ContainerModificationsNotAllowedException($name);
441426
}
442427

443428
/**
@@ -763,49 +748,22 @@ private function createLazyServiceDelegatorFactory()
763748
}
764749

765750
/**
766-
* Create aliases for invokable classes.
751+
* Create aliases and factories for invokable classes.
767752
*
768753
* If an invokable service name does not match the class it maps to, this
769754
* creates an alias to the class (which will later be mapped as an
770755
* invokable factory).
771756
*
772757
* @param array $invokables
773-
* @return array
774758
*/
775-
private function createAliasesForInvokables(array $invokables)
759+
private function createAliasesAndFactoriesForInvokables(array $invokables)
776760
{
777-
$aliases = [];
778761
foreach ($invokables as $name => $class) {
779-
if ($name === $class) {
780-
continue;
762+
$this->factories[$class] = Factory\InvokableFactory::class;
763+
if ($name !== $class) {
764+
$this->aliases[$name] = $class;
781765
}
782-
$aliases[$name] = $class;
783-
}
784-
return $aliases;
785-
}
786-
787-
/**
788-
* Create invokable factories for invokable classes.
789-
*
790-
* If an invokable service name does not match the class it maps to, this
791-
* creates an invokable factory entry for the class name; otherwise, it
792-
* creates an invokable factory for the entry name.
793-
*
794-
* @param array $invokables
795-
* @return array
796-
*/
797-
private function createFactoriesForInvokables(array $invokables)
798-
{
799-
$factories = [];
800-
foreach ($invokables as $name => $class) {
801-
if ($name === $class) {
802-
$factories[$name] = Factory\InvokableFactory::class;
803-
continue;
804-
}
805-
806-
$factories[$class] = Factory\InvokableFactory::class;
807766
}
808-
return $factories;
809767
}
810768

811769
/**
@@ -824,7 +782,7 @@ private function createFactoriesForInvokables(array $invokables)
824782
*/
825783
private function validateServiceNames(array $config)
826784
{
827-
if ($this->allowOverride || ! $this->configured) {
785+
if ($this->allowOverride) {
828786
return;
829787
}
830788

0 commit comments

Comments
 (0)