Reputation: 1951
I'm making my own framework for practice and I'm stuck with DI/IoC container, with sending parameters in constructor.
Container.php
class Container
{
private $registry = array();
private $shared = array();
public function set($name, Closure $resolve)
{
if (!$this->exists($name)) {
$this->registry[$name] = $resolve;
} else {
throw new Exception('Class with name ' . $name . ' is already registered.');
}
}
public function get($name, $arguments = array())
{
if (!$this->exists($name, 'shared')) {
$this->shared[$name] = $this->getNew($name, $arguments);
}
return $this->shared[$name];
}
public function getNew($name, $arguments = array())
{
if ($this->exists($name, 'registry')) {
$class = $this->registry[$name];
return $class();
}
throw new Exception('Class with name ' . $name . ' does not exist.');
}
private function exists($name, $type = 'registry')
{
return array_key_exists($name, $this->$type);
}
}
Usage:
$container = new Container;
$container->set('file', function() {
return new Application\Library\File();
});
$container->set('cache', function($path, $type = 'json') use ($container) {
return new Application\Library\Cache($container->get('file'), $path, $type);
});
$params = array (
'path/to/cache/',
'json'
);
$cache = $container->get('cache', $params);
Question is: how can i send parameters/arguments with get() or getNew() method to constructor. I saw ReflectionClass($classname) and newInstanceArgs($args) but I don't know how to use it and where to put it.
EDIT - Maybe something like this:
public function getNew($name, $arguments = array())
{
if ($this->exists($name, 'registry')) {
if (count($arguments) > 0) {
$object = new ReflectionClass($name);
return $object->newInstanceArgs($arguments);
} else {
$class = $this->registry[$name];
return $class();
}
}
throw new Exception('Class with name ' . $name . ' does not exist.');
}
But it does not work because for creating Cache class I inject only 2 of 3 parameters with $container->get() and $params array. First parameter is fixed in $container->set() with File class and it does not inject properly with instance creation. Is there any solution ?
Error: Catchable fatal error: Argument 1 passed to Application\Library\Cache::__construct() must be an instance of Application\Library\File, string given
Upvotes: 2
Views: 640
Reputation: 1951
Possible solution that works but I'm not happy with it:
public function getNew($name, $arguments = array())
{
if ($this->exists($name, 'registry')) {
$class = $this->registry[$name];
$count = count($arguments);
switch ($count) {
case 1:
$object = $class($arguments[0]);
break;
case 2:
$object = $class($arguments[0], $arguments[1]);
break;
case 3:
$object = $class($arguments[0], $arguments[1], $arguments[2]);
break;
case 4:
$object = $class($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
break;
default:
$object = $class();
break;
}
return $object;
}
throw new Exception('Class with name ' . $name . ' does not exist.');
}
Counts number of $arguments, going through switch case and return instance of $classname with injected arguments.
Upvotes: 0