Automattic\WooCommerce\Internal\DependencyManagement

ExtendedContainer::replace()publicWC 1.0

Replace an existing registration with a different concrete. See also 'reset_replacement' and 'reset_all_replacements'.

Method of the class: ExtendedContainer{}

No Hooks.

Return

DefinitionInterface. The modified definition.

Usage

$ExtendedContainer = new ExtendedContainer();
$ExtendedContainer->replace( $class_name, $concrete ) : DefinitionInterface;
$class_name(string) (required)
The class name whose definition will be replaced.
$concrete(mixed) (required)
The new concrete (same as "add").

ExtendedContainer::replace() code WC 8.7.0

public function replace( string $class_name, $concrete ) : DefinitionInterface {
	if ( ! $this->has( $class_name ) ) {
		throw new ContainerException( "The container doesn't have '$class_name' registered, please use 'add' instead of 'replace'." );
	}

	$concrete_class = $this->get_class_from_concrete( $concrete );
	if ( isset( $concrete_class ) && ! $this->is_class_allowed( $concrete_class ) && ! $this->is_anonymous_class( $concrete_class ) ) {
		throw new ContainerException( "You cannot use concrete '$concrete_class', only classes in the {$this->woocommerce_namespace} namespace are allowed." );
	}

	if ( ! array_key_exists( $class_name, $this->original_concretes ) ) {
		// LegacyProxy is a special case: we replace it with MockableLegacyProxy at unit testing bootstrap time.
		$original_concrete                       = LegacyProxy::class === $class_name ? MockableLegacyProxy::class : $this->extend( $class_name )->getConcrete( $concrete );
		$this->original_concretes[ $class_name ] = $original_concrete;
	}

	return $this->extend( $class_name )->setConcrete( $concrete );
}