Source for file ServiceLocator.php

Documentation is available at ServiceLocator.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Service locator pattern implementation.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette
  20. 20:  */
  21. 21: class ServiceLocator extends Object implements IServiceLocator
  22. 22: {
  23. 23:     /** @var IServiceLocator */
  24. 24:     private $parent;
  25. 25:  
  26. 26:     /** @var array  storage for shared objects */
  27. 27:     private $registry array();
  28. 28:  
  29. 29:     /** @var array  storage for service factories */
  30. 30:     private $factories array();
  31. 31:  
  32. 32:  
  33. 33:  
  34. 34:     /**
  35. 35:      * @param  IServiceLocator 
  36. 36:      */
  37. 37:     public function __construct(IServiceLocator $parent NULL)
  38. 38:     {
  39. 39:         $this->parent $parent;
  40. 40:     }
  41. 41:  
  42. 42:  
  43. 43:  
  44. 44:     /**
  45. 45:      * Adds the specified service to the service container.
  46. 46:      * @param  string service name
  47. 47:      * @param  mixed  object, class name or factory callback
  48. 48:      * @param  bool   is singleton?
  49. 49:      * @param  array  factory options
  50. 50:      * @return void 
  51. 51:      */
  52. 52:     public function addService($name$service$singleton TRUEarray $options NULL)
  53. 53:     {
  54. 54:         if (!is_string($name|| $name === ''{
  55. 55:             throw new InvalidArgumentException("Service name must be a non-empty string, " gettype($name" given.");
  56. 56:         }
  57. 57:  
  58. 58:         $lower strtolower($name);
  59. 59:         if (isset($this->registry[$lower])) // only for instantiated services?
  60. 60:             throw new AmbiguousServiceException("Service named '$name' has been already registered.");
  61. 61:         }
  62. 62:  
  63. 63:         if (is_object($service)) {
  64. 64:             if (!$singleton || $options{
  65. 65:                 throw new InvalidArgumentException("Service named '$name' is an instantiated object and must therefore be singleton without options.");
  66. 66:             }
  67. 67:             $this->registry[$lower$service;
  68. 68:  
  69. 69:         else {
  70. 70:             if (!$service{
  71. 71:                 throw new InvalidArgumentException("Service named '$name' is empty.");
  72. 72:             }
  73. 73:             $this->factories[$lowerarray($service$singleton$options);
  74. 74:         }
  75. 75:     }
  76. 76:  
  77. 77:  
  78. 78:  
  79. 79:     /**
  80. 80:      * Removes the specified service type from the service container.
  81. 81:      * @return void 
  82. 82:      */
  83. 83:     public function removeService($name)
  84. 84:     {
  85. 85:         if (!is_string($name|| $name === ''{
  86. 86:             throw new InvalidArgumentException("Service name must be a non-empty string, " gettype($name" given.");
  87. 87:         }
  88. 88:  
  89. 89:         $lower strtolower($name);
  90. 90:         unset($this->registry[$lower]$this->factories[$lower]);
  91. 91:     }
  92. 92:  
  93. 93:  
  94. 94:  
  95. 95:     /**
  96. 96:      * Gets the service object of the specified type.
  97. 97:      * @param  string service name
  98. 98:      * @param  array  options in case service is not singleton
  99. 99:      * @return mixed 
  100. 100:      */
  101. 101:     public function getService($namearray $options NULL)
  102. 102:     {
  103. 103:         if (!is_string($name|| $name === ''{
  104. 104:             throw new InvalidArgumentException("Service name must be a non-empty string, " gettype($name" given.");
  105. 105:         }
  106. 106:  
  107. 107:         $lower strtolower($name);
  108. 108:  
  109. 109:         if (isset($this->registry[$lower])) // instantiated singleton
  110. 110:             if ($options{
  111. 111:                 throw new InvalidArgumentException("Service named '$name' is singleton and therefore can not have options.");
  112. 112:             }
  113. 113:             return $this->registry[$lower];
  114. 114:  
  115. 115:         elseif (isset($this->factories[$lower])) {
  116. 116:             list($factory$singleton$defOptions$this->factories[$lower];
  117. 117:  
  118. 118:             if ($singleton && $options{
  119. 119:                 throw new InvalidArgumentException("Service named '$name' is singleton and therefore can not have options.");
  120. 120:  
  121. 121:             elseif ($defOptions{
  122. 122:                 $options $options $options $defOptions $defOptions;
  123. 123:             }
  124. 124:  
  125. 125:             if (is_string($factory&& strpos($factory':'=== FALSE// class name
  126. 126:                 Framework::fixNamespace($factory);
  127. 127:                 if (!class_exists($factory)) {
  128. 128:                     throw new AmbiguousServiceException("Cannot instantiate service '$name', class '$factory' not found.");
  129. 129:                 }
  130. 130:                 $service new $factory;
  131. 131:                 if ($options && method_exists($service'setOptions')) {
  132. 132:                     $service->setOptions($options)// TODO: better!
  133. 133:                 }
  134. 134:  
  135. 135:             else // factory callback
  136. 136:                 $factory callback($factory);
  137. 137:                 if (!$factory->isCallable()) {
  138. 138:                     throw new InvalidStateException("Cannot instantiate service '$name', handler '$factory' is not callable.");
  139. 139:                 }
  140. 140:                 $service $factory->invoke($options);
  141. 141:                 if (!is_object($service)) {
  142. 142:                     throw new AmbiguousServiceException("Cannot instantiate service '$name', value returned by '$factory' is not object.");
  143. 143:                 }
  144. 144:             }
  145. 145:  
  146. 146:             if ($singleton{
  147. 147:                 $this->registry[$lower$service;
  148. 148:                 unset($this->factories[$lower]);
  149. 149:             }
  150. 150:             return $service;
  151. 151:         }
  152. 152:  
  153. 153:         if ($this->parent !== NULL{
  154. 154:             return $this->parent->getService($name$options);
  155. 155:  
  156. 156:         else {
  157. 157:             throw new InvalidStateException("Service '$name' not found.");
  158. 158:         }
  159. 159:     }
  160. 160:  
  161. 161:  
  162. 162:  
  163. 163:     /**
  164. 164:      * Exists the service?
  165. 165:      * @param  string service name
  166. 166:      * @param  bool   must be created yet?
  167. 167:      * @return bool 
  168. 168:      */
  169. 169:     public function hasService($name$created FALSE)
  170. 170:     {
  171. 171:         if (!is_string($name|| $name === ''{
  172. 172:             throw new InvalidArgumentException("Service name must be a non-empty string, " gettype($name" given.");
  173. 173:         }
  174. 174:  
  175. 175:         $lower strtolower($name);
  176. 176:         return isset($this->registry[$lower]|| (!$created && isset($this->factories[$lower])) || ($this->parent !== NULL && $this->parent->hasService($name$created));
  177. 177:     }
  178. 178:  
  179. 179:  
  180. 180:  
  181. 181:     /**
  182. 182:      * Returns the parent container if any.
  183. 183:      * @return IServiceLocator|NULL
  184. 184:      */
  185. 185:     public function getParent()
  186. 186:     {
  187. 187:         return $this->parent;
  188. 188:     }
  189. 189:  
  190. 191:  
  191. 192:  
  192. 193:  
  193. 194: /**
  194. 195:  * Ambiguous service resolution exception.
  195. 196:  *
  196. 197:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  197. 198:  * @package    Nette
  198. 199:  */
  199. 200: class AmbiguousServiceException extends Exception