Skip to content

Commit

Permalink
Caching override injector for current override module
Browse files Browse the repository at this point in the history
  • Loading branch information
NaokiTsuchiya committed Mar 28, 2023
1 parent 59ced62 commit c058571
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class Application extends \Illuminate\Foundation\Application

private AbstractModule|null $overrideModule = null;

/** @var array<string, InjectorInterface> */
private array $overrideInjectorInstance = [];
private InjectorInterface|null $overrideInjectorInstance = null;

public function __construct(string $basePath, AbstractInjectorContext $injectorContext)
{
Expand Down Expand Up @@ -79,12 +78,13 @@ public function flush()

$this->overrideModule = null;
$this->abstractsResolvedByRay = [];
$this->overrideInjectorInstance = [];
$this->overrideInjectorInstance = null;
}

public function overrideModule(AbstractModule $module): void
{
$this->overrideModule = $module;
$this->overrideInjectorInstance = null;
}

private function getInjector(): InjectorInterface
Expand All @@ -93,15 +93,12 @@ private function getInjector(): InjectorInterface
return $this->injector;
}

$currentModuleString = spl_object_hash($this->overrideModule);

if (isset($this->overrideInjectorInstance[$currentModuleString])) {
return $this->overrideInjectorInstance[$currentModuleString];
if ($this->overrideInjectorInstance !== null) {
return $this->overrideInjectorInstance;
}

$injector = ContextInjector::getOverrideInstance($this->context, $this->overrideModule);
$newModuleString = spl_object_hash($this->overrideModule);
$this->overrideInjectorInstance[$newModuleString] = $injector;
$this->overrideInjectorInstance = $injector;

return $injector;
}
Expand Down

0 comments on commit c058571

Please sign in to comment.