Skip to content

Commit

Permalink
Improved resolving of primitive dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
reinvanoyen committed Jan 21, 2020
1 parent 5ada057 commit 7728d61
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ class Container implements ContainerInterface
private $arguments = [];

/**
* Stores a implementation in the container for the given key
*
* @param string $key
* @param mixed $implementation
* @param string $contract
* @param $implementation
* @return mixed|void
*/
public function set(string $contract, $implementation)
{
Expand Down Expand Up @@ -113,7 +112,7 @@ public function get(string $contract)

/**
* @param string $contract
* @param string $arg
* @param string $argument
* @param $value
*/
public function whenAsksGive(string $contract, string $argument, $value)
Expand All @@ -137,11 +136,10 @@ public function getWith(string $contract, array $arguments)
}

/**
* Creates an instance from a contract
*
* @param string $contract
* @return mixed
* @throws \Exception
* @param array $arguments
* @return mixed|object
* @throws \ReflectionException
*/
private function create(string $contract, array $arguments = [])
{
Expand All @@ -158,7 +156,7 @@ private function create(string $contract, array $arguments = [])

// Check if we have to give this class some stored arguments
if (is_string($implementation) && isset($this->arguments[$implementation])) {
$arguments = array_merge($arguments, $this->arguments[$implementation]);
$arguments = array_merge($this->arguments[$implementation], $arguments);
}

// Is it callable? Call it right away and return the results
Expand Down Expand Up @@ -191,15 +189,45 @@ private function create(string $contract, array $arguments = [])
// Check if it was explicitely given as an argument
if (isset($arguments[$className])) {

// Get the explicit argument from the container
$injections[] = $this->get($arguments[$className]);
continue;
// Check if it's a string
if (is_string($arguments[$className])) {

// If so, get it from the container
$injections[] = $this->get($arguments[$className]);
continue;

} else {

// ...else inject it raw
$injections[] = $arguments[$className];
continue;
}
}

// Get the class from the container
$argName = $parameter->getName();

// Check if the argument was given by argument name instead of class
if (isset($arguments[$argName])) {

// Check if it's a string
if (is_string($arguments[$argName])) {

// If so, get it from the container
$injections[] = $this->get($arguments[$argName]);
continue;

} else {

// ...else inject it raw
$injections[] = $arguments[$argName];
continue;
}
}

// Check if the container has the class
if ($this->has($className)) {

// Get the class from the container and add it as injection
$injections[] = $this->get($className);
continue;
}
Expand All @@ -212,8 +240,8 @@ private function create(string $contract, array $arguments = [])
continue;
}

// Try to inject it anyway
$injections[] = $this->get($className);

continue;

} else {
Expand Down

0 comments on commit 7728d61

Please sign in to comment.