diff --git a/src/Container/Container.php b/src/Container/Container.php index 1143269..c00d9ab 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -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) { @@ -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) @@ -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 = []) { @@ -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 @@ -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; } @@ -212,8 +240,8 @@ private function create(string $contract, array $arguments = []) continue; } + // Try to inject it anyway $injections[] = $this->get($className); - continue; } else {