|
15 | 15 | use ProxyManager\Configuration as ProxyConfiguration;
|
16 | 16 | use ProxyManager\Factory\LazyLoadingValueHolderFactory;
|
17 | 17 | use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
|
| 18 | +use Zend\ServiceManager\Exception\ContainerModificationsNotAllowedException; |
18 | 19 | use Zend\ServiceManager\Exception\InvalidArgumentException;
|
19 | 20 | use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
20 | 21 | use Zend\ServiceManager\Factory\AbstractFactoryInterface;
|
@@ -53,6 +54,13 @@ class ServiceManager implements ServiceLocatorInterface
|
53 | 54 | */
|
54 | 55 | protected $aliases = [];
|
55 | 56 |
|
| 57 | + /** |
| 58 | + * Whether or not changes may be made to this instance. |
| 59 | + * |
| 60 | + * @param bool |
| 61 | + */ |
| 62 | + protected $allowOverride = true; |
| 63 | + |
56 | 64 | /**
|
57 | 65 | * @var ContainerInterface
|
58 | 66 | */
|
@@ -132,26 +140,6 @@ public function __construct(array $config = [])
|
132 | 140 | $this->configure($config);
|
133 | 141 | }
|
134 | 142 |
|
135 |
| - /** |
136 |
| - * Create a new service locator that merges the provided configuration. |
137 |
| - * |
138 |
| - * Note that the original service locator is left untouched (as with PSR-7 |
139 |
| - * interfaces). |
140 |
| - * |
141 |
| - * See {@see \Zend\ServiceManager\ServiceManager::configure()} for details |
142 |
| - * on what $config accepts. |
143 |
| - * |
144 |
| - * @param array $config |
145 |
| - * @return ContainerInterface |
146 |
| - */ |
147 |
| - public function withConfig(array $config) |
148 |
| - { |
149 |
| - $container = clone $this; |
150 |
| - $container->creationContext = ($this === $this->creationContext) ? $container : $this->creationContext; |
151 |
| - $container->configure($config); |
152 |
| - return $container; |
153 |
| - } |
154 |
| - |
155 | 143 | /**
|
156 | 144 | * {@inheritDoc}
|
157 | 145 | */
|
@@ -230,6 +218,26 @@ public function has($name)
|
230 | 218 | return false;
|
231 | 219 | }
|
232 | 220 |
|
| 221 | + /** |
| 222 | + * Indicate whether or not the instance is immutable. |
| 223 | + * |
| 224 | + * @param bool $flag |
| 225 | + */ |
| 226 | + public function setAllowOverride($flag) |
| 227 | + { |
| 228 | + $this->allowOverride = (bool) $flag; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Retrieve the flag indicating immutability status. |
| 233 | + * |
| 234 | + * @return bool |
| 235 | + */ |
| 236 | + public function getAllowOverride() |
| 237 | + { |
| 238 | + return $this->allowOverride; |
| 239 | + } |
| 240 | + |
233 | 241 | /**
|
234 | 242 | * Configure the service manager
|
235 | 243 | *
|
@@ -265,10 +273,18 @@ public function has($name)
|
265 | 273 | * should be shared by default.
|
266 | 274 | *
|
267 | 275 | * @param array $config
|
268 |
| - * @return void |
| 276 | + * @return self |
| 277 | + * @throws ContainerModificationsNotAllowedException if the allow |
| 278 | + * override flag has been toggled off. |
269 | 279 | */
|
270 |
| - protected function configure(array $config) |
| 280 | + public function configure(array $config) |
271 | 281 | {
|
| 282 | + if ($this->allowOverride === false) { |
| 283 | + throw new ContainerModificationsNotAllowedException( |
| 284 | + 'Overrides have been disabled on this container instance' |
| 285 | + ); |
| 286 | + } |
| 287 | + |
272 | 288 | if (isset($config['services'])) {
|
273 | 289 | $this->services = $config['services'] + $this->services;
|
274 | 290 | }
|
@@ -326,6 +342,109 @@ protected function configure(array $config)
|
326 | 342 | }
|
327 | 343 |
|
328 | 344 | $this->resolveAliases();
|
| 345 | + |
| 346 | + return $this; |
| 347 | + } |
| 348 | + |
| 349 | + /** |
| 350 | + * Add an alias. |
| 351 | + * |
| 352 | + * @param string $alias |
| 353 | + * @param string $target |
| 354 | + */ |
| 355 | + public function setAlias($alias, $target) |
| 356 | + { |
| 357 | + $this->configure(['aliases' => [$alias => $target]]); |
| 358 | + } |
| 359 | + |
| 360 | + /** |
| 361 | + * Add an invokable class mapping. |
| 362 | + * |
| 363 | + * @param string $name Service name |
| 364 | + * @param null|string $class Class to which to map; if omitted, $name is |
| 365 | + * assumed. |
| 366 | + */ |
| 367 | + public function setInvokableClass($name, $class = null) |
| 368 | + { |
| 369 | + $this->configure(['invokables' => [$name => $class ?: $name]]); |
| 370 | + } |
| 371 | + |
| 372 | + /** |
| 373 | + * Specify a factory for a given service name. |
| 374 | + * |
| 375 | + * @param string $name Service name |
| 376 | + * @param string|callable|Factory\FactoryInterface $factory Factory to which |
| 377 | + * to map. |
| 378 | + */ |
| 379 | + public function setFactory($name, $factory) |
| 380 | + { |
| 381 | + $this->configure(['factories' => [$name => $factory]]); |
| 382 | + } |
| 383 | + |
| 384 | + /** |
| 385 | + * Create a lazy service mapping to a class. |
| 386 | + * |
| 387 | + * @param string $name Service name to map |
| 388 | + * @param null|string $class Class to which to map; if not provided, $name |
| 389 | + * will be used for the mapping. |
| 390 | + */ |
| 391 | + public function mapLazyService($name, $class = null) |
| 392 | + { |
| 393 | + $this->configure(['lazy_services' => ['class_map' => [$name => $class ?: $name]]]); |
| 394 | + } |
| 395 | + |
| 396 | + /** |
| 397 | + * Add an abstract factory for resolving services. |
| 398 | + * |
| 399 | + * @param string|Factory\AbstractFactoryInterface $name Service name |
| 400 | + */ |
| 401 | + public function addAbstractFactory($factory) |
| 402 | + { |
| 403 | + $this->configure(['abstract_factories' => [$factory]]); |
| 404 | + } |
| 405 | + |
| 406 | + /** |
| 407 | + * Add a delegator for a given service. |
| 408 | + * |
| 409 | + * @param string $name Service name |
| 410 | + * @param string|callable|Factory\DelegatorFactoryInterface $factory Delegator |
| 411 | + * factory to assign. |
| 412 | + */ |
| 413 | + public function addDelegator($name, $factory) |
| 414 | + { |
| 415 | + $this->configure(['delegators' => [$name => [$factory]]]); |
| 416 | + } |
| 417 | + |
| 418 | + /** |
| 419 | + * Add an initializer. |
| 420 | + * |
| 421 | + * @param string|callable|Initalizer\InitializerInterface $initalizer |
| 422 | + */ |
| 423 | + public function addInitializer($initializer) |
| 424 | + { |
| 425 | + $this->configure(['initializers' => [$initializer]]); |
| 426 | + } |
| 427 | + |
| 428 | + /** |
| 429 | + * Map a service. |
| 430 | + * |
| 431 | + * @param string $name Service name |
| 432 | + * @param array|object $service |
| 433 | + */ |
| 434 | + public function setService($name, $service) |
| 435 | + { |
| 436 | + $this->configure(['services' => [$name => $service]]); |
| 437 | + } |
| 438 | + |
| 439 | + /** |
| 440 | + * Add a service sharing rule. |
| 441 | + * |
| 442 | + * @param string $name Service name |
| 443 | + * @param boolean $flag Whether or not the service should be shared. |
| 444 | + */ |
| 445 | + public function setShared($name, $flag) |
| 446 | + { |
| 447 | + $this->configure(['shared' => [$name => (bool) $flag]]); |
329 | 448 | }
|
330 | 449 |
|
331 | 450 | /**
|
|
0 commit comments