ioc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php namespace Laravel; use Closure;
  2. class IoC {
  3. /**
  4. * The registered dependencies.
  5. *
  6. * @var array
  7. */
  8. public static $registry = array();
  9. /**
  10. * The resolved singleton instances.
  11. *
  12. * @var array
  13. */
  14. public static $singletons = array();
  15. /**
  16. * Register an object and its resolver.
  17. *
  18. * @param string $name
  19. * @param mixed $resolver
  20. * @param bool $singleton
  21. * @return void
  22. */
  23. public static function register($name, $resolver = null, $singleton = false)
  24. {
  25. if (is_null($resolver)) $resolver = $name;
  26. static::$registry[$name] = compact('resolver', 'singleton');
  27. }
  28. /**
  29. * Determine if an object has been registered in the container.
  30. *
  31. * @param string $name
  32. * @return bool
  33. */
  34. public static function registered($name)
  35. {
  36. return array_key_exists($name, static::$registry);
  37. }
  38. /**
  39. * Register an object as a singleton.
  40. *
  41. * Singletons will only be instantiated the first time they are resolved.
  42. *
  43. * @param string $name
  44. * @param Closure $resolver
  45. * @return void
  46. */
  47. public static function singleton($name, $resolver = null)
  48. {
  49. static::register($name, $resolver, true);
  50. }
  51. /**
  52. * Register an existing instance as a singleton.
  53. *
  54. * <code>
  55. * // Register an instance as a singleton in the container
  56. * IoC::instance('mailer', new Mailer);
  57. * </code>
  58. *
  59. * @param string $name
  60. * @param mixed $instance
  61. * @return void
  62. */
  63. public static function instance($name, $instance)
  64. {
  65. static::$singletons[$name] = $instance;
  66. }
  67. /**
  68. * Resolve a given type to an instance.
  69. *
  70. * <code>
  71. * // Get an instance of the "mailer" object registered in the container
  72. * $mailer = IoC::resolve('mailer');
  73. *
  74. * // Get an instance of the "mailer" object and pass parameters to the resolver
  75. * $mailer = IoC::resolve('mailer', array('test'));
  76. * </code>
  77. *
  78. * @param string $type
  79. * @return mixed
  80. */
  81. public static function resolve($type, $parameters = array())
  82. {
  83. // If an instance of the type is currently being managed as a singleton, we will
  84. // just return the existing instance instead of instantiating a fresh instance
  85. // so the developer can keep re-using the exact same object instance from us.
  86. if (isset(static::$singletons[$type]))
  87. {
  88. return static::$singletons[$type];
  89. }
  90. // If we don't have a registered resolver or concrete for the type, we'll just
  91. // assume the type is the concrete name and will attempt to resolve it as is
  92. // since the container should be able to resolve concretes automatically.
  93. if ( ! isset(static::$registry[$type]))
  94. {
  95. $concrete = $type;
  96. }
  97. else
  98. {
  99. $concrete = array_get(static::$registry[$type], 'resolver', $type);
  100. }
  101. // We're ready to instantiate an instance of the concrete type registered for
  102. // the binding. This will instantiate the type, as well as resolve any of
  103. // its nested dependencies recursively until they are each resolved.
  104. if ($concrete == $type or $concrete instanceof Closure)
  105. {
  106. $object = static::build($concrete, $parameters);
  107. }
  108. else
  109. {
  110. $object = static::resolve($concrete);
  111. }
  112. // If the requested type is registered as a singleton, we want to cache off
  113. // the instance in memory so we can return it later without creating an
  114. // entirely new instances of the object on each subsequent request.
  115. if (isset(static::$registry[$type]['singleton']))
  116. {
  117. static::$singletons[$type] = $object;
  118. }
  119. Event::fire('laravel.resolving', array($type, $object));
  120. return $object;
  121. }
  122. /**
  123. * Instantiate an instance of the given type.
  124. *
  125. * @param string $type
  126. * @param array $parameters
  127. * @return mixed
  128. */
  129. protected static function build($type, $parameters = array())
  130. {
  131. // If the concrete type is actually a Closure, we will just execute it and
  132. // hand back the results of the function, which allows functions to be
  133. // used as resolvers for more fine-tuned resolution of the objects.
  134. if ($type instanceof Closure)
  135. {
  136. return call_user_func_array($type, $parameters);
  137. }
  138. $reflector = new \ReflectionClass($type);
  139. // If the type is not instantiable, the developer is attempting to resolve
  140. // an abstract type such as an Interface of Abstract Class and there is
  141. // no binding registered for the abstraction so we need to bail out.
  142. if ( ! $reflector->isInstantiable())
  143. {
  144. throw new Exception("Resolution target [$type] is not instantiable.");
  145. }
  146. $constructor = $reflector->getConstructor();
  147. // If there is no constructor, that means there are no dependencies and
  148. // we can just resolve an instance of the object right away without
  149. // resolving any other types or dependencies from the container.
  150. if (is_null($constructor))
  151. {
  152. return new $type;
  153. }
  154. $dependencies = static::dependencies($constructor->getParameters());
  155. return $reflector->newInstanceArgs($dependencies);
  156. }
  157. /**
  158. * Resolve all of the dependencies from the ReflectionParameters.
  159. *
  160. * @param array $parameterrs
  161. * @return array
  162. */
  163. protected static function dependencies($parameters)
  164. {
  165. $dependencies = array();
  166. foreach ($parameters as $parameter)
  167. {
  168. $dependency = $parameter->getClass();
  169. // If the class is null, it means the dependency is a string or some other
  170. // primitive type, which we can not esolve since it is not a class and
  171. // we'll just bomb out with an error since we have nowhere to go.
  172. if (is_null($dependency))
  173. {
  174. throw new Exception("Unresolvable dependency resolving [$parameter].");
  175. }
  176. $dependencies[] = static::resolve($dependency->name);
  177. }
  178. return (array) $dependencies;
  179. }
  180. }