ioc.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 Closure $resolver
  20. * @return void
  21. */
  22. public static function register($name, Closure $resolver, $singleton = false)
  23. {
  24. static::$registry[$name] = compact('resolver', 'singleton');
  25. }
  26. /**
  27. * Determine if an object has been registered in the container.
  28. *
  29. * @param string $name
  30. * @return bool
  31. */
  32. public static function registered($name)
  33. {
  34. return array_key_exists($name, static::$registry);
  35. }
  36. /**
  37. * Register an object as a singleton.
  38. *
  39. * Singletons will only be instantiated the first time they are resolved.
  40. *
  41. * @param string $name
  42. * @param Closure $resolver
  43. * @return void
  44. */
  45. public static function singleton($name, $resolver)
  46. {
  47. static::register($name, $resolver, true);
  48. }
  49. /**
  50. * Register an existing instance as a singleton.
  51. *
  52. * <code>
  53. * // Register an instance as a singleton in the container
  54. * IoC::instance('mailer', new Mailer);
  55. * </code>
  56. *
  57. * @param string $name
  58. * @param mixed $instance
  59. * @return void
  60. */
  61. public static function instance($name, $instance)
  62. {
  63. static::$singletons[$name] = $instance;
  64. }
  65. /**
  66. * Register a controller with the IoC container.
  67. *
  68. * @param string $name
  69. * @param Closure $resolver
  70. * @return void
  71. */
  72. public static function controller($name, $resolver)
  73. {
  74. static::register("controller: {$name}", $resolver);
  75. }
  76. /**
  77. * Resolve a core Laravel class from the container.
  78. *
  79. * <code>
  80. * // Resolve the "laravel.router" class from the container
  81. * $input = IoC::core('router');
  82. *
  83. * // Equivalent resolution of the router using the "resolve" method
  84. * $input = IoC::resolve('laravel.router');
  85. * </code>
  86. *
  87. * @param string $name
  88. * @param array $parameters
  89. * @return mixed
  90. */
  91. public static function core($name, $parameters = array())
  92. {
  93. return static::resolve("laravel.{$name}", $parameters);
  94. }
  95. /**
  96. * Resolve an object instance from the container.
  97. *
  98. * <code>
  99. * // Get an instance of the "mailer" object registered in the container
  100. * $mailer = IoC::resolve('mailer');
  101. *
  102. * // Get an instance of the "mailer" object and pass parameters to the resolver
  103. * $mailer = IoC::resolve('mailer', array('test'));
  104. * </code>
  105. *
  106. * @param string $name
  107. * @param array $parameters
  108. * @return mixed
  109. */
  110. public static function resolve($name, $parameters = array())
  111. {
  112. if (array_key_exists($name, static::$singletons))
  113. {
  114. return static::$singletons[$name];
  115. }
  116. $object = call_user_func(static::$registry[$name]['resolver'], $parameters);
  117. // If the resolver is registering as a singleton resolver, we will cache
  118. // the instance of the object in the container so we can resolve it next
  119. // time without having to instantiate a new instance of the object.
  120. //
  121. // This allows the developer to reuse objects that do not need to be
  122. // instantiated each time they are needed, such as a SwiftMailer or
  123. // Twig object that can be shared.
  124. if (isset(static::$registry[$name]['singleton']))
  125. {
  126. return static::$singletons[$name] = $object;
  127. }
  128. return $object;
  129. }
  130. }