container.php 4.6 KB

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