Translated using Weblate (Dutch)
[phpmyadmin.git] / libraries / di / Container.php
blob557a20c0026ff356d3ccc79e00af629d8e39caa5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds the PMA\libraries\di\Container class
6 * @package PMA
7 */
8 namespace PMA\libraries\di;
10 use Psr\Container\ContainerInterface;
12 /**
13 * Class Container
15 * @package PMA\libraries\di
17 class Container implements ContainerInterface
19 /**
20 * @var Item[] $content
22 protected $content = array();
24 /**
25 * @var Container
27 protected static $defaultContainer;
29 /**
30 * Create a dependency injection container
32 * @param Container $base Container
34 public function __construct(Container $base = null)
36 if (isset($base)) {
37 $this->content = $base->content;
38 } else {
39 $this->alias('container', 'Container');
41 $this->set('Container', $this);
44 /**
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.
53 * @return mixed
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];
65 } else {
66 throw new ContainerException("Error while retrieving the entry.");
70 /**
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.
79 * @return bool
81 public function has($name)
83 return isset($this->content[$name]) || isset($GLOBALS[$name]);
86 /**
87 * Remove an object from container
89 * @param string $name Name
91 * @return void
93 public function remove($name)
95 unset($this->content[$name]);
98 /**
99 * Rename an object in container
101 * @param string $name Name
102 * @param string $newName New name
104 * @return void
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
118 * @return void
120 public function set($name, $value = null)
122 if (is_array($name)) {
123 foreach ($name as $key => $val) {
124 $this->set($key, $val);
126 return;
128 $this->content[$name] = new ValueItem($value);
132 * Register a service in the container
134 * @param string $name Name
135 * @param mixed $service Service
137 * @return void
139 public function service($name, $service = null)
141 if (!isset($service)) {
142 $service = $name;
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
153 * @return void
155 public function factory($name, $factory = null)
157 if (!isset($factory)) {
158 $factory = $name;
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
169 * @return void
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
180 * @return Container
182 public static function getDefaultContainer()
184 if (!isset(static::$defaultContainer)) {
185 static::$defaultContainer = new Container();
187 return static::$defaultContainer;