Automattic\WooCommerce\Internal\DependencyManagement
RuntimeContainer::get_core
Core function to get an instance of a class.
Method of the class: RuntimeContainer{}
No Hooks.
Returns
Object. The resolved object.
Usage
// protected - for code of main (parent) or child class $result = $this->get_core( $class_name, $resolve_chain );
- $class_name(string) (required)
- The class name.
- $resolve_chain(array) (required)
- 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() RuntimeContainer::get core code WC 10.7.0
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;
}