*/
private $serviceInstantiators = [];
+ /**
+ * @var boolean[] disabled status, per service name
+ */
+ private $disabled = [];
+
/**
* @var array
*/
}
}
+ /**
+ * Imports all wiring defined in $container. Wiring defined in $container
+ * will override any wiring already defined locally. However, already
+ * existing service instances will be preserved.
+ *
+ * @since 1.28
+ *
+ * @param ServiceContainer $container
+ * @param string[] $skip A list of service names to skip during import
+ */
+ public function importWiring( ServiceContainer $container, $skip = [] ) {
+ $newInstantiators = array_diff_key(
+ $container->serviceInstantiators,
+ array_flip( $skip )
+ );
+
+ $this->serviceInstantiators = array_merge(
+ $this->serviceInstantiators,
+ $newInstantiators
+ );
+ }
+
/**
* Returns true if a service is defined for $name, that is, if a call to getService( $name )
* would return a service instance.
}
$this->serviceInstantiators[$name] = $instantiator;
+ unset( $this->disabled[$name] );
}
/**
public function disableService( $name ) {
$this->resetService( $name );
- $this->redefineService( $name, function() use ( $name ) {
- throw new ServiceDisabledException( $name );
- } );
+ $this->disabled[$name] = true;
}
/**
}
unset( $this->services[$name] );
+ unset( $this->disabled[$name] );
}
/**
* @param string $name The service name
*
* @throws NoSuchServiceException if $name is not a known service.
- * @throws ServiceDisabledException if this container has already been destroyed.
+ * @throws ContainerDisabledException if this container has already been destroyed.
+ * @throws ServiceDisabledException if the requested service has been disabled.
*
* @return object The service instance
*/
throw new ContainerDisabledException();
}
+ if ( isset( $this->disabled[$name] ) ) {
+ throw new ServiceDisabledException( $name );
+ }
+
if ( !isset( $this->services[$name] ) ) {
$this->services[$name] = $this->createService( $name );
}
$this->serviceInstantiators[$name],
array_merge( [ $this ], $this->extraInstantiationParams )
);
+ // NOTE: when adding more wiring logic here, make sure copyWiring() is kept in sync!
} else {
throw new NoSuchServiceException( $name );
}
return $service;
}
+ /**
+ * @param string $name
+ * @return bool Whether the service is disabled
+ * @since 1.28
+ */
+ public function isServiceDisabled( $name ) {
+ return isset( $this->disabled[$name] );
+ }
}