@@ -15,373 +15,373 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_DependencyContainer |
| 17 | 17 | { |
| 18 | - /** Constant for literal value types */ |
|
| 19 | - const TYPE_VALUE = 0x00001; |
|
| 20 | - |
|
| 21 | - /** Constant for new instance types */ |
|
| 22 | - const TYPE_INSTANCE = 0x00010; |
|
| 23 | - |
|
| 24 | - /** Constant for shared instance types */ |
|
| 25 | - const TYPE_SHARED = 0x00100; |
|
| 26 | - |
|
| 27 | - /** Constant for aliases */ |
|
| 28 | - const TYPE_ALIAS = 0x01000; |
|
| 29 | - |
|
| 30 | - /** Constant for arrays */ |
|
| 31 | - const TYPE_ARRAY = 0x10000; |
|
| 32 | - |
|
| 33 | - /** Singleton instance */ |
|
| 34 | - private static $instance = null; |
|
| 35 | - |
|
| 36 | - /** The data container */ |
|
| 37 | - private $store = []; |
|
| 38 | - |
|
| 39 | - /** The current endpoint in the data container */ |
|
| 40 | - private $endPoint; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Constructor should not be used. |
|
| 44 | - * |
|
| 45 | - * Use {@link getInstance()} instead. |
|
| 46 | - */ |
|
| 47 | - public function __construct() |
|
| 48 | - { |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Returns a singleton of the DependencyContainer. |
|
| 53 | - * |
|
| 54 | - * @return self |
|
| 55 | - */ |
|
| 56 | - public static function getInstance() |
|
| 57 | - { |
|
| 58 | - if (!isset(self::$instance)) { |
|
| 59 | - self::$instance = new self(); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - return self::$instance; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * List the names of all items stored in the Container. |
|
| 67 | - * |
|
| 68 | - * @return array |
|
| 69 | - */ |
|
| 70 | - public function listItems() |
|
| 71 | - { |
|
| 72 | - return array_keys($this->store); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Test if an item is registered in this container with the given name. |
|
| 77 | - * |
|
| 78 | - * @see register() |
|
| 79 | - * |
|
| 80 | - * @param string $itemName |
|
| 81 | - * |
|
| 82 | - * @return bool |
|
| 83 | - */ |
|
| 84 | - public function has($itemName) |
|
| 85 | - { |
|
| 86 | - return \array_key_exists($itemName, $this->store) |
|
| 87 | - && isset($this->store[$itemName]['lookupType']); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Lookup the item with the given $itemName. |
|
| 92 | - * |
|
| 93 | - * @see register() |
|
| 94 | - * |
|
| 95 | - * @param string $itemName |
|
| 96 | - * |
|
| 97 | - * @return mixed |
|
| 98 | - * |
|
| 99 | - * @throws Swift_DependencyException If the dependency is not found |
|
| 100 | - */ |
|
| 101 | - public function lookup($itemName) |
|
| 102 | - { |
|
| 103 | - if (!$this->has($itemName)) { |
|
| 104 | - throw new Swift_DependencyException('Cannot lookup dependency "'.$itemName.'" since it is not registered.'); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - switch ($this->store[$itemName]['lookupType']) { |
|
| 108 | - case self::TYPE_ALIAS: |
|
| 109 | - return $this->createAlias($itemName); |
|
| 110 | - case self::TYPE_VALUE: |
|
| 111 | - return $this->getValue($itemName); |
|
| 112 | - case self::TYPE_INSTANCE: |
|
| 113 | - return $this->createNewInstance($itemName); |
|
| 114 | - case self::TYPE_SHARED: |
|
| 115 | - return $this->createSharedInstance($itemName); |
|
| 116 | - case self::TYPE_ARRAY: |
|
| 117 | - return $this->createDependenciesFor($itemName); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Create an array of arguments passed to the constructor of $itemName. |
|
| 123 | - * |
|
| 124 | - * @param string $itemName |
|
| 125 | - * |
|
| 126 | - * @return array |
|
| 127 | - */ |
|
| 128 | - public function createDependenciesFor($itemName) |
|
| 129 | - { |
|
| 130 | - $args = []; |
|
| 131 | - if (isset($this->store[$itemName]['args'])) { |
|
| 132 | - $args = $this->resolveArgs($this->store[$itemName]['args']); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - return $args; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Register a new dependency with $itemName. |
|
| 140 | - * |
|
| 141 | - * This method returns the current DependencyContainer instance because it |
|
| 142 | - * requires the use of the fluid interface to set the specific details for the |
|
| 143 | - * dependency. |
|
| 144 | - * |
|
| 145 | - * @see asNewInstanceOf(), asSharedInstanceOf(), asValue() |
|
| 146 | - * |
|
| 147 | - * @param string $itemName |
|
| 148 | - * |
|
| 149 | - * @return $this |
|
| 150 | - */ |
|
| 151 | - public function register($itemName) |
|
| 152 | - { |
|
| 153 | - $this->store[$itemName] = []; |
|
| 154 | - $this->endPoint = &$this->store[$itemName]; |
|
| 155 | - |
|
| 156 | - return $this; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Specify the previously registered item as a literal value. |
|
| 161 | - * |
|
| 162 | - * {@link register()} must be called before this will work. |
|
| 163 | - * |
|
| 164 | - * @param mixed $value |
|
| 165 | - * |
|
| 166 | - * @return $this |
|
| 167 | - */ |
|
| 168 | - public function asValue($value) |
|
| 169 | - { |
|
| 170 | - $endPoint = &$this->getEndPoint(); |
|
| 171 | - $endPoint['lookupType'] = self::TYPE_VALUE; |
|
| 172 | - $endPoint['value'] = $value; |
|
| 173 | - |
|
| 174 | - return $this; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Specify the previously registered item as an alias of another item. |
|
| 179 | - * |
|
| 180 | - * @param string $lookup |
|
| 181 | - * |
|
| 182 | - * @return $this |
|
| 183 | - */ |
|
| 184 | - public function asAliasOf($lookup) |
|
| 185 | - { |
|
| 186 | - $endPoint = &$this->getEndPoint(); |
|
| 187 | - $endPoint['lookupType'] = self::TYPE_ALIAS; |
|
| 188 | - $endPoint['ref'] = $lookup; |
|
| 189 | - |
|
| 190 | - return $this; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Specify the previously registered item as a new instance of $className. |
|
| 195 | - * |
|
| 196 | - * {@link register()} must be called before this will work. |
|
| 197 | - * Any arguments can be set with {@link withDependencies()}, |
|
| 198 | - * {@link addConstructorValue()} or {@link addConstructorLookup()}. |
|
| 199 | - * |
|
| 200 | - * @see withDependencies(), addConstructorValue(), addConstructorLookup() |
|
| 201 | - * |
|
| 202 | - * @param string $className |
|
| 203 | - * |
|
| 204 | - * @return $this |
|
| 205 | - */ |
|
| 206 | - public function asNewInstanceOf($className) |
|
| 207 | - { |
|
| 208 | - $endPoint = &$this->getEndPoint(); |
|
| 209 | - $endPoint['lookupType'] = self::TYPE_INSTANCE; |
|
| 210 | - $endPoint['className'] = $className; |
|
| 211 | - |
|
| 212 | - return $this; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Specify the previously registered item as a shared instance of $className. |
|
| 217 | - * |
|
| 218 | - * {@link register()} must be called before this will work. |
|
| 219 | - * |
|
| 220 | - * @param string $className |
|
| 221 | - * |
|
| 222 | - * @return $this |
|
| 223 | - */ |
|
| 224 | - public function asSharedInstanceOf($className) |
|
| 225 | - { |
|
| 226 | - $endPoint = &$this->getEndPoint(); |
|
| 227 | - $endPoint['lookupType'] = self::TYPE_SHARED; |
|
| 228 | - $endPoint['className'] = $className; |
|
| 229 | - |
|
| 230 | - return $this; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Specify the previously registered item as array of dependencies. |
|
| 235 | - * |
|
| 236 | - * {@link register()} must be called before this will work. |
|
| 237 | - * |
|
| 238 | - * @return $this |
|
| 239 | - */ |
|
| 240 | - public function asArray() |
|
| 241 | - { |
|
| 242 | - $endPoint = &$this->getEndPoint(); |
|
| 243 | - $endPoint['lookupType'] = self::TYPE_ARRAY; |
|
| 244 | - |
|
| 245 | - return $this; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Specify a list of injected dependencies for the previously registered item. |
|
| 250 | - * |
|
| 251 | - * This method takes an array of lookup names. |
|
| 252 | - * |
|
| 253 | - * @see addConstructorValue(), addConstructorLookup() |
|
| 254 | - * |
|
| 255 | - * @return $this |
|
| 256 | - */ |
|
| 257 | - public function withDependencies(array $lookups) |
|
| 258 | - { |
|
| 259 | - $endPoint = &$this->getEndPoint(); |
|
| 260 | - $endPoint['args'] = []; |
|
| 261 | - foreach ($lookups as $lookup) { |
|
| 262 | - $this->addConstructorLookup($lookup); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - return $this; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Specify a literal (non looked up) value for the constructor of the |
|
| 270 | - * previously registered item. |
|
| 271 | - * |
|
| 272 | - * @see withDependencies(), addConstructorLookup() |
|
| 273 | - * |
|
| 274 | - * @param mixed $value |
|
| 275 | - * |
|
| 276 | - * @return $this |
|
| 277 | - */ |
|
| 278 | - public function addConstructorValue($value) |
|
| 279 | - { |
|
| 280 | - $endPoint = &$this->getEndPoint(); |
|
| 281 | - if (!isset($endPoint['args'])) { |
|
| 282 | - $endPoint['args'] = []; |
|
| 283 | - } |
|
| 284 | - $endPoint['args'][] = ['type' => 'value', 'item' => $value]; |
|
| 285 | - |
|
| 286 | - return $this; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Specify a dependency lookup for the constructor of the previously |
|
| 291 | - * registered item. |
|
| 292 | - * |
|
| 293 | - * @see withDependencies(), addConstructorValue() |
|
| 294 | - * |
|
| 295 | - * @param string $lookup |
|
| 296 | - * |
|
| 297 | - * @return $this |
|
| 298 | - */ |
|
| 299 | - public function addConstructorLookup($lookup) |
|
| 300 | - { |
|
| 301 | - $endPoint = &$this->getEndPoint(); |
|
| 302 | - if (!isset($this->endPoint['args'])) { |
|
| 303 | - $endPoint['args'] = []; |
|
| 304 | - } |
|
| 305 | - $endPoint['args'][] = ['type' => 'lookup', 'item' => $lookup]; |
|
| 306 | - |
|
| 307 | - return $this; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** Get the literal value with $itemName */ |
|
| 311 | - private function getValue($itemName) |
|
| 312 | - { |
|
| 313 | - return $this->store[$itemName]['value']; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** Resolve an alias to another item */ |
|
| 317 | - private function createAlias($itemName) |
|
| 318 | - { |
|
| 319 | - return $this->lookup($this->store[$itemName]['ref']); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** Create a fresh instance of $itemName */ |
|
| 323 | - private function createNewInstance($itemName) |
|
| 324 | - { |
|
| 325 | - $reflector = new ReflectionClass($this->store[$itemName]['className']); |
|
| 326 | - if ($reflector->getConstructor()) { |
|
| 327 | - return $reflector->newInstanceArgs( |
|
| 328 | - $this->createDependenciesFor($itemName) |
|
| 329 | - ); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - return $reflector->newInstance(); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** Create and register a shared instance of $itemName */ |
|
| 336 | - private function createSharedInstance($itemName) |
|
| 337 | - { |
|
| 338 | - if (!isset($this->store[$itemName]['instance'])) { |
|
| 339 | - $this->store[$itemName]['instance'] = $this->createNewInstance($itemName); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - return $this->store[$itemName]['instance']; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** Get the current endpoint in the store */ |
|
| 346 | - private function &getEndPoint() |
|
| 347 | - { |
|
| 348 | - if (!isset($this->endPoint)) { |
|
| 349 | - throw new BadMethodCallException('Component must first be registered by calling register()'); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - return $this->endPoint; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** Get an argument list with dependencies resolved */ |
|
| 356 | - private function resolveArgs(array $args) |
|
| 357 | - { |
|
| 358 | - $resolved = []; |
|
| 359 | - foreach ($args as $argDefinition) { |
|
| 360 | - switch ($argDefinition['type']) { |
|
| 361 | - case 'lookup': |
|
| 362 | - $resolved[] = $this->lookupRecursive($argDefinition['item']); |
|
| 363 | - break; |
|
| 364 | - case 'value': |
|
| 365 | - $resolved[] = $argDefinition['item']; |
|
| 366 | - break; |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - return $resolved; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** Resolve a single dependency with an collections */ |
|
| 374 | - private function lookupRecursive($item) |
|
| 375 | - { |
|
| 376 | - if (\is_array($item)) { |
|
| 377 | - $collection = []; |
|
| 378 | - foreach ($item as $k => $v) { |
|
| 379 | - $collection[$k] = $this->lookupRecursive($v); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - return $collection; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - return $this->lookup($item); |
|
| 386 | - } |
|
| 18 | + /** Constant for literal value types */ |
|
| 19 | + const TYPE_VALUE = 0x00001; |
|
| 20 | + |
|
| 21 | + /** Constant for new instance types */ |
|
| 22 | + const TYPE_INSTANCE = 0x00010; |
|
| 23 | + |
|
| 24 | + /** Constant for shared instance types */ |
|
| 25 | + const TYPE_SHARED = 0x00100; |
|
| 26 | + |
|
| 27 | + /** Constant for aliases */ |
|
| 28 | + const TYPE_ALIAS = 0x01000; |
|
| 29 | + |
|
| 30 | + /** Constant for arrays */ |
|
| 31 | + const TYPE_ARRAY = 0x10000; |
|
| 32 | + |
|
| 33 | + /** Singleton instance */ |
|
| 34 | + private static $instance = null; |
|
| 35 | + |
|
| 36 | + /** The data container */ |
|
| 37 | + private $store = []; |
|
| 38 | + |
|
| 39 | + /** The current endpoint in the data container */ |
|
| 40 | + private $endPoint; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Constructor should not be used. |
|
| 44 | + * |
|
| 45 | + * Use {@link getInstance()} instead. |
|
| 46 | + */ |
|
| 47 | + public function __construct() |
|
| 48 | + { |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Returns a singleton of the DependencyContainer. |
|
| 53 | + * |
|
| 54 | + * @return self |
|
| 55 | + */ |
|
| 56 | + public static function getInstance() |
|
| 57 | + { |
|
| 58 | + if (!isset(self::$instance)) { |
|
| 59 | + self::$instance = new self(); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + return self::$instance; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * List the names of all items stored in the Container. |
|
| 67 | + * |
|
| 68 | + * @return array |
|
| 69 | + */ |
|
| 70 | + public function listItems() |
|
| 71 | + { |
|
| 72 | + return array_keys($this->store); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Test if an item is registered in this container with the given name. |
|
| 77 | + * |
|
| 78 | + * @see register() |
|
| 79 | + * |
|
| 80 | + * @param string $itemName |
|
| 81 | + * |
|
| 82 | + * @return bool |
|
| 83 | + */ |
|
| 84 | + public function has($itemName) |
|
| 85 | + { |
|
| 86 | + return \array_key_exists($itemName, $this->store) |
|
| 87 | + && isset($this->store[$itemName]['lookupType']); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Lookup the item with the given $itemName. |
|
| 92 | + * |
|
| 93 | + * @see register() |
|
| 94 | + * |
|
| 95 | + * @param string $itemName |
|
| 96 | + * |
|
| 97 | + * @return mixed |
|
| 98 | + * |
|
| 99 | + * @throws Swift_DependencyException If the dependency is not found |
|
| 100 | + */ |
|
| 101 | + public function lookup($itemName) |
|
| 102 | + { |
|
| 103 | + if (!$this->has($itemName)) { |
|
| 104 | + throw new Swift_DependencyException('Cannot lookup dependency "'.$itemName.'" since it is not registered.'); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + switch ($this->store[$itemName]['lookupType']) { |
|
| 108 | + case self::TYPE_ALIAS: |
|
| 109 | + return $this->createAlias($itemName); |
|
| 110 | + case self::TYPE_VALUE: |
|
| 111 | + return $this->getValue($itemName); |
|
| 112 | + case self::TYPE_INSTANCE: |
|
| 113 | + return $this->createNewInstance($itemName); |
|
| 114 | + case self::TYPE_SHARED: |
|
| 115 | + return $this->createSharedInstance($itemName); |
|
| 116 | + case self::TYPE_ARRAY: |
|
| 117 | + return $this->createDependenciesFor($itemName); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Create an array of arguments passed to the constructor of $itemName. |
|
| 123 | + * |
|
| 124 | + * @param string $itemName |
|
| 125 | + * |
|
| 126 | + * @return array |
|
| 127 | + */ |
|
| 128 | + public function createDependenciesFor($itemName) |
|
| 129 | + { |
|
| 130 | + $args = []; |
|
| 131 | + if (isset($this->store[$itemName]['args'])) { |
|
| 132 | + $args = $this->resolveArgs($this->store[$itemName]['args']); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + return $args; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Register a new dependency with $itemName. |
|
| 140 | + * |
|
| 141 | + * This method returns the current DependencyContainer instance because it |
|
| 142 | + * requires the use of the fluid interface to set the specific details for the |
|
| 143 | + * dependency. |
|
| 144 | + * |
|
| 145 | + * @see asNewInstanceOf(), asSharedInstanceOf(), asValue() |
|
| 146 | + * |
|
| 147 | + * @param string $itemName |
|
| 148 | + * |
|
| 149 | + * @return $this |
|
| 150 | + */ |
|
| 151 | + public function register($itemName) |
|
| 152 | + { |
|
| 153 | + $this->store[$itemName] = []; |
|
| 154 | + $this->endPoint = &$this->store[$itemName]; |
|
| 155 | + |
|
| 156 | + return $this; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Specify the previously registered item as a literal value. |
|
| 161 | + * |
|
| 162 | + * {@link register()} must be called before this will work. |
|
| 163 | + * |
|
| 164 | + * @param mixed $value |
|
| 165 | + * |
|
| 166 | + * @return $this |
|
| 167 | + */ |
|
| 168 | + public function asValue($value) |
|
| 169 | + { |
|
| 170 | + $endPoint = &$this->getEndPoint(); |
|
| 171 | + $endPoint['lookupType'] = self::TYPE_VALUE; |
|
| 172 | + $endPoint['value'] = $value; |
|
| 173 | + |
|
| 174 | + return $this; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Specify the previously registered item as an alias of another item. |
|
| 179 | + * |
|
| 180 | + * @param string $lookup |
|
| 181 | + * |
|
| 182 | + * @return $this |
|
| 183 | + */ |
|
| 184 | + public function asAliasOf($lookup) |
|
| 185 | + { |
|
| 186 | + $endPoint = &$this->getEndPoint(); |
|
| 187 | + $endPoint['lookupType'] = self::TYPE_ALIAS; |
|
| 188 | + $endPoint['ref'] = $lookup; |
|
| 189 | + |
|
| 190 | + return $this; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Specify the previously registered item as a new instance of $className. |
|
| 195 | + * |
|
| 196 | + * {@link register()} must be called before this will work. |
|
| 197 | + * Any arguments can be set with {@link withDependencies()}, |
|
| 198 | + * {@link addConstructorValue()} or {@link addConstructorLookup()}. |
|
| 199 | + * |
|
| 200 | + * @see withDependencies(), addConstructorValue(), addConstructorLookup() |
|
| 201 | + * |
|
| 202 | + * @param string $className |
|
| 203 | + * |
|
| 204 | + * @return $this |
|
| 205 | + */ |
|
| 206 | + public function asNewInstanceOf($className) |
|
| 207 | + { |
|
| 208 | + $endPoint = &$this->getEndPoint(); |
|
| 209 | + $endPoint['lookupType'] = self::TYPE_INSTANCE; |
|
| 210 | + $endPoint['className'] = $className; |
|
| 211 | + |
|
| 212 | + return $this; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Specify the previously registered item as a shared instance of $className. |
|
| 217 | + * |
|
| 218 | + * {@link register()} must be called before this will work. |
|
| 219 | + * |
|
| 220 | + * @param string $className |
|
| 221 | + * |
|
| 222 | + * @return $this |
|
| 223 | + */ |
|
| 224 | + public function asSharedInstanceOf($className) |
|
| 225 | + { |
|
| 226 | + $endPoint = &$this->getEndPoint(); |
|
| 227 | + $endPoint['lookupType'] = self::TYPE_SHARED; |
|
| 228 | + $endPoint['className'] = $className; |
|
| 229 | + |
|
| 230 | + return $this; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Specify the previously registered item as array of dependencies. |
|
| 235 | + * |
|
| 236 | + * {@link register()} must be called before this will work. |
|
| 237 | + * |
|
| 238 | + * @return $this |
|
| 239 | + */ |
|
| 240 | + public function asArray() |
|
| 241 | + { |
|
| 242 | + $endPoint = &$this->getEndPoint(); |
|
| 243 | + $endPoint['lookupType'] = self::TYPE_ARRAY; |
|
| 244 | + |
|
| 245 | + return $this; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Specify a list of injected dependencies for the previously registered item. |
|
| 250 | + * |
|
| 251 | + * This method takes an array of lookup names. |
|
| 252 | + * |
|
| 253 | + * @see addConstructorValue(), addConstructorLookup() |
|
| 254 | + * |
|
| 255 | + * @return $this |
|
| 256 | + */ |
|
| 257 | + public function withDependencies(array $lookups) |
|
| 258 | + { |
|
| 259 | + $endPoint = &$this->getEndPoint(); |
|
| 260 | + $endPoint['args'] = []; |
|
| 261 | + foreach ($lookups as $lookup) { |
|
| 262 | + $this->addConstructorLookup($lookup); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + return $this; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Specify a literal (non looked up) value for the constructor of the |
|
| 270 | + * previously registered item. |
|
| 271 | + * |
|
| 272 | + * @see withDependencies(), addConstructorLookup() |
|
| 273 | + * |
|
| 274 | + * @param mixed $value |
|
| 275 | + * |
|
| 276 | + * @return $this |
|
| 277 | + */ |
|
| 278 | + public function addConstructorValue($value) |
|
| 279 | + { |
|
| 280 | + $endPoint = &$this->getEndPoint(); |
|
| 281 | + if (!isset($endPoint['args'])) { |
|
| 282 | + $endPoint['args'] = []; |
|
| 283 | + } |
|
| 284 | + $endPoint['args'][] = ['type' => 'value', 'item' => $value]; |
|
| 285 | + |
|
| 286 | + return $this; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Specify a dependency lookup for the constructor of the previously |
|
| 291 | + * registered item. |
|
| 292 | + * |
|
| 293 | + * @see withDependencies(), addConstructorValue() |
|
| 294 | + * |
|
| 295 | + * @param string $lookup |
|
| 296 | + * |
|
| 297 | + * @return $this |
|
| 298 | + */ |
|
| 299 | + public function addConstructorLookup($lookup) |
|
| 300 | + { |
|
| 301 | + $endPoint = &$this->getEndPoint(); |
|
| 302 | + if (!isset($this->endPoint['args'])) { |
|
| 303 | + $endPoint['args'] = []; |
|
| 304 | + } |
|
| 305 | + $endPoint['args'][] = ['type' => 'lookup', 'item' => $lookup]; |
|
| 306 | + |
|
| 307 | + return $this; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** Get the literal value with $itemName */ |
|
| 311 | + private function getValue($itemName) |
|
| 312 | + { |
|
| 313 | + return $this->store[$itemName]['value']; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** Resolve an alias to another item */ |
|
| 317 | + private function createAlias($itemName) |
|
| 318 | + { |
|
| 319 | + return $this->lookup($this->store[$itemName]['ref']); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** Create a fresh instance of $itemName */ |
|
| 323 | + private function createNewInstance($itemName) |
|
| 324 | + { |
|
| 325 | + $reflector = new ReflectionClass($this->store[$itemName]['className']); |
|
| 326 | + if ($reflector->getConstructor()) { |
|
| 327 | + return $reflector->newInstanceArgs( |
|
| 328 | + $this->createDependenciesFor($itemName) |
|
| 329 | + ); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + return $reflector->newInstance(); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** Create and register a shared instance of $itemName */ |
|
| 336 | + private function createSharedInstance($itemName) |
|
| 337 | + { |
|
| 338 | + if (!isset($this->store[$itemName]['instance'])) { |
|
| 339 | + $this->store[$itemName]['instance'] = $this->createNewInstance($itemName); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + return $this->store[$itemName]['instance']; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** Get the current endpoint in the store */ |
|
| 346 | + private function &getEndPoint() |
|
| 347 | + { |
|
| 348 | + if (!isset($this->endPoint)) { |
|
| 349 | + throw new BadMethodCallException('Component must first be registered by calling register()'); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + return $this->endPoint; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** Get an argument list with dependencies resolved */ |
|
| 356 | + private function resolveArgs(array $args) |
|
| 357 | + { |
|
| 358 | + $resolved = []; |
|
| 359 | + foreach ($args as $argDefinition) { |
|
| 360 | + switch ($argDefinition['type']) { |
|
| 361 | + case 'lookup': |
|
| 362 | + $resolved[] = $this->lookupRecursive($argDefinition['item']); |
|
| 363 | + break; |
|
| 364 | + case 'value': |
|
| 365 | + $resolved[] = $argDefinition['item']; |
|
| 366 | + break; |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + return $resolved; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** Resolve a single dependency with an collections */ |
|
| 374 | + private function lookupRecursive($item) |
|
| 375 | + { |
|
| 376 | + if (\is_array($item)) { |
|
| 377 | + $collection = []; |
|
| 378 | + foreach ($item as $k => $v) { |
|
| 379 | + $collection[$k] = $this->lookupRecursive($v); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + return $collection; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + return $this->lookup($item); |
|
| 386 | + } |
|
| 387 | 387 | } |
@@ -15,12 +15,12 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_NullTransport extends Swift_Transport_NullTransport |
| 17 | 17 | { |
| 18 | - public function __construct() |
|
| 19 | - { |
|
| 20 | - \call_user_func_array( |
|
| 21 | - [$this, 'Swift_Transport_NullTransport::__construct'], |
|
| 22 | - Swift_DependencyContainer::getInstance() |
|
| 23 | - ->createDependenciesFor('transport.null') |
|
| 24 | - ); |
|
| 25 | - } |
|
| 18 | + public function __construct() |
|
| 19 | + { |
|
| 20 | + \call_user_func_array( |
|
| 21 | + [$this, 'Swift_Transport_NullTransport::__construct'], |
|
| 22 | + Swift_DependencyContainer::getInstance() |
|
| 23 | + ->createDependenciesFor('transport.null') |
|
| 24 | + ); |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -15,10 +15,10 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | interface Swift_FileStream extends Swift_OutputByteStream |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Get the complete path to the file. |
|
| 20 | - * |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - public function getPath(); |
|
| 18 | + /** |
|
| 19 | + * Get the complete path to the file. |
|
| 20 | + * |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + public function getPath(); |
|
| 24 | 24 | } |
@@ -18,29 +18,29 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | interface Swift_OutputByteStream |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Reads $length bytes from the stream into a string and moves the pointer |
|
| 23 | - * through the stream by $length. |
|
| 24 | - * |
|
| 25 | - * If less bytes exist than are requested the remaining bytes are given instead. |
|
| 26 | - * If no bytes are remaining at all, boolean false is returned. |
|
| 27 | - * |
|
| 28 | - * @param int $length |
|
| 29 | - * |
|
| 30 | - * @throws Swift_IoException |
|
| 31 | - * |
|
| 32 | - * @return string|bool |
|
| 33 | - */ |
|
| 34 | - public function read($length); |
|
| 21 | + /** |
|
| 22 | + * Reads $length bytes from the stream into a string and moves the pointer |
|
| 23 | + * through the stream by $length. |
|
| 24 | + * |
|
| 25 | + * If less bytes exist than are requested the remaining bytes are given instead. |
|
| 26 | + * If no bytes are remaining at all, boolean false is returned. |
|
| 27 | + * |
|
| 28 | + * @param int $length |
|
| 29 | + * |
|
| 30 | + * @throws Swift_IoException |
|
| 31 | + * |
|
| 32 | + * @return string|bool |
|
| 33 | + */ |
|
| 34 | + public function read($length); |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Move the internal read pointer to $byteOffset in the stream. |
|
| 38 | - * |
|
| 39 | - * @param int $byteOffset |
|
| 40 | - * |
|
| 41 | - * @throws Swift_IoException |
|
| 42 | - * |
|
| 43 | - * @return bool |
|
| 44 | - */ |
|
| 45 | - public function setReadPointer($byteOffset); |
|
| 36 | + /** |
|
| 37 | + * Move the internal read pointer to $byteOffset in the stream. |
|
| 38 | + * |
|
| 39 | + * @param int $byteOffset |
|
| 40 | + * |
|
| 41 | + * @throws Swift_IoException |
|
| 42 | + * |
|
| 43 | + * @return bool |
|
| 44 | + */ |
|
| 45 | + public function setReadPointer($byteOffset); |
|
| 46 | 46 | } |
@@ -15,12 +15,12 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | interface Swift_CharacterReaderFactory |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Returns a CharacterReader suitable for the charset applied. |
|
| 20 | - * |
|
| 21 | - * @param string $charset |
|
| 22 | - * |
|
| 23 | - * @return Swift_CharacterReader |
|
| 24 | - */ |
|
| 25 | - public function getReaderFor($charset); |
|
| 18 | + /** |
|
| 19 | + * Returns a CharacterReader suitable for the charset applied. |
|
| 20 | + * |
|
| 21 | + * @param string $charset |
|
| 22 | + * |
|
| 23 | + * @return Swift_CharacterReader |
|
| 24 | + */ |
|
| 25 | + public function getReaderFor($charset); |
|
| 26 | 26 | } |
@@ -15,90 +15,90 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | interface Swift_KeyCache |
| 17 | 17 | { |
| 18 | - /** Mode for replacing existing cached data */ |
|
| 19 | - const MODE_WRITE = 1; |
|
| 18 | + /** Mode for replacing existing cached data */ |
|
| 19 | + const MODE_WRITE = 1; |
|
| 20 | 20 | |
| 21 | - /** Mode for appending data to the end of existing cached data */ |
|
| 22 | - const MODE_APPEND = 2; |
|
| 21 | + /** Mode for appending data to the end of existing cached data */ |
|
| 22 | + const MODE_APPEND = 2; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Set a string into the cache under $itemKey for the namespace $nsKey. |
|
| 26 | - * |
|
| 27 | - * @see MODE_WRITE, MODE_APPEND |
|
| 28 | - * |
|
| 29 | - * @param string $nsKey |
|
| 30 | - * @param string $itemKey |
|
| 31 | - * @param string $string |
|
| 32 | - * @param int $mode |
|
| 33 | - */ |
|
| 34 | - public function setString($nsKey, $itemKey, $string, $mode); |
|
| 24 | + /** |
|
| 25 | + * Set a string into the cache under $itemKey for the namespace $nsKey. |
|
| 26 | + * |
|
| 27 | + * @see MODE_WRITE, MODE_APPEND |
|
| 28 | + * |
|
| 29 | + * @param string $nsKey |
|
| 30 | + * @param string $itemKey |
|
| 31 | + * @param string $string |
|
| 32 | + * @param int $mode |
|
| 33 | + */ |
|
| 34 | + public function setString($nsKey, $itemKey, $string, $mode); |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Set a ByteStream into the cache under $itemKey for the namespace $nsKey. |
|
| 38 | - * |
|
| 39 | - * @see MODE_WRITE, MODE_APPEND |
|
| 40 | - * |
|
| 41 | - * @param string $nsKey |
|
| 42 | - * @param string $itemKey |
|
| 43 | - * @param int $mode |
|
| 44 | - */ |
|
| 45 | - public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode); |
|
| 36 | + /** |
|
| 37 | + * Set a ByteStream into the cache under $itemKey for the namespace $nsKey. |
|
| 38 | + * |
|
| 39 | + * @see MODE_WRITE, MODE_APPEND |
|
| 40 | + * |
|
| 41 | + * @param string $nsKey |
|
| 42 | + * @param string $itemKey |
|
| 43 | + * @param int $mode |
|
| 44 | + */ |
|
| 45 | + public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode); |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Provides a ByteStream which when written to, writes data to $itemKey. |
|
| 49 | - * |
|
| 50 | - * NOTE: The stream will always write in append mode. |
|
| 51 | - * If the optional third parameter is passed all writes will go through $is. |
|
| 52 | - * |
|
| 53 | - * @param string $nsKey |
|
| 54 | - * @param string $itemKey |
|
| 55 | - * @param Swift_InputByteStream $is optional input stream |
|
| 56 | - * |
|
| 57 | - * @return Swift_InputByteStream |
|
| 58 | - */ |
|
| 59 | - public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $is = null); |
|
| 47 | + /** |
|
| 48 | + * Provides a ByteStream which when written to, writes data to $itemKey. |
|
| 49 | + * |
|
| 50 | + * NOTE: The stream will always write in append mode. |
|
| 51 | + * If the optional third parameter is passed all writes will go through $is. |
|
| 52 | + * |
|
| 53 | + * @param string $nsKey |
|
| 54 | + * @param string $itemKey |
|
| 55 | + * @param Swift_InputByteStream $is optional input stream |
|
| 56 | + * |
|
| 57 | + * @return Swift_InputByteStream |
|
| 58 | + */ |
|
| 59 | + public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $is = null); |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Get data back out of the cache as a string. |
|
| 63 | - * |
|
| 64 | - * @param string $nsKey |
|
| 65 | - * @param string $itemKey |
|
| 66 | - * |
|
| 67 | - * @return string |
|
| 68 | - */ |
|
| 69 | - public function getString($nsKey, $itemKey); |
|
| 61 | + /** |
|
| 62 | + * Get data back out of the cache as a string. |
|
| 63 | + * |
|
| 64 | + * @param string $nsKey |
|
| 65 | + * @param string $itemKey |
|
| 66 | + * |
|
| 67 | + * @return string |
|
| 68 | + */ |
|
| 69 | + public function getString($nsKey, $itemKey); |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Get data back out of the cache as a ByteStream. |
|
| 73 | - * |
|
| 74 | - * @param string $nsKey |
|
| 75 | - * @param string $itemKey |
|
| 76 | - * @param Swift_InputByteStream $is stream to write the data to |
|
| 77 | - */ |
|
| 78 | - public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is); |
|
| 71 | + /** |
|
| 72 | + * Get data back out of the cache as a ByteStream. |
|
| 73 | + * |
|
| 74 | + * @param string $nsKey |
|
| 75 | + * @param string $itemKey |
|
| 76 | + * @param Swift_InputByteStream $is stream to write the data to |
|
| 77 | + */ |
|
| 78 | + public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is); |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Check if the given $itemKey exists in the namespace $nsKey. |
|
| 82 | - * |
|
| 83 | - * @param string $nsKey |
|
| 84 | - * @param string $itemKey |
|
| 85 | - * |
|
| 86 | - * @return bool |
|
| 87 | - */ |
|
| 88 | - public function hasKey($nsKey, $itemKey); |
|
| 80 | + /** |
|
| 81 | + * Check if the given $itemKey exists in the namespace $nsKey. |
|
| 82 | + * |
|
| 83 | + * @param string $nsKey |
|
| 84 | + * @param string $itemKey |
|
| 85 | + * |
|
| 86 | + * @return bool |
|
| 87 | + */ |
|
| 88 | + public function hasKey($nsKey, $itemKey); |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Clear data for $itemKey in the namespace $nsKey if it exists. |
|
| 92 | - * |
|
| 93 | - * @param string $nsKey |
|
| 94 | - * @param string $itemKey |
|
| 95 | - */ |
|
| 96 | - public function clearKey($nsKey, $itemKey); |
|
| 90 | + /** |
|
| 91 | + * Clear data for $itemKey in the namespace $nsKey if it exists. |
|
| 92 | + * |
|
| 93 | + * @param string $nsKey |
|
| 94 | + * @param string $itemKey |
|
| 95 | + */ |
|
| 96 | + public function clearKey($nsKey, $itemKey); |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Clear all data in the namespace $nsKey if it exists. |
|
| 100 | - * |
|
| 101 | - * @param string $nsKey |
|
| 102 | - */ |
|
| 103 | - public function clearAll($nsKey); |
|
| 98 | + /** |
|
| 99 | + * Clear all data in the namespace $nsKey if it exists. |
|
| 100 | + * |
|
| 101 | + * @param string $nsKey |
|
| 102 | + */ |
|
| 103 | + public function clearAll($nsKey); |
|
| 104 | 104 | } |
@@ -15,162 +15,162 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class Swift_ByteStream_AbstractFilterableInputStream implements Swift_InputByteStream, Swift_Filterable |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Write sequence. |
|
| 20 | - */ |
|
| 21 | - protected $sequence = 0; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * StreamFilters. |
|
| 25 | - * |
|
| 26 | - * @var Swift_StreamFilter[] |
|
| 27 | - */ |
|
| 28 | - private $filters = []; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * A buffer for writing. |
|
| 32 | - */ |
|
| 33 | - private $writeBuffer = ''; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Bound streams. |
|
| 37 | - * |
|
| 38 | - * @var Swift_InputByteStream[] |
|
| 39 | - */ |
|
| 40 | - private $mirrors = []; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Commit the given bytes to the storage medium immediately. |
|
| 44 | - * |
|
| 45 | - * @param string $bytes |
|
| 46 | - */ |
|
| 47 | - abstract protected function doCommit($bytes); |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Flush any buffers/content with immediate effect. |
|
| 51 | - */ |
|
| 52 | - abstract protected function flush(); |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Add a StreamFilter to this InputByteStream. |
|
| 56 | - * |
|
| 57 | - * @param string $key |
|
| 58 | - */ |
|
| 59 | - public function addFilter(Swift_StreamFilter $filter, $key) |
|
| 60 | - { |
|
| 61 | - $this->filters[$key] = $filter; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Remove an already present StreamFilter based on its $key. |
|
| 66 | - * |
|
| 67 | - * @param string $key |
|
| 68 | - */ |
|
| 69 | - public function removeFilter($key) |
|
| 70 | - { |
|
| 71 | - unset($this->filters[$key]); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Writes $bytes to the end of the stream. |
|
| 76 | - * |
|
| 77 | - * @param string $bytes |
|
| 78 | - * |
|
| 79 | - * @throws Swift_IoException |
|
| 80 | - * |
|
| 81 | - * @return int |
|
| 82 | - */ |
|
| 83 | - public function write($bytes) |
|
| 84 | - { |
|
| 85 | - $this->writeBuffer .= $bytes; |
|
| 86 | - foreach ($this->filters as $filter) { |
|
| 87 | - if ($filter->shouldBuffer($this->writeBuffer)) { |
|
| 88 | - return; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - $this->doWrite($this->writeBuffer); |
|
| 92 | - |
|
| 93 | - return ++$this->sequence; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * For any bytes that are currently buffered inside the stream, force them |
|
| 98 | - * off the buffer. |
|
| 99 | - * |
|
| 100 | - * @throws Swift_IoException |
|
| 101 | - */ |
|
| 102 | - public function commit() |
|
| 103 | - { |
|
| 104 | - $this->doWrite($this->writeBuffer); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Attach $is to this stream. |
|
| 109 | - * |
|
| 110 | - * The stream acts as an observer, receiving all data that is written. |
|
| 111 | - * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
|
| 112 | - */ |
|
| 113 | - public function bind(Swift_InputByteStream $is) |
|
| 114 | - { |
|
| 115 | - $this->mirrors[] = $is; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Remove an already bound stream. |
|
| 120 | - * |
|
| 121 | - * If $is is not bound, no errors will be raised. |
|
| 122 | - * If the stream currently has any buffered data it will be written to $is |
|
| 123 | - * before unbinding occurs. |
|
| 124 | - */ |
|
| 125 | - public function unbind(Swift_InputByteStream $is) |
|
| 126 | - { |
|
| 127 | - foreach ($this->mirrors as $k => $stream) { |
|
| 128 | - if ($is === $stream) { |
|
| 129 | - if ('' !== $this->writeBuffer) { |
|
| 130 | - $stream->write($this->writeBuffer); |
|
| 131 | - } |
|
| 132 | - unset($this->mirrors[$k]); |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Flush the contents of the stream (empty it) and set the internal pointer |
|
| 139 | - * to the beginning. |
|
| 140 | - * |
|
| 141 | - * @throws Swift_IoException |
|
| 142 | - */ |
|
| 143 | - public function flushBuffers() |
|
| 144 | - { |
|
| 145 | - if ('' !== $this->writeBuffer) { |
|
| 146 | - $this->doWrite($this->writeBuffer); |
|
| 147 | - } |
|
| 148 | - $this->flush(); |
|
| 149 | - |
|
| 150 | - foreach ($this->mirrors as $stream) { |
|
| 151 | - $stream->flushBuffers(); |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** Run $bytes through all filters */ |
|
| 156 | - private function filter($bytes) |
|
| 157 | - { |
|
| 158 | - foreach ($this->filters as $filter) { |
|
| 159 | - $bytes = $filter->filter($bytes); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return $bytes; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** Just write the bytes to the stream */ |
|
| 166 | - private function doWrite($bytes) |
|
| 167 | - { |
|
| 168 | - $this->doCommit($this->filter($bytes)); |
|
| 169 | - |
|
| 170 | - foreach ($this->mirrors as $stream) { |
|
| 171 | - $stream->write($bytes); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - $this->writeBuffer = ''; |
|
| 175 | - } |
|
| 18 | + /** |
|
| 19 | + * Write sequence. |
|
| 20 | + */ |
|
| 21 | + protected $sequence = 0; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * StreamFilters. |
|
| 25 | + * |
|
| 26 | + * @var Swift_StreamFilter[] |
|
| 27 | + */ |
|
| 28 | + private $filters = []; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * A buffer for writing. |
|
| 32 | + */ |
|
| 33 | + private $writeBuffer = ''; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Bound streams. |
|
| 37 | + * |
|
| 38 | + * @var Swift_InputByteStream[] |
|
| 39 | + */ |
|
| 40 | + private $mirrors = []; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Commit the given bytes to the storage medium immediately. |
|
| 44 | + * |
|
| 45 | + * @param string $bytes |
|
| 46 | + */ |
|
| 47 | + abstract protected function doCommit($bytes); |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Flush any buffers/content with immediate effect. |
|
| 51 | + */ |
|
| 52 | + abstract protected function flush(); |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Add a StreamFilter to this InputByteStream. |
|
| 56 | + * |
|
| 57 | + * @param string $key |
|
| 58 | + */ |
|
| 59 | + public function addFilter(Swift_StreamFilter $filter, $key) |
|
| 60 | + { |
|
| 61 | + $this->filters[$key] = $filter; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Remove an already present StreamFilter based on its $key. |
|
| 66 | + * |
|
| 67 | + * @param string $key |
|
| 68 | + */ |
|
| 69 | + public function removeFilter($key) |
|
| 70 | + { |
|
| 71 | + unset($this->filters[$key]); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Writes $bytes to the end of the stream. |
|
| 76 | + * |
|
| 77 | + * @param string $bytes |
|
| 78 | + * |
|
| 79 | + * @throws Swift_IoException |
|
| 80 | + * |
|
| 81 | + * @return int |
|
| 82 | + */ |
|
| 83 | + public function write($bytes) |
|
| 84 | + { |
|
| 85 | + $this->writeBuffer .= $bytes; |
|
| 86 | + foreach ($this->filters as $filter) { |
|
| 87 | + if ($filter->shouldBuffer($this->writeBuffer)) { |
|
| 88 | + return; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + $this->doWrite($this->writeBuffer); |
|
| 92 | + |
|
| 93 | + return ++$this->sequence; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * For any bytes that are currently buffered inside the stream, force them |
|
| 98 | + * off the buffer. |
|
| 99 | + * |
|
| 100 | + * @throws Swift_IoException |
|
| 101 | + */ |
|
| 102 | + public function commit() |
|
| 103 | + { |
|
| 104 | + $this->doWrite($this->writeBuffer); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Attach $is to this stream. |
|
| 109 | + * |
|
| 110 | + * The stream acts as an observer, receiving all data that is written. |
|
| 111 | + * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
|
| 112 | + */ |
|
| 113 | + public function bind(Swift_InputByteStream $is) |
|
| 114 | + { |
|
| 115 | + $this->mirrors[] = $is; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Remove an already bound stream. |
|
| 120 | + * |
|
| 121 | + * If $is is not bound, no errors will be raised. |
|
| 122 | + * If the stream currently has any buffered data it will be written to $is |
|
| 123 | + * before unbinding occurs. |
|
| 124 | + */ |
|
| 125 | + public function unbind(Swift_InputByteStream $is) |
|
| 126 | + { |
|
| 127 | + foreach ($this->mirrors as $k => $stream) { |
|
| 128 | + if ($is === $stream) { |
|
| 129 | + if ('' !== $this->writeBuffer) { |
|
| 130 | + $stream->write($this->writeBuffer); |
|
| 131 | + } |
|
| 132 | + unset($this->mirrors[$k]); |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Flush the contents of the stream (empty it) and set the internal pointer |
|
| 139 | + * to the beginning. |
|
| 140 | + * |
|
| 141 | + * @throws Swift_IoException |
|
| 142 | + */ |
|
| 143 | + public function flushBuffers() |
|
| 144 | + { |
|
| 145 | + if ('' !== $this->writeBuffer) { |
|
| 146 | + $this->doWrite($this->writeBuffer); |
|
| 147 | + } |
|
| 148 | + $this->flush(); |
|
| 149 | + |
|
| 150 | + foreach ($this->mirrors as $stream) { |
|
| 151 | + $stream->flushBuffers(); |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** Run $bytes through all filters */ |
|
| 156 | + private function filter($bytes) |
|
| 157 | + { |
|
| 158 | + foreach ($this->filters as $filter) { |
|
| 159 | + $bytes = $filter->filter($bytes); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return $bytes; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** Just write the bytes to the stream */ |
|
| 166 | + private function doWrite($bytes) |
|
| 167 | + { |
|
| 168 | + $this->doCommit($this->filter($bytes)); |
|
| 169 | + |
|
| 170 | + foreach ($this->mirrors as $stream) { |
|
| 171 | + $stream->write($bytes); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + $this->writeBuffer = ''; |
|
| 175 | + } |
|
| 176 | 176 | } |
@@ -15,164 +15,164 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_ByteStream_ArrayByteStream implements Swift_InputByteStream, Swift_OutputByteStream |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * The internal stack of bytes. |
|
| 20 | - * |
|
| 21 | - * @var string[] |
|
| 22 | - */ |
|
| 23 | - private $array = []; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * The size of the stack. |
|
| 27 | - * |
|
| 28 | - * @var int |
|
| 29 | - */ |
|
| 30 | - private $arraySize = 0; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * The internal pointer offset. |
|
| 34 | - * |
|
| 35 | - * @var int |
|
| 36 | - */ |
|
| 37 | - private $offset = 0; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Bound streams. |
|
| 41 | - * |
|
| 42 | - * @var Swift_InputByteStream[] |
|
| 43 | - */ |
|
| 44 | - private $mirrors = []; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Create a new ArrayByteStream. |
|
| 48 | - * |
|
| 49 | - * If $stack is given the stream will be populated with the bytes it contains. |
|
| 50 | - * |
|
| 51 | - * @param mixed $stack of bytes in string or array form, optional |
|
| 52 | - */ |
|
| 53 | - public function __construct($stack = null) |
|
| 54 | - { |
|
| 55 | - if (\is_array($stack)) { |
|
| 56 | - $this->array = $stack; |
|
| 57 | - $this->arraySize = \count($stack); |
|
| 58 | - } elseif (\is_string($stack)) { |
|
| 59 | - $this->write($stack); |
|
| 60 | - } else { |
|
| 61 | - $this->array = []; |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Reads $length bytes from the stream into a string and moves the pointer |
|
| 67 | - * through the stream by $length. |
|
| 68 | - * |
|
| 69 | - * If less bytes exist than are requested the |
|
| 70 | - * remaining bytes are given instead. If no bytes are remaining at all, boolean |
|
| 71 | - * false is returned. |
|
| 72 | - * |
|
| 73 | - * @param int $length |
|
| 74 | - * |
|
| 75 | - * @return string |
|
| 76 | - */ |
|
| 77 | - public function read($length) |
|
| 78 | - { |
|
| 79 | - if ($this->offset == $this->arraySize) { |
|
| 80 | - return false; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // Don't use array slice |
|
| 84 | - $end = $length + $this->offset; |
|
| 85 | - $end = $this->arraySize < $end ? $this->arraySize : $end; |
|
| 86 | - $ret = ''; |
|
| 87 | - for (; $this->offset < $end; ++$this->offset) { |
|
| 88 | - $ret .= $this->array[$this->offset]; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return $ret; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Writes $bytes to the end of the stream. |
|
| 96 | - * |
|
| 97 | - * @param string $bytes |
|
| 98 | - */ |
|
| 99 | - public function write($bytes) |
|
| 100 | - { |
|
| 101 | - $to_add = str_split($bytes); |
|
| 102 | - foreach ($to_add as $value) { |
|
| 103 | - $this->array[] = $value; |
|
| 104 | - } |
|
| 105 | - $this->arraySize = \count($this->array); |
|
| 106 | - |
|
| 107 | - foreach ($this->mirrors as $stream) { |
|
| 108 | - $stream->write($bytes); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Not used. |
|
| 114 | - */ |
|
| 115 | - public function commit() |
|
| 116 | - { |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Attach $is to this stream. |
|
| 121 | - * |
|
| 122 | - * The stream acts as an observer, receiving all data that is written. |
|
| 123 | - * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
|
| 124 | - */ |
|
| 125 | - public function bind(Swift_InputByteStream $is) |
|
| 126 | - { |
|
| 127 | - $this->mirrors[] = $is; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Remove an already bound stream. |
|
| 132 | - * |
|
| 133 | - * If $is is not bound, no errors will be raised. |
|
| 134 | - * If the stream currently has any buffered data it will be written to $is |
|
| 135 | - * before unbinding occurs. |
|
| 136 | - */ |
|
| 137 | - public function unbind(Swift_InputByteStream $is) |
|
| 138 | - { |
|
| 139 | - foreach ($this->mirrors as $k => $stream) { |
|
| 140 | - if ($is === $stream) { |
|
| 141 | - unset($this->mirrors[$k]); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Move the internal read pointer to $byteOffset in the stream. |
|
| 148 | - * |
|
| 149 | - * @param int $byteOffset |
|
| 150 | - * |
|
| 151 | - * @return bool |
|
| 152 | - */ |
|
| 153 | - public function setReadPointer($byteOffset) |
|
| 154 | - { |
|
| 155 | - if ($byteOffset > $this->arraySize) { |
|
| 156 | - $byteOffset = $this->arraySize; |
|
| 157 | - } elseif ($byteOffset < 0) { |
|
| 158 | - $byteOffset = 0; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $this->offset = $byteOffset; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Flush the contents of the stream (empty it) and set the internal pointer |
|
| 166 | - * to the beginning. |
|
| 167 | - */ |
|
| 168 | - public function flushBuffers() |
|
| 169 | - { |
|
| 170 | - $this->offset = 0; |
|
| 171 | - $this->array = []; |
|
| 172 | - $this->arraySize = 0; |
|
| 173 | - |
|
| 174 | - foreach ($this->mirrors as $stream) { |
|
| 175 | - $stream->flushBuffers(); |
|
| 176 | - } |
|
| 177 | - } |
|
| 18 | + /** |
|
| 19 | + * The internal stack of bytes. |
|
| 20 | + * |
|
| 21 | + * @var string[] |
|
| 22 | + */ |
|
| 23 | + private $array = []; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * The size of the stack. |
|
| 27 | + * |
|
| 28 | + * @var int |
|
| 29 | + */ |
|
| 30 | + private $arraySize = 0; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * The internal pointer offset. |
|
| 34 | + * |
|
| 35 | + * @var int |
|
| 36 | + */ |
|
| 37 | + private $offset = 0; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Bound streams. |
|
| 41 | + * |
|
| 42 | + * @var Swift_InputByteStream[] |
|
| 43 | + */ |
|
| 44 | + private $mirrors = []; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Create a new ArrayByteStream. |
|
| 48 | + * |
|
| 49 | + * If $stack is given the stream will be populated with the bytes it contains. |
|
| 50 | + * |
|
| 51 | + * @param mixed $stack of bytes in string or array form, optional |
|
| 52 | + */ |
|
| 53 | + public function __construct($stack = null) |
|
| 54 | + { |
|
| 55 | + if (\is_array($stack)) { |
|
| 56 | + $this->array = $stack; |
|
| 57 | + $this->arraySize = \count($stack); |
|
| 58 | + } elseif (\is_string($stack)) { |
|
| 59 | + $this->write($stack); |
|
| 60 | + } else { |
|
| 61 | + $this->array = []; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Reads $length bytes from the stream into a string and moves the pointer |
|
| 67 | + * through the stream by $length. |
|
| 68 | + * |
|
| 69 | + * If less bytes exist than are requested the |
|
| 70 | + * remaining bytes are given instead. If no bytes are remaining at all, boolean |
|
| 71 | + * false is returned. |
|
| 72 | + * |
|
| 73 | + * @param int $length |
|
| 74 | + * |
|
| 75 | + * @return string |
|
| 76 | + */ |
|
| 77 | + public function read($length) |
|
| 78 | + { |
|
| 79 | + if ($this->offset == $this->arraySize) { |
|
| 80 | + return false; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // Don't use array slice |
|
| 84 | + $end = $length + $this->offset; |
|
| 85 | + $end = $this->arraySize < $end ? $this->arraySize : $end; |
|
| 86 | + $ret = ''; |
|
| 87 | + for (; $this->offset < $end; ++$this->offset) { |
|
| 88 | + $ret .= $this->array[$this->offset]; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return $ret; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Writes $bytes to the end of the stream. |
|
| 96 | + * |
|
| 97 | + * @param string $bytes |
|
| 98 | + */ |
|
| 99 | + public function write($bytes) |
|
| 100 | + { |
|
| 101 | + $to_add = str_split($bytes); |
|
| 102 | + foreach ($to_add as $value) { |
|
| 103 | + $this->array[] = $value; |
|
| 104 | + } |
|
| 105 | + $this->arraySize = \count($this->array); |
|
| 106 | + |
|
| 107 | + foreach ($this->mirrors as $stream) { |
|
| 108 | + $stream->write($bytes); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Not used. |
|
| 114 | + */ |
|
| 115 | + public function commit() |
|
| 116 | + { |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Attach $is to this stream. |
|
| 121 | + * |
|
| 122 | + * The stream acts as an observer, receiving all data that is written. |
|
| 123 | + * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
|
| 124 | + */ |
|
| 125 | + public function bind(Swift_InputByteStream $is) |
|
| 126 | + { |
|
| 127 | + $this->mirrors[] = $is; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Remove an already bound stream. |
|
| 132 | + * |
|
| 133 | + * If $is is not bound, no errors will be raised. |
|
| 134 | + * If the stream currently has any buffered data it will be written to $is |
|
| 135 | + * before unbinding occurs. |
|
| 136 | + */ |
|
| 137 | + public function unbind(Swift_InputByteStream $is) |
|
| 138 | + { |
|
| 139 | + foreach ($this->mirrors as $k => $stream) { |
|
| 140 | + if ($is === $stream) { |
|
| 141 | + unset($this->mirrors[$k]); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Move the internal read pointer to $byteOffset in the stream. |
|
| 148 | + * |
|
| 149 | + * @param int $byteOffset |
|
| 150 | + * |
|
| 151 | + * @return bool |
|
| 152 | + */ |
|
| 153 | + public function setReadPointer($byteOffset) |
|
| 154 | + { |
|
| 155 | + if ($byteOffset > $this->arraySize) { |
|
| 156 | + $byteOffset = $this->arraySize; |
|
| 157 | + } elseif ($byteOffset < 0) { |
|
| 158 | + $byteOffset = 0; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $this->offset = $byteOffset; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Flush the contents of the stream (empty it) and set the internal pointer |
|
| 166 | + * to the beginning. |
|
| 167 | + */ |
|
| 168 | + public function flushBuffers() |
|
| 169 | + { |
|
| 170 | + $this->offset = 0; |
|
| 171 | + $this->array = []; |
|
| 172 | + $this->arraySize = 0; |
|
| 173 | + |
|
| 174 | + foreach ($this->mirrors as $stream) { |
|
| 175 | + $stream->flushBuffers(); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | 178 | } |
@@ -13,40 +13,40 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Swift_ByteStream_TemporaryFileByteStream extends Swift_ByteStream_FileByteStream |
| 15 | 15 | { |
| 16 | - public function __construct() |
|
| 17 | - { |
|
| 18 | - $filePath = tempnam(sys_get_temp_dir(), 'FileByteStream'); |
|
| 19 | - |
|
| 20 | - if (false === $filePath) { |
|
| 21 | - throw new Swift_IoException('Failed to retrieve temporary file name.'); |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - parent::__construct($filePath, true); |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - public function getContent() |
|
| 28 | - { |
|
| 29 | - if (false === ($content = file_get_contents($this->getPath()))) { |
|
| 30 | - throw new Swift_IoException('Failed to get temporary file content.'); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - return $content; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - public function __destruct() |
|
| 37 | - { |
|
| 38 | - if (file_exists($this->getPath())) { |
|
| 39 | - @unlink($this->getPath()); |
|
| 40 | - } |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function __sleep() |
|
| 44 | - { |
|
| 45 | - throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - public function __wakeup() |
|
| 49 | - { |
|
| 50 | - throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
| 51 | - } |
|
| 16 | + public function __construct() |
|
| 17 | + { |
|
| 18 | + $filePath = tempnam(sys_get_temp_dir(), 'FileByteStream'); |
|
| 19 | + |
|
| 20 | + if (false === $filePath) { |
|
| 21 | + throw new Swift_IoException('Failed to retrieve temporary file name.'); |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + parent::__construct($filePath, true); |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + public function getContent() |
|
| 28 | + { |
|
| 29 | + if (false === ($content = file_get_contents($this->getPath()))) { |
|
| 30 | + throw new Swift_IoException('Failed to get temporary file content.'); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + return $content; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + public function __destruct() |
|
| 37 | + { |
|
| 38 | + if (file_exists($this->getPath())) { |
|
| 39 | + @unlink($this->getPath()); |
|
| 40 | + } |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function __sleep() |
|
| 44 | + { |
|
| 45 | + throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public function __wakeup() |
|
| 49 | + { |
|
| 50 | + throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
| 51 | + } |
|
| 52 | 52 | } |