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 (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/IServiceLocator.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/Object.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Service locator pattern implementation.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette
  34. 34:  */
  35. 35: class ServiceLocator extends Object implements IServiceLocator
  36. 36: {
  37. 37:     /** @var IServiceLocator */
  38. 38:     private $parent;
  39. 39:  
  40. 40:     /** @var array  storage for shared objects */
  41. 41:     private $registry array();
  42. 42:  
  43. 43:     /** @var array  storage for shared objects */
  44. 44:     private $factories array();
  45. 45:  
  46. 46:  
  47. 47:  
  48. 48:     /**
  49. 49:      * @param  IServiceLocator 
  50. 50:      */
  51. 51:     public function __construct(IServiceLocator $parent NULL)
  52. 52:     {
  53. 53:         $this->parent $parent;
  54. 54:     }
  55. 55:  
  56. 56:  
  57. 57:  
  58. 58:     /**
  59. 59:      * Adds the specified service to the service container.
  60. 60:      * @param  mixed  object, class name or service factory callback
  61. 61:      * @param  string optional service name (for factories is not optional)
  62. 62:      * @param  bool   promote to higher level?
  63. 63:      * @return void 
  64. 64:      * @throws InvalidArgumentException, AmbiguousServiceException
  65. 65:      */
  66. 66:     public function addService($service$name NULL$promote FALSE)
  67. 67:     {
  68. 68:         if (is_object($service)) {
  69. 69:             if ($name === NULL$name get_class($service);
  70. 70:  
  71. 71:         elseif (is_string($service)) {
  72. 72:             if ($name === NULL$name $service;
  73. 73:  
  74. 74:         elseif (is_callable($serviceTRUE)) {
  75. 75:             if (empty($name)) {
  76. 76:                 throw new InvalidArgumentException('When factory callback is given, service name must be specified.');
  77. 77:             }
  78. 78:  
  79. 79:         else {
  80. 80:             throw new InvalidArgumentException('Service must be name, object or factory callback.');
  81. 81:         }
  82. 82:  
  83. 83:         $lower strtolower($name);
  84. 84:         if (isset($this->registry[$lower])) {
  85. 85:             throw new AmbiguousServiceException("Service named '$name' has been already registered.");
  86. 86:         }
  87. 87:  
  88. 88:         if (is_object($service)) {
  89. 89:             $this->registry[$lower$service;
  90. 90:  
  91. 91:         else {
  92. 92:             $this->factories[$lower$service;
  93. 93:         }
  94. 94:  
  95. 95:         if ($promote && $this->parent !== NULL{
  96. 96:             $this->parent->addService($service$nameTRUE);
  97. 97:         }
  98. 98:     }
  99. 99:  
  100. 100:  
  101. 101:  
  102. 102:     /**
  103. 103:      * Removes the specified service type from the service container.
  104. 104:      * @param  bool   promote to higher level?
  105. 105:      * @return void 
  106. 106:      */
  107. 107:     public function removeService($name$promote TRUE)
  108. 108:     {
  109. 109:         if (!is_string($name|| $name === ''{
  110. 110:             throw new InvalidArgumentException("Service name must be a non-empty string, " gettype($name" given.");
  111. 111:         }
  112. 112:  
  113. 113:         $lower strtolower($name);
  114. 114:         unset($this->registry[$lower]$this->factories[$lower]);
  115. 115:  
  116. 116:         if ($promote && $this->parent !== NULL{
  117. 117:             $this->parent->removeService($nameTRUE);
  118. 118:         }
  119. 119:     }
  120. 120:  
  121. 121:  
  122. 122:  
  123. 123:     /**
  124. 124:      * Gets the service object of the specified type.
  125. 125:      * @param  string service name
  126. 126:      * @param  bool   throw exception if service doesn't exist?
  127. 127:      * @return mixed 
  128. 128:      */
  129. 129:     public function getService($name$need TRUE)
  130. 130:     {
  131. 131:         if (!is_string($name|| $name === ''{
  132. 132:             throw new InvalidArgumentException("Service name must be a non-empty string, " gettype($name" given.");
  133. 133:         }
  134. 134:  
  135. 135:         $lower strtolower($name);
  136. 136:  
  137. 137:         if (isset($this->registry[$lower])) {
  138. 138:             return $this->registry[$lower];
  139. 139:  
  140. 140:         elseif (isset($this->factories[$lower])) {
  141. 141:             $service $this->factories[$lower];
  142. 142:  
  143. 143:             if (is_string($service)) {
  144. 144:                 if (substr($service-2=== '()'{
  145. 145:                     // trick to pass callback as string
  146. 146:                     $service substr($service0-2);
  147. 147:  
  148. 148:                 else {
  149. 149:                     fixNamespace($service);
  150. 150:  
  151. 151:                     if (!class_exists($service)) {
  152. 152:                         throw new AmbiguousServiceException("Cannot instantiate service '$name', class '$service' not found.");
  153. 153:                     }
  154. 154:                     return $this->registry[$lowernew $service;
  155. 155:                 }
  156. 156:             }
  157. 157:  
  158. 158:             fixCallback($service);
  159. 159:             if (!is_callable($service)) {
  160. 160:                 $able is_callable($serviceTRUE$textual);
  161. 161:                 throw new AmbiguousServiceException("Cannot instantiate service '$name', handler '$textual' is not ($able 'callable.' 'valid PHP callback.'));
  162. 162:             }
  163. 163:  
  164. 164:             return $this->registry[$lowercall_user_func($service);
  165. 165:         }
  166. 166:  
  167. 167:         if ($this->parent !== NULL{
  168. 168:             return $this->parent->getService($name$need);
  169. 169:  
  170. 170:         elseif ($need{
  171. 171:             throw new InvalidStateException("Service '$name' not found.");
  172. 172:  
  173. 173:         else {
  174. 174:             return NULL;
  175. 175:         }
  176. 176:     }
  177. 177:  
  178. 178:  
  179. 179:  
  180. 180:     /**
  181. 181:      * Exists the service?
  182. 182:      * @param  string service name
  183. 183:      * @param  bool   must be created yet?
  184. 184:      * @return bool 
  185. 185:      */
  186. 186:     public function hasService($name$created FALSE)
  187. 187:     {
  188. 188:         if (!is_string($name|| $name === ''{
  189. 189:             throw new InvalidArgumentException("Service name must be a non-empty string, " gettype($name" given.");
  190. 190:         }
  191. 191:  
  192. 192:         $lower strtolower($name);
  193. 193:         return isset($this->registry[$lower]|| (!$created && isset($this->factories[$lower])) || ($this->parent !== NULL && $this->parent->hasService($name$created));
  194. 194:     }
  195. 195:  
  196. 196:  
  197. 197:  
  198. 198:     /**
  199. 199:      * Returns the parent container if any.
  200. 200:      * @return IServiceLocator|NULL
  201. 201:      */
  202. 202:     public function getParent()
  203. 203:     {
  204. 204:         return $this->parent;
  205. 205:     }
  206. 206:  
  207. 208:  
  208. 209:  
  209. 210:  
  210. 211: /**
  211. 212:  * Ambiguous service resolution exception.
  212. 213:  *
  213. 214:  * @author     David Grudl
  214. 215:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  215. 216:  * @package    Nette
  216. 217:  */
  217. 218: class AmbiguousServiceException extends Exception