container.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php namespace Laravel;
  2. class IoC {
  3. /**
  4. * The active container instance.
  5. *
  6. * @var Container
  7. */
  8. public static $container;
  9. /**
  10. * Bootstrap the global IoC instance.
  11. *
  12. * @return void
  13. */
  14. public static function bootstrap()
  15. {
  16. static::$container = new Container(Config::$items['container']);
  17. }
  18. /**
  19. * Get the active container instance.
  20. *
  21. * @return Container
  22. */
  23. public static function container()
  24. {
  25. return static::$container;
  26. }
  27. /**
  28. * Resolve a core Laravel class from the container.
  29. *
  30. * <code>
  31. * // Resolve the "laravel.router" class from the container
  32. * $input = IoC::core('router');
  33. *
  34. * // Equivalent resolution using the "resolve" method
  35. * $input = IoC::resolve('laravel.router');
  36. *
  37. * // Pass an array of parameters to the resolver
  38. * $input = IoC::core('router', array('test'));
  39. * </code>
  40. *
  41. * @param string $name
  42. * @param array $parameters
  43. * @return mixed
  44. */
  45. public static function core($name, $parameters = array())
  46. {
  47. return static::$container->core($name, $parameters);
  48. }
  49. /**
  50. * Magic Method for calling methods on the active container instance.
  51. *
  52. * <code>
  53. * // Call the "resolve" method on the active container
  54. * $instance = IoC::resolve('laravel.routing.router');
  55. *
  56. * // Call the "instance" method on the active container
  57. * IoC::instance('mailer', new Mailer);
  58. * </code>
  59. */
  60. public static function __callStatic($method, $parameters)
  61. {
  62. return call_user_func_array(array(static::$container, $method), $parameters);
  63. }
  64. }
  65. class Container {
  66. /**
  67. * The resolved singleton instances.
  68. *
  69. * @var array
  70. */
  71. public $singletons = array();
  72. /**
  73. * The registered dependencies.
  74. *
  75. * @var array
  76. */
  77. protected $registry = array();
  78. /**
  79. * Create a new IoC container instance.
  80. *
  81. * @param array $registry
  82. * @return void
  83. */
  84. public function __construct($registry = array())
  85. {
  86. $this->registry = $registry;
  87. }
  88. /**
  89. * Register an object and its resolver.
  90. *
  91. * The IoC container instance is always passed to the resolver, allowing the
  92. * nested resolution of other objects from the container.
  93. *
  94. * <code>
  95. * // Register an object and its resolver
  96. * IoC::container()->register('mailer', function($c) {return new Mailer;});
  97. * </code>
  98. *
  99. * @param string $name
  100. * @param Closure $resolver
  101. * @return void
  102. */
  103. public function register($name, $resolver, $singleton = false)
  104. {
  105. $this->registry[$name] = array('resolver' => $resolver, 'singleton' => $singleton);
  106. }
  107. /**
  108. * Determine if an object has been registered in the container.
  109. *
  110. * @param string $name
  111. * @return bool
  112. */
  113. public function registered($name)
  114. {
  115. return array_key_exists($name, $this->registry);
  116. }
  117. /**
  118. * Register an object as a singleton.
  119. *
  120. * Singletons will only be instantiated the first time they are resolved.
  121. * On subsequent requests for the object, the original instance will be returned.
  122. *
  123. * @param string $name
  124. * @param Closure $resolver
  125. * @return void
  126. */
  127. public function singleton($name, $resolver)
  128. {
  129. $this->register($name, $resolver, true);
  130. }
  131. /**
  132. * Register an instance as a singleton.
  133. *
  134. * This method allows you to register an already existing object instance
  135. * with the container to be managed as a singleton instance.
  136. *
  137. * <code>
  138. * // Register an instance as a singleton in the container
  139. * IoC::container()->instance('mailer', new Mailer);
  140. * </code>
  141. *
  142. * @param string $name
  143. * @param mixed $instance
  144. * @return void
  145. */
  146. public function instance($name, $instance)
  147. {
  148. $this->singletons[$name] = $instance;
  149. }
  150. /**
  151. * Resolve a core Laravel class from the container.
  152. *
  153. * <code>
  154. * // Resolve the "laravel.router" class from the container
  155. * $input = IoC::container()->core('router');
  156. *
  157. * // Equivalent resolution using the "resolve" method
  158. * $input = IoC::container()->resolve('laravel.router');
  159. *
  160. * // Pass an array of parameters to the resolver
  161. * $input = IoC::container()->core('router', array('test'));
  162. * </code>
  163. *
  164. * @param string $name
  165. * @param array $parameters
  166. * @return mixed
  167. */
  168. public function core($name, $parameters = array())
  169. {
  170. return $this->resolve("laravel.{$name}", $parameters);
  171. }
  172. /**
  173. * Resolve an object instance from the container.
  174. *
  175. * <code>
  176. * // Get an instance of the "mailer" object registered in the container
  177. * $mailer = IoC::container()->resolve('mailer');
  178. *
  179. * // Pass an array of parameters to the resolver
  180. * $mailer = IoC::container()->resolve('mailer', array('test'));
  181. * </code>
  182. *
  183. * @param string $name
  184. * @param array $parameters
  185. * @return mixed
  186. */
  187. public function resolve($name, $parameters = array())
  188. {
  189. if (array_key_exists($name, $this->singletons)) return $this->singletons[$name];
  190. if ( ! $this->registered($name))
  191. {
  192. throw new \Exception("Error resolving [$name]. No resolver has been registered in the container.");
  193. }
  194. $object = call_user_func($this->registry[$name]['resolver'], $this, $parameters);
  195. if (isset($this->registry[$name]['singleton']) and $this->registry[$name]['singleton'])
  196. {
  197. return $this->singletons[$name] = $object;
  198. }
  199. return $object;
  200. }
  201. }