Skip to content

Commit

Permalink
add new method for the condition
Browse files Browse the repository at this point in the history
this method is use to change the `loopItSelf` status
  • Loading branch information
mjamilasfihani committed Jan 25, 2023
1 parent 9e501d9 commit 93db899
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public function build(): Controller

$constructor = $this->getReflectionConstructor();

return ($constructor !== null) ? new $class(...$this->getInjectedClass($constructor, false)) : new $class();
return ($constructor !== null) ? new $class(...$this->asItSelfIsDisabled()->getInjectedClass($constructor)) : new $class();
}
}
22 changes: 19 additions & 3 deletions src/Libraries/Traits/WithInjectedClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,28 @@

trait WithInjectedClass
{
private bool $loopItSelf = false;

protected function asItSelfIsEnabled()
{
$this->loopItSelf = true;

return $this;
}

protected function asItSelfIsDisabled()
{
$this->loopItSelf = false;

return $this;
}

protected function setInjectedClass(ReflectionParameter $reflectionParameter): string
{
return $reflectionParameter->getType()->getName(); // @phpstan-ignore-line
}

protected function getInjectedClass(ReflectionMethod $constructor, bool $calling_itself = false): array
protected function getInjectedClass(ReflectionMethod $constructor): array
{
$injector = [];

Expand All @@ -22,10 +38,10 @@ protected function getInjectedClass(ReflectionMethod $constructor, bool $calling
$class = $this->setInjectedClass($param);

// re-define the constructor to support child injection
$constructor = ($calling_itself === false) ? Container::withClass($class)->initialize()->getReflectionConstructor() : null;
$constructor = ($this->loopItSelf === false) ? Container::withClass($class)->initialize()->getReflectionConstructor() : null;

// building up the library
$injector[$key] = (null !== $constructor) ? new $class(...$this->getInjectedClass($constructor, true)) : new $class();
$injector[$key] = (null !== $constructor) ? new $class(...$this->asItSelfIsEnabled()->getInjectedClass($constructor)) : new $class();
}

return $injector;
Expand Down

0 comments on commit 93db899

Please sign in to comment.