container.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. public static function __callStatic($method, $parameters)
  22. {
  23. return call_user_func_array(array(static::$container, $method), $parameters);
  24. }
  25. }
  26. class Container {
  27. /**
  28. * The resolved singleton instances.
  29. *
  30. * @var array
  31. */
  32. public $singletons = array();
  33. /**
  34. * The registered dependencies.
  35. *
  36. * @var array
  37. */
  38. protected $registry = array();
  39. /**
  40. * Create a new IoC container instance.
  41. *
  42. * @param array $registry
  43. * @return void
  44. */
  45. public function __construct($registry = array())
  46. {
  47. $this->registry = $registry;
  48. }
  49. /**
  50. * Register an object and its resolver.
  51. *
  52. * @param string $name
  53. * @param Closure $resolver
  54. * @return void
  55. */
  56. public function register($name, $resolver, $singleton = false)
  57. {
  58. $this->registry[$name] = array('resolver' => $resolver, 'singleton' => $singleton);
  59. }
  60. /**
  61. * Determine if an object has been registered in the container.
  62. *
  63. * @param string $name
  64. * @return bool
  65. */
  66. public function registered($name)
  67. {
  68. return array_key_exists($name, $this->registry);
  69. }
  70. /**
  71. * Register an object as a singleton.
  72. *
  73. * Singletons will only be instantiated the first time they are resolved.
  74. * On subsequent requests for the object, the original instance will be returned.
  75. *
  76. * @param string $name
  77. * @param Closure $resolver
  78. * @return void
  79. */
  80. public function singleton($name, $resolver)
  81. {
  82. $this->register($name, $resolver, true);
  83. }
  84. /**
  85. * Register an instance as a singleton.
  86. *
  87. * This method allows you to register an already existing object instance
  88. * with the container to be managed as a singleton instance.
  89. *
  90. * @param string $name
  91. * @param mixed $instance
  92. * @return void
  93. */
  94. public function instance($name, $instance)
  95. {
  96. $this->singletons[$name] = $instance;
  97. }
  98. /**
  99. * Resolve an object.
  100. *
  101. * @param string $name
  102. * @return mixed
  103. */
  104. public function resolve($name)
  105. {
  106. if (array_key_exists($name, $this->singletons)) return $this->singletons[$name];
  107. if ( ! $this->registered($name))
  108. {
  109. throw new \Exception("Error resolving [$name]. No resolver has been registered in the container.");
  110. }
  111. $object = call_user_func($this->registry[$name]['resolver'], $this);
  112. return (isset($this->registry[$name]['singleton'])) ? $this->singletons[$name] = $object : $object;
  113. }
  114. /**
  115. * Magic Method for resolving classes out of the IoC container.
  116. */
  117. public function __get($key)
  118. {
  119. if ($this->registered($key)) return $this->resolve($key);
  120. throw new \Exception("Attempting to resolve undefined class [$key].");
  121. }
  122. }