container.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * @return Container
  13. */
  14. public static function container()
  15. {
  16. return static::$container;
  17. }
  18. /**
  19. * Magic Method for calling methods on the active container instance.
  20. *
  21. * <code>
  22. * // Call the "resolve" method on the active container
  23. * $instance = IoC::resolve('laravel.routing.router');
  24. *
  25. * // Call the "instance" method on the active container
  26. * IoC::instance('mailer', new Mailer);
  27. * </code>
  28. */
  29. public static function __callStatic($method, $parameters)
  30. {
  31. return call_user_func_array(array(static::$container, $method), $parameters);
  32. }
  33. }
  34. class Container {
  35. /**
  36. * The resolved singleton instances.
  37. *
  38. * @var array
  39. */
  40. public $singletons = array();
  41. /**
  42. * The registered dependencies.
  43. *
  44. * @var array
  45. */
  46. protected $registry = array();
  47. /**
  48. * Create a new IoC container instance.
  49. *
  50. * @param array $registry
  51. * @return void
  52. */
  53. public function __construct($registry = array())
  54. {
  55. $this->registry = $registry;
  56. }
  57. /**
  58. * Register an object and its resolver.
  59. *
  60. * The IoC container instance is always passed to the resolver, allowing the
  61. * nested resolution of other objects from the container.
  62. *
  63. * <code>
  64. * // Register an object and its resolver
  65. * IoC::container()->register('mailer', function($c) {return new Mailer;});
  66. * </code>
  67. *
  68. * @param string $name
  69. * @param Closure $resolver
  70. * @return void
  71. */
  72. public function register($name, $resolver, $singleton = false)
  73. {
  74. $this->registry[$name] = array('resolver' => $resolver, 'singleton' => $singleton);
  75. }
  76. /**
  77. * Determine if an object has been registered in the container.
  78. *
  79. * @param string $name
  80. * @return bool
  81. */
  82. public function registered($name)
  83. {
  84. return array_key_exists($name, $this->registry);
  85. }
  86. /**
  87. * Register an object as a singleton.
  88. *
  89. * Singletons will only be instantiated the first time they are resolved.
  90. * On subsequent requests for the object, the original instance will be returned.
  91. *
  92. * @param string $name
  93. * @param Closure $resolver
  94. * @return void
  95. */
  96. public function singleton($name, $resolver)
  97. {
  98. $this->register($name, $resolver, true);
  99. }
  100. /**
  101. * Register an instance as a singleton.
  102. *
  103. * This method allows you to register an already existing object instance
  104. * with the container to be managed as a singleton instance.
  105. *
  106. * <code>
  107. * // Register an instance as a singleton in the container
  108. * IoC::container()->instance('mailer', new Mailer);
  109. * </code>
  110. *
  111. * @param string $name
  112. * @param mixed $instance
  113. * @return void
  114. */
  115. public function instance($name, $instance)
  116. {
  117. $this->singletons[$name] = $instance;
  118. }
  119. /**
  120. * Resolve an object instance from the container.
  121. *
  122. * <code>
  123. * // Get an instance of the "mailer" object registered in the container
  124. * $mailer = IoC::container()->resolve('mailer');
  125. * </code>
  126. *
  127. * @param string $name
  128. * @return mixed
  129. */
  130. public function resolve($name)
  131. {
  132. if (array_key_exists($name, $this->singletons)) return $this->singletons[$name];
  133. if ( ! $this->registered($name))
  134. {
  135. throw new \Exception("Error resolving [$name]. No resolver has been registered in the container.");
  136. }
  137. $object = call_user_func($this->registry[$name]['resolver'], $this);
  138. return (isset($this->registry[$name]['singleton'])) ? $this->singletons[$name] = $object : $object;
  139. }
  140. }