<?php
namespace Laravel\Container;
class Container
{
protected $bindings = [];
protected $instances = [];
public function bind($abstract, $concrete = null, $shared = false)
{
$this->bindings[$abstract]['shared'] = $shared;
$this->bindings[$abstract]['concrete'] = $concrete;
}
public function getBindings()
{
}
public function make($abstract, $parameters = [])
{
if (!$this->has($abstract)) {
new \Exception('你提供的不存在');
}
if ($this->instances[$abstract]) {
return $this->instances[$abstract];
}
$object = $this->bindings[$abstract]['concrete'];
if ($object instanceof \Closure) {
return $object();
}
if (!is_object($object)) {
$object = new $object(...$parameters);
}
if ($this->bindings[$abstract]['shared']) {
$this->instances[$abstract] = $object;
}
return $object;
}
public function singleton($abstract, $concrete = null, $shared = true)
{
$this->bind($abstract, $concrete, $shared);
}
public function has($abstract)
{
return isset($this->bindings[$abstract]['concrete']) ||
isset($this->instances[$abstract]['concrete']);
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結