container.php 2.9 KB

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