| 1 |
<?php namespace Viocon; |
| 2 |
|
| 3 |
class Container |
| 4 |
{ |
| 5 |
/** |
| 6 |
* @var array |
| 7 |
*/ |
| 8 |
public $registry = array(); |
| 9 |
|
| 10 |
/** |
| 11 |
* Singleton instances |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
public $singletons = array(); |
| 16 |
|
| 17 |
public function __construct($alias = null) |
| 18 |
{ |
| 19 |
if ($alias) { |
| 20 |
AliasFacade::setVioconInstance($this); |
| 21 |
class_alias('\\Viocon\\AliasFacade', $alias); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Register an object with a key |
| 27 |
* |
| 28 |
* @param string $key |
| 29 |
* @param mixed $object |
| 30 |
* @param bool $singleton |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function set($key, $object, $singleton = false) |
| 35 |
{ |
| 36 |
$this->registry[$key] = compact('object', 'singleton'); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* If we have a registry for the given key |
| 41 |
* |
| 42 |
* @param string $key |
| 43 |
* |
| 44 |
* @return bool |
| 45 |
*/ |
| 46 |
public function has($key) |
| 47 |
{ |
| 48 |
return array_key_exists($key, $this->registry); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Register as singleton. |
| 53 |
* |
| 54 |
* @param string $key |
| 55 |
* @param mixed $object |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function singleton($key, $object) |
| 60 |
{ |
| 61 |
$this->set($key, $object, true); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Register or replace an instance as a singleton. |
| 66 |
* Useful for replacing with Mocked instance |
| 67 |
* |
| 68 |
* @param string $key |
| 69 |
* @param mixed $instance |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function setInstance($key, $instance) |
| 74 |
{ |
| 75 |
$this->singletons[$key] = $instance; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Build from the given key. |
| 80 |
* If there is a class registered with Container::set() then it's instance |
| 81 |
* will be returned. If a closure is registered, a closure's return value |
| 82 |
* will be returned. If nothing is registered then it will try to build an |
| 83 |
* instance with new $key(...). |
| 84 |
* |
| 85 |
* $parameters will be passed to closure or class constructor. |
| 86 |
* |
| 87 |
* |
| 88 |
* @param string $key |
| 89 |
* @param array $parameters |
| 90 |
* |
| 91 |
* @return mixed |
| 92 |
*/ |
| 93 |
public function build($key, $parameters = array()) |
| 94 |
{ |
| 95 |
// If we have a singleton instance registered the just return it |
| 96 |
if (array_key_exists($key, $this->singletons)) { |
| 97 |
return $this->singletons[$key]; |
| 98 |
} |
| 99 |
|
| 100 |
// If we don't have a registered object with the key then assume user |
| 101 |
// is trying to build a class with the given key/name |
| 102 |
|
| 103 |
if (!array_key_exists($key, $this->registry)) { |
| 104 |
$object = $key; |
| 105 |
} else { |
| 106 |
$object = $this->registry[$key]['object']; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
$instance = $this->instanciate($object, $parameters); |
| 111 |
|
| 112 |
// If the key is registered as a singleton, we can save the instance as singleton |
| 113 |
// for later use |
| 114 |
if (isset($this->registry[$key]['singleton']) && $this->registry[$key]['singleton'] === true) { |
| 115 |
$this->singletons[$key] = $instance; |
| 116 |
} |
| 117 |
|
| 118 |
return $instance; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Instantiate an instance of the given type. |
| 123 |
* |
| 124 |
* @param string $key |
| 125 |
* @param array $parameters |
| 126 |
* |
| 127 |
* @throws \Exception |
| 128 |
* @return mixed |
| 129 |
*/ |
| 130 |
protected function instanciate($key, $parameters = null) |
| 131 |
{ |
| 132 |
|
| 133 |
if ($key instanceof \Closure) { |
| 134 |
return call_user_func_array($key, $parameters); |
| 135 |
} |
| 136 |
|
| 137 |
$reflection = new \ReflectionClass($key); |
| 138 |
return $reflection->newInstanceArgs($parameters); |
| 139 |
} |
| 140 |
} |