Witam.
Chciałem przedstawić klasę prostego kontenera.
Wzorowałem się na kontenerze z laravela, lecz nie "kopiowałem", a pisałem samodzielnie, co na pewno widać
Chciałbym prosić o ocenę kodu, w miarę możliwości tego jak będzie wydajny oraz wytknięcie błędów.
Z góry dziękuję za waszą pomoc
class Container implements ArrayAccess {
protected
$bindings = array();
protected
$instances = array();
private $maxdepth = 10;
// ================================================== //
public function make
($key, $params = array()) {
if($bind = $this->getBinding($key))
{
if(isset($bind['closure'])) {
if(isset($bind['singleton']) and
$bind['singleton'] === true) {
$bind['instance'] = $bind['closure']();
$this->instances[$key] = $bind['instance'];
unset($this->bindings[$key]['closure'], $this->bindings[$key]['singleton']); }
else
{
$bind['instance'] = $bind['closure']();
}
}
return $bind['instance'];
}
else
{
$ret = $this->makeClassWithParams($key, (array)$params);
if($ret != false)
{
$this->bind($key, $ret);
}
return $ret;
}
}
public function bind($key, $value)
{
return $this->makeBinding($key, $value, false);
}
public function singleton($key, $value)
{
return $this->makeBinding($key, $value, true);
}
// ================================================== //
protected
function makeClassWithParams
($name, array $params, $depth = 1
) {
if($depth >= $this->maxdepth)
{
throw new OutOfBoundsException('Maximum depth of makeClassWithParams is '.$this->maxdepth);
return false;
}
if(class_exists($name))
{
$class = new ReflectionClass($name);
$constructor = $class->getConstructor()->getParameters();
{
$constructor = $class->getConstructor()->getParameters();
foreach($constructor as $classparam)
{
$classparam = $classparam->getClass();
{
$params[] = null;
}
else
{
$params[] = $this->makeClassWithParams($classparam->name, array(), $depth+1
); }
}
$class = $class->newInstanceArgs($params);
}
else
{
$class = $class->newInstanceArgs($params);
}
return $class;
}
else
{
return false;
}
}
protected function makeBinding($key, $value, $singleton)
{
$this->clearBinding($key);
$bind['instance'] = $key;
if($value instanceof Closure)
{
$bind['closure'] = $value;
$bind['singleton'] = (bool)$singleton;
}
else
{
$bind['instance'] = $value;
}
if(isset($bind['instance'])) {
$this->instances[$key] = $bind['instance'];
}
else
{
$this->instances[$key] = null;
}
$bind['instance'] = $key;
$this->bindings[$key] = $bind;
}
protected function getBinding($key)
{
if(isset($this->bindings[$key])) {
$bind = $this->bindings[$key];
if(! is_null($this->instances[$key])) {
$bind['instance'] = $this->instances[$key];
}
return $bind;
}
return false;
}
protected function clearBinding($key)
{
unset($this->bindings[$key], $this->instances[$key]); }
// ================================================== //
public function offsetGet($key)
{
return $this->make($key);
}
public function offsetSet($key, $value)
{
$function = function() use ($value) {
return $value;
};
$this->bind($key, $function);
}
public function offsetExists($key)
{
return isset($this->bindings['key']); }
public function offsetUnset($key)
{
$this->clearBinding($key);
}
public function __set($key, $value)
{
$this[$key] = $value;
}
public function __get($key)
{
return $this[$key];
}
}
Ten post edytował untorched 5.10.2014, 00:50:05