container.php 4.3 KB

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