container.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php namespace Laravel;
  2. class IoC {
  3. /**
  4. * The active container instance.
  5. *
  6. * @var Container
  7. */
  8. public static $container;
  9. /**
  10. * Get the active container instance.
  11. *
  12. * The container is set early in the request cycle and can be access here for
  13. * use as a service locator if object injection is not practical.
  14. *
  15. * <code>
  16. * // Get the active container instance
  17. * $container = IoC::container();
  18. *
  19. * // Get the active container instance and call the resolve method
  20. * $container = IoC::container()->resolve('instance');
  21. * </code>
  22. *
  23. * @return Container
  24. */
  25. public static function container()
  26. {
  27. return static::$container;
  28. }
  29. /**
  30. * Magic Method for calling methods on the active container instance.
  31. *
  32. * <code>
  33. * // Call the "resolve" method on the active container instance
  34. * $instance = IoC::resolve('instance');
  35. *
  36. * // Equivalent operation using the "container" method
  37. * $instance = IoC::container()->resolve('instance');
  38. * </code>
  39. */
  40. public static function __callStatic($method, $parameters)
  41. {
  42. return call_user_func_array(array(static::$container, $method), $parameters);
  43. }
  44. }
  45. class Container {
  46. /**
  47. * The resolved singleton instances.
  48. *
  49. * @var array
  50. */
  51. public $singletons = array();
  52. /**
  53. * The registered dependencies.
  54. *
  55. * @var array
  56. */
  57. protected $registry = array();
  58. /**
  59. * Create a new IoC container instance.
  60. *
  61. * @param array $registry
  62. * @return void
  63. */
  64. public function __construct($registry = array())
  65. {
  66. $this->registry = $registry;
  67. }
  68. /**
  69. * Register an object and its resolver.
  70. *
  71. * The resolver function is called when the registered object is requested.
  72. *
  73. * <code>
  74. * // Register an object in the container
  75. * IoC::register('something', function($container) {return new Something;});
  76. *
  77. * // Register an object in the container as a singleton
  78. * IoC::register('something', function($container) {return new Something;}, true);
  79. * </code>
  80. *
  81. * @param string $name
  82. * @param Closure $resolver
  83. * @return void
  84. */
  85. public function register($name, $resolver, $singleton = false)
  86. {
  87. $this->registry[$name] = array('resolver' => $resolver, 'singleton' => $singleton);
  88. }
  89. /**
  90. * Determine if an object has been registered in the container.
  91. *
  92. * @param string $name
  93. * @return bool
  94. */
  95. public function registered($name)
  96. {
  97. return array_key_exists($name, $this->registry);
  98. }
  99. /**
  100. * Register an object as a singleton.
  101. *
  102. * Singletons will only be instantiated the first time they are resolved. On subsequent
  103. * requests for the object, the original instance will be returned.
  104. *
  105. * <code>
  106. * // Register an object in the container as a singleton
  107. * IoC::singleton('something', function($container) {return new Something;});
  108. * </code>
  109. *
  110. * @param string $name
  111. * @param Closure $resolver
  112. * @return void
  113. */
  114. public function singleton($name, $resolver)
  115. {
  116. $this->register($name, $resolver, true);
  117. }
  118. /**
  119. * Register an instance as a singleton.
  120. *
  121. * This method allows you to register an already existing object instance with the
  122. * container to be managed as a singleton instance.
  123. *
  124. * <code>
  125. * // Register an instance with the IoC container
  126. * IoC::instance('something', new Something);
  127. * </code>
  128. *
  129. * @param string $name
  130. * @param mixed $instance
  131. * @return void
  132. */
  133. public function instance($name, $instance)
  134. {
  135. $this->singletons[$name] = $instance;
  136. }
  137. /**
  138. * Resolve an object.
  139. *
  140. * The object's resolver will be called and its result will be returned. If the
  141. * object is registered as a singleton and has already been resolved, the instance
  142. * that has already been instantiated will be returned.
  143. *
  144. * <code>
  145. * // Get the "something" object out of the IoC container
  146. * $something = IoC::resolve('something');
  147. * </code>
  148. *
  149. * @param string $name
  150. * @return mixed
  151. */
  152. public function resolve($name)
  153. {
  154. if (array_key_exists($name, $this->singletons)) return $this->singletons[$name];
  155. if ( ! $this->registered($name))
  156. {
  157. throw new \Exception("Error resolving [$name]. No resolver has been registered in the container.");
  158. }
  159. $object = call_user_func($this->registry[$name]['resolver'], $this);
  160. return (isset($this->registry[$name]['singleton'])) ? $this->singletons[$name] = $object : $object;
  161. }
  162. /**
  163. * Magic Method for resolving classes out of the IoC container.
  164. *
  165. * <code>
  166. * // Get the "something" instance out of the IoC container
  167. * $something = IoC::container()->something;
  168. *
  169. * // Equivalent method of retrieving the instance using the resolve method
  170. * $something = IoC::container()->resolve('something');
  171. * </code>
  172. */
  173. public function __get($key)
  174. {
  175. if ($this->registered($key)) return $this->resolve($key);
  176. throw new \Exception("Attempting to resolve undefined class [$key].");
  177. }
  178. }