Container.class.php 3.7 KB

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