Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. <?php
  2. namespace App\DI;
  3.  
  4. use Closure;
  5. use App\DI\ServiceProviderInterface;
  6. //use olifant\exceptions\AppException;
  7.  
  8. class Container implements \ArrayAccess
  9. {
  10. protected $aliases = [];
  11. protected $freezed = [];
  12. protected $instances = [];
  13. protected $shared = [];
  14.  
  15. public function set($key, $entity)
  16. {
  17. if (null === $key) {
  18. throw new Appexception('Key cannot be null');
  19. }
  20.  
  21. $this->instances[$key] = $entity;
  22. }
  23.  
  24. public function singleton($key, $entity)
  25. {
  26. $this->shared[$key] = $entity;
  27. }
  28.  
  29. public function has($key)
  30. {
  31. $key = $this->resolveAlias($key);
  32.  
  33. return (
  34. isset($this->instances[$key]) or
  35. isset($this->shared[$key])
  36. );
  37. }
  38.  
  39. public function alias($alias, $key)
  40. {
  41. $this->aliases[$alias] = $key;
  42.  
  43. return $this;
  44. }
  45.  
  46. protected function resolveAlias($key)
  47. {
  48. if (isset($this->aliases[$key])) {
  49. return $this->aliases[$key];
  50. }
  51.  
  52. return $key;
  53. }
  54.  
  55. public function extend($key, \Closure $callable)
  56. {
  57. $store = $this->get($key);
  58.  
  59. $closure = function ($container) use($callable, $store) {
  60. return $callable($store->get($container), $container);
  61. };
  62.  
  63. $this->set($key, $closure, $store->isShared());
  64.  
  65. return $this;
  66. }
  67.  
  68. public function fork($key, $newkey)
  69. {
  70.  
  71. }
  72.  
  73. public function get($key)
  74. {
  75. $key = $this->resolveAlias($key);
  76.  
  77. if(isset($this->instances[$key])){
  78. if($this->instances[$key] instanceof Closure)
  79. return call_user_func_array(
  80. $this->instances[$key],
  81. array($this)
  82. );
  83.  
  84. return $this->instances[$key];
  85. }
  86.  
  87. if(isset($this->shared[$key])){
  88. if($this->shared[$key] instanceof Closure)
  89. $this->shared[$key] = call_user_func_array(
  90. $this->shared[$key],
  91. array($this)
  92. );
  93.  
  94. return $this->shared[$key];
  95. }
  96. }
  97.  
  98. public function offsetSet($offset, $value)
  99. {
  100. $this->set($offset,$value);
  101. }
  102.  
  103. public function offsetExists($offset)
  104. {
  105. return $this->has($offset);
  106. }
  107.  
  108. public function offsetGet($offset)
  109. {
  110. return $this->get($offset);
  111. }
  112.  
  113. public function offsetUnset($offset)
  114. {
  115. //$offset = $this->resolveAlias($offset);
  116.  
  117. unset(
  118. $this->instances[$offset],
  119. $this->shared[$offset]
  120. );
  121. }
  122.  
  123. public function keys()
  124. {
  125. return array_unique(
  126. array_merge(
  127. array_keys($this->instances),
  128. array_keys($this->shared),
  129. array_keys($this->aliases)
  130. )
  131. );
  132. }
  133.  
  134. public function register(ServiceProviderInterface $provider)
  135. {
  136. $provider->register($this);
  137.  
  138. return $this;
  139. }
  140. }
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement