Skip to content

Commit

Permalink
whenAsksGive
Browse files Browse the repository at this point in the history
  • Loading branch information
reinvanoyen committed Jan 15, 2020
1 parent e37cc87 commit f028b39
Showing 1 changed file with 75 additions and 17 deletions.
92 changes: 75 additions & 17 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Oak\Container;

use Exception;
use Oak\Contracts\Container\ContainerInterface;
use ReflectionClass;

Expand Down Expand Up @@ -32,6 +33,11 @@ class Container implements ContainerInterface
*/
private $instances = [];

/**
* @var array
*/
private $arguments = [];

/**
* Stores a implementation in the container for the given key
*
Expand All @@ -43,6 +49,17 @@ public function set(string $contract, $implementation)
$this->contracts[$contract] = $implementation;
}

/**
* Checks if the container has a value by a given contract
*
* @param string $contract
* @return bool
*/
public function has(string $contract): bool
{
return isset($this->contracts[$contract]);
}

/**
* Stores a implementation in the container for the given key and also store it as a singleton
*
Expand Down Expand Up @@ -71,6 +88,7 @@ public function instance(string $contract, $instance)
* Retreives a value from the container by a given contract
*
* @param string $contract
* @return mixed
* @throws \Exception
*/
public function get(string $contract)
Expand All @@ -95,24 +113,27 @@ public function get(string $contract)

/**
* @param string $contract
* @param array $arguments
* @return mixed
* @throws \Exception
* @param string $arg
* @param $value
*/
public function getWith(string $contract, array $arguments)
public function whenAsksGive(string $contract, string $argument, $value)
{
return $this->create($contract, $arguments);
if (! isset($this->arguments[$contract])) {
$this->arguments[$contract] = [];
}

$this->arguments[$contract][$argument] = $value;
}

/**
* Checks if the container has a value by a given contract
*
* @param string $contract
* @return bool
* @param array $arguments
* @return mixed
* @throws \Exception
*/
public function has(string $contract): bool
public function getWith(string $contract, array $arguments)
{
return isset($this->contracts[$contract]);
return $this->create($contract, $arguments);
}

/**
Expand All @@ -135,6 +156,12 @@ private function create(string $contract, array $arguments = [])
$implementation = $this->contracts[$contract];
}

// 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]);
}

// Is it callable? Call it right away and return the results
if (is_callable($implementation)) {
return call_user_func($implementation, $this);
}
Expand All @@ -156,16 +183,47 @@ private function create(string $contract, array $arguments = [])

foreach ($parameters as $parameter) {

// Check if the parameter was given
if (isset($arguments[$parameter->getName()])) {
$injections[] = $arguments[$parameter->getName()];
// Check if param is a class
if ($class = $parameter->getClass()) {

$className = $class->name;

// 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;

}

// Get the class from the container
$injections[] = $this->get($className);

continue;
}

// It's a dependency, so inject it
$class = $parameter->getClass()->name;
} else {

$argName = $parameter->getName();

// Check if the argument was explicitely given
if (isset($arguments[$argName])) {

$injections[] = $arguments[$argName];
continue;

}

// Check if the argument has a default value
if($parameter->isDefaultValueAvailable()) {

// Inject the default value
$injections[] = $parameter->getDefaultValue();
continue;
}
}

$injections[] = $this->get($class);
throw new Exception('Could not provide argument "'.$parameter->getName().'" to '.$contract);
}

return $reflect->newInstanceArgs($injections);
Expand Down

0 comments on commit f028b39

Please sign in to comment.