container.php 3.4 KB

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