2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Holds the PMA\libraries\di\Container class
8 namespace PMA\libraries\di
;
10 use Psr\Container\ContainerInterface
;
15 * @package PMA\libraries\di
17 class Container
implements ContainerInterface
20 * @var Item[] $content
22 protected $content = array();
27 protected static $defaultContainer;
30 * Create a dependency injection container
32 * @param Container $base Container
34 public function __construct(Container
$base = null)
37 $this->content
= $base->content
;
39 $this->alias('container', 'Container');
41 $this->set('Container', $this);
45 * Get an object with given name and parameters
47 * @param string $name Name
48 * @param array $params Parameters
50 * @throws NotFoundException No entry was found for **this** identifier.
51 * @throws ContainerException Error while retrieving the entry.
55 public function get($name, $params = array())
57 if (!$this->has($name)) {
58 throw new NotFoundException("No entry was found for $name identifier.");
61 if (isset($this->content
[$name])) {
62 return $this->content
[$name]->get($params);
63 } else if (isset($GLOBALS[$name])) {
64 return $GLOBALS[$name];
66 throw new ContainerException("Error while retrieving the entry.");
71 * Returns true if the container can return an entry for the given identifier.
72 * Returns false otherwise.
74 * `has($name)` returning true does not mean that `get($name)` will not throw an exception.
75 * It does however mean that `get($name)` will not throw a `NotFoundException`.
77 * @param string $name Identifier of the entry to look for.
81 public function has($name)
83 return isset($this->content
[$name]) ||
isset($GLOBALS[$name]);
87 * Remove an object from container
89 * @param string $name Name
93 public function remove($name)
95 unset($this->content
[$name]);
99 * Rename an object in container
101 * @param string $name Name
102 * @param string $newName New name
106 public function rename($name, $newName)
108 $this->content
[$newName] = $this->content
[$name];
109 $this->remove($name);
113 * Set values in the container
115 * @param string|array $name Name
116 * @param mixed $value Value
120 public function set($name, $value = null)
122 if (is_array($name)) {
123 foreach ($name as $key => $val) {
124 $this->set($key, $val);
128 $this->content
[$name] = new ValueItem($value);
132 * Register a service in the container
134 * @param string $name Name
135 * @param mixed $service Service
139 public function service($name, $service = null)
141 if (!isset($service)) {
144 $this->content
[$name] = new ServiceItem($this, $service);
148 * Register a factory in the container
150 * @param string $name Name
151 * @param mixed $factory Factory
155 public function factory($name, $factory = null)
157 if (!isset($factory)) {
160 $this->content
[$name] = new FactoryItem($this, $factory);
164 * Register an alias in the container
166 * @param string $name Name
167 * @param string $target Target
171 public function alias($name, $target)
173 // The target may be not defined yet
174 $this->content
[$name] = new AliasItem($this, $target);
178 * Get the global default container
182 public static function getDefaultContainer()
184 if (!isset(static::$defaultContainer)) {
185 static::$defaultContainer = new Container();
187 return static::$defaultContainer;