Automattic\WooCommerce\Internal\DependencyManagement

RuntimeContainer::get_core()protectedWC 1.0

Core function to get an instance of a class.

Метод класса: RuntimeContainer{}

Хуков нет.

Возвращает

Объект. The resolved object.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_core( $class_name, $resolve_chain );
$class_name(строка) (обязательный)
The class name.
$resolve_chain(массив) (обязательный)
Classes already resolved in this resolution chain. Passed between recursive calls to the method in order to detect a recursive resolution condition.

Код RuntimeContainer::get_core() WC 9.5.1

protected function get_core( string $class_name, array &$resolve_chain ) {
	if ( isset( $this->resolved_cache[ $class_name ] ) ) {
		return $this->resolved_cache[ $class_name ];
	}

	// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped

	if ( in_array( $class_name, $resolve_chain, true ) ) {
		throw new ContainerException( "Recursive resolution of class '$class_name'. Resolution chain: " . implode( ', ', $resolve_chain ) );
	}

	if ( ! $this->is_class_allowed( $class_name ) ) {
		throw new ContainerException( "Attempt to get an instance of class '$class_name', which is not in the " . self::WOOCOMMERCE_NAMESPACE . ' namespace. Did you forget to add a namespace import?' );
	}

	if ( ! class_exists( $class_name ) ) {
		throw new ContainerException( "Attempt to get an instance of class '$class_name', which doesn't exist." );
	}

	// Account for the containers used by the Store API and Blocks.
	if ( StringUtil::starts_with( $class_name, 'Automattic\WooCommerce\StoreApi\\' ) ) {
		return StoreApi::container()->get( $class_name );
	}
	if ( StringUtil::starts_with( $class_name, 'Automattic\WooCommerce\Blocks\\' ) ) {
		return BlocksPackage::container()->get( $class_name );
	}

	$resolve_chain[] = $class_name;

	try {
		$instance = $this->instantiate_class_using_reflection( $class_name, $resolve_chain );
	} catch ( \ReflectionException $e ) {
		throw new ContainerException( "Reflection error when resolving '$class_name': (" . get_class( $e ) . ") {$e->getMessage()}", 0, $e );
	}

	// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped

	$this->resolved_cache[ $class_name ] = $instance;

	return $instance;
}