Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. <?php
  2. // Simple container
  3. // Простая реализация контейнера.
  4.  
  5. namespace System\Container;
  6.  
  7. use Psr\Container\ContainerInterface;
  8. use ReflectionClass;
  9. use ReflectionException;
  10.  
  11. /**
  12. * Class Container
  13. * @package System\Container
  14. */
  15. class Container implements ContainerInterface
  16. {
  17. /**
  18. * @var array
  19. */
  20. private $instances = [];
  21.  
  22. /**
  23. * @var array
  24. */
  25. private $bindings = [];
  26.  
  27. /**
  28. * @var Container
  29. */
  30. private static $instance;
  31.  
  32. /**
  33. * Container constructor.
  34. */
  35. private function __construct()
  36. {
  37. }
  38.  
  39. /**
  40. * Get container instance
  41. *
  42. * @return Container
  43. */
  44. public static function getInstance()
  45. {
  46. if (static::$instance === null) {
  47. static::$instance = new static();
  48. }
  49.  
  50. return static::$instance;
  51. }
  52.  
  53. /**
  54. * Added bind with the name
  55. *
  56. * @param string $name
  57. * @param mixed $source
  58. * @param mixed $params
  59. * @return $this
  60. */
  61. public function bind($name, $source, $params = [])
  62. {
  63. if (($source instanceof \Closure) || is_string($source)) {
  64. $this->bindings[$name] = [$source, $params];
  65. } else {
  66. $this->instances[$name] = $source;
  67. }
  68.  
  69. return $this;
  70. }
  71.  
  72. /**
  73. * @param string $name
  74. * @return mixed
  75. * @throws NotFoundException
  76. * @throws ReflectionException
  77. */
  78. public function get($name)
  79. {
  80. if ($this->has($name)) {
  81. if ($this->hasInstance($name)) {
  82. $instance = $this->instances[$name];
  83. } else {
  84. $instance = $this->binding($name);
  85. $this->instances[$name] = $instance;
  86. }
  87. return $instance;
  88. }
  89.  
  90. throw new NotFoundException();
  91. }
  92.  
  93. /**
  94. * Binding instance to the name
  95. *
  96. * @param string $name
  97. * @return mixed
  98. * @throws NotFoundException
  99. * @throws ReflectionException
  100. */
  101. private function binding($name)
  102. {
  103. list($source, $params) = $this->bindings[$name];
  104.  
  105. if (is_string($source) || class_exists($source)) {
  106. $reflector = new ReflectionClass($source);
  107. return $reflector->newInstanceArgs($params);
  108. } elseif ($source instanceof \Closure) {
  109. return call_user_func_array($source, $params);
  110. }
  111.  
  112. throw new NotFoundException();
  113. }
  114.  
  115. /**
  116. * Check exist instance with the name
  117. *
  118. * @param string $name
  119. * @return bool
  120. */
  121. private function hasInstance($name)
  122. {
  123. return isset($this->instances[$name]);
  124. }
  125.  
  126. /**
  127. * Check exist bind with the name
  128. *
  129. * @param string $name
  130. * @return bool
  131. */
  132. private function hasBinding($name)
  133. {
  134. return isset($this->bindings[$name]);
  135. }
  136.  
  137. /**
  138. * @param string $name
  139. * @return bool
  140. */
  141. public function has($name)
  142. {
  143. return $this->hasBinding($name) || $this->hasInstance($name);
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement