ioc.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php namespace Laravel;
  2. class IoC {
  3. /**
  4. * The registered dependencies.
  5. *
  6. * @var array
  7. */
  8. protected static $registry = array();
  9. /**
  10. * The resolved singleton instances.
  11. *
  12. * @var array
  13. */
  14. protected static $singletons = array();
  15. /**
  16. * Bootstrap the application IoC container.
  17. *
  18. * This method is called automatically the first time the class is loaded.
  19. *
  20. * @param array $registry
  21. * @return void
  22. */
  23. public static function bootstrap($registry = array())
  24. {
  25. if (Config::load('container'))
  26. {
  27. static::$registry = Config::$items['container'];
  28. }
  29. }
  30. /**
  31. * Register an object and its resolver.
  32. *
  33. * The IoC container instance is always passed to the resolver, allowing the
  34. * nested resolution of other objects from the container.
  35. *
  36. * <code>
  37. * // Register an object and its resolver
  38. * IoC::register('mailer', function($c) {return new Mailer;});
  39. * </code>
  40. *
  41. * @param string $name
  42. * @param Closure $resolver
  43. * @return void
  44. */
  45. public static function register($name, $resolver, $singleton = false)
  46. {
  47. static::$registry[$name] = array('resolver' => $resolver, 'singleton' => $singleton);
  48. }
  49. /**
  50. * Determine if an object has been registered in the container.
  51. *
  52. * @param string $name
  53. * @return bool
  54. */
  55. public static function registered($name)
  56. {
  57. return array_key_exists($name, static::$registry);
  58. }
  59. /**
  60. * Register an object as a singleton.
  61. *
  62. * Singletons will only be instantiated the first time they are resolved.
  63. * On subsequent requests for the object, the original instance will be returned.
  64. *
  65. * @param string $name
  66. * @param Closure $resolver
  67. * @return void
  68. */
  69. public static function singleton($name, $resolver)
  70. {
  71. static::register($name, $resolver, true);
  72. }
  73. /**
  74. * Register an instance as a singleton.
  75. *
  76. * This method allows you to register an already existing object instance
  77. * with the container to be managed as a singleton instance.
  78. *
  79. * <code>
  80. * // Register an instance as a singleton in the container
  81. * IoC::instance('mailer', new Mailer);
  82. * </code>
  83. *
  84. * @param string $name
  85. * @param mixed $instance
  86. * @return void
  87. */
  88. public static function instance($name, $instance)
  89. {
  90. static::$singletons[$name] = $instance;
  91. }
  92. /**
  93. * Resolve a core Laravel class from the container.
  94. *
  95. * <code>
  96. * // Resolve the "laravel.router" class from the container
  97. * $input = IoC::core('router');
  98. *
  99. * // Equivalent resolution using the "resolve" method
  100. * $input = IoC::resolve('laravel.router');
  101. *
  102. * // Pass an array of parameters to the resolver
  103. * $input = IoC::core('router', array('test'));
  104. * </code>
  105. *
  106. * @param string $name
  107. * @param array $parameters
  108. * @return mixed
  109. */
  110. public static function core($name, $parameters = array())
  111. {
  112. return static::resolve("laravel.{$name}", $parameters);
  113. }
  114. /**
  115. * Resolve an object instance from the container.
  116. *
  117. * <code>
  118. * // Get an instance of the "mailer" object registered in the container
  119. * $mailer = IoC::resolve('mailer');
  120. *
  121. * // Pass an array of parameters to the resolver
  122. * $mailer = IoC::resolve('mailer', array('test'));
  123. * </code>
  124. *
  125. * @param string $name
  126. * @param array $parameters
  127. * @return mixed
  128. */
  129. public static function resolve($name, $parameters = array())
  130. {
  131. if (array_key_exists($name, static::$singletons))
  132. {
  133. return static::$singletons[$name];
  134. }
  135. if ( ! static::registered($name))
  136. {
  137. throw new \OutOfBoundsException("Error resolving [$name]. No resolver has been registered.");
  138. }
  139. $object = call_user_func(static::$registry[$name]['resolver'], $parameters);
  140. if (isset(static::$registry[$name]['singleton']) and static::$registry[$name]['singleton'])
  141. {
  142. return static::$singletons[$name] = $object;
  143. }
  144. return $object;
  145. }
  146. }
  147. /**
  148. * We only bootstrap the IoC container once the class has been
  149. * loaded since there isn't any reason to load the container
  150. * configuration until the class is first requested.
  151. */
  152. IoC::bootstrap();