ioc.php 3.2 KB

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