Container.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Di\Container class
  5. *
  6. * @package PhpMyAdmin\Di
  7. */
  8. namespace PhpMyAdmin\Di;
  9. use Psr\Container\ContainerInterface;
  10. /**
  11. * Class Container
  12. *
  13. * @package PhpMyAdmin\Di
  14. */
  15. class Container implements ContainerInterface
  16. {
  17. /**
  18. * @var Item[] $content
  19. */
  20. protected $content = array();
  21. /**
  22. * @var Container
  23. */
  24. protected static $defaultContainer;
  25. /**
  26. * Create a dependency injection container
  27. *
  28. * @param Container $base Container
  29. */
  30. public function __construct(Container $base = null)
  31. {
  32. if (isset($base)) {
  33. $this->content = $base->content;
  34. } else {
  35. $this->alias('container', 'Container');
  36. }
  37. $this->set('Container', $this);
  38. }
  39. /**
  40. * Get an object with given name and parameters
  41. *
  42. * @param string $name Name
  43. * @param array $params Parameters
  44. *
  45. * @throws NotFoundException No entry was found for **this** identifier.
  46. * @throws ContainerException Error while retrieving the entry.
  47. *
  48. * @return mixed
  49. */
  50. public function get($name, array $params = array())
  51. {
  52. if (!$this->has($name)) {
  53. throw new NotFoundException("No entry was found for $name identifier.");
  54. }
  55. if (isset($this->content[$name])) {
  56. return $this->content[$name]->get($params);
  57. } elseif (isset($GLOBALS[$name])) {
  58. return $GLOBALS[$name];
  59. } else {
  60. throw new ContainerException("Error while retrieving the entry.");
  61. }
  62. }
  63. /**
  64. * Returns true if the container can return an entry for the given identifier.
  65. * Returns false otherwise.
  66. *
  67. * `has($name)` returning true does not mean that `get($name)` will not throw an exception.
  68. * It does however mean that `get($name)` will not throw a `NotFoundException`.
  69. *
  70. * @param string $name Identifier of the entry to look for.
  71. *
  72. * @return bool
  73. */
  74. public function has($name)
  75. {
  76. return isset($this->content[$name]) || isset($GLOBALS[$name]);
  77. }
  78. /**
  79. * Remove an object from container
  80. *
  81. * @param string $name Name
  82. *
  83. * @return void
  84. */
  85. public function remove($name)
  86. {
  87. unset($this->content[$name]);
  88. }
  89. /**
  90. * Rename an object in container
  91. *
  92. * @param string $name Name
  93. * @param string $newName New name
  94. *
  95. * @return void
  96. */
  97. public function rename($name, $newName)
  98. {
  99. $this->content[$newName] = $this->content[$name];
  100. $this->remove($name);
  101. }
  102. /**
  103. * Set values in the container
  104. *
  105. * @param string|array $name Name
  106. * @param mixed $value Value
  107. *
  108. * @return void
  109. */
  110. public function set($name, $value = null)
  111. {
  112. if (is_array($name)) {
  113. foreach ($name as $key => $val) {
  114. $this->set($key, $val);
  115. }
  116. return;
  117. }
  118. $this->content[$name] = new ValueItem($value);
  119. }
  120. /**
  121. * Register a service in the container
  122. *
  123. * @param string $name Name
  124. * @param mixed $service Service
  125. *
  126. * @return void
  127. */
  128. public function service($name, $service = null)
  129. {
  130. if (!isset($service)) {
  131. $service = $name;
  132. }
  133. $this->content[$name] = new ServiceItem($this, $service);
  134. }
  135. /**
  136. * Register a factory in the container
  137. *
  138. * @param string $name Name
  139. * @param mixed $factory Factory
  140. *
  141. * @return void
  142. */
  143. public function factory($name, $factory = null)
  144. {
  145. if (!isset($factory)) {
  146. $factory = $name;
  147. }
  148. $this->content[$name] = new FactoryItem($this, $factory);
  149. }
  150. /**
  151. * Register an alias in the container
  152. *
  153. * @param string $name Name
  154. * @param string $target Target
  155. *
  156. * @return void
  157. */
  158. public function alias($name, $target)
  159. {
  160. // The target may be not defined yet
  161. $this->content[$name] = new AliasItem($this, $target);
  162. }
  163. /**
  164. * Get the global default container
  165. *
  166. * @return Container
  167. */
  168. public static function getDefaultContainer()
  169. {
  170. if (!isset(static::$defaultContainer)) {
  171. static::$defaultContainer = new Container();
  172. }
  173. return static::$defaultContainer;
  174. }
  175. }