Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Registry
File: Container.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\Blocks\Registry;
[1] Fix | Delete
[2] Fix | Delete
use Closure;
[3] Fix | Delete
use Exception;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* A simple Dependency Injection Container
[7] Fix | Delete
*
[8] Fix | Delete
* This is used to manage dependencies used throughout the plugin.
[9] Fix | Delete
*
[10] Fix | Delete
* @since 2.5.0
[11] Fix | Delete
*/
[12] Fix | Delete
class Container {
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* A map of Dependency Type objects used to resolve dependencies.
[16] Fix | Delete
*
[17] Fix | Delete
* @var AbstractDependencyType[]
[18] Fix | Delete
*/
[19] Fix | Delete
private $registry = [];
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Public api for adding a factory to the container.
[23] Fix | Delete
*
[24] Fix | Delete
* Factory dependencies will have the instantiation callback invoked
[25] Fix | Delete
* every time the dependency is requested.
[26] Fix | Delete
*
[27] Fix | Delete
* Typical Usage:
[28] Fix | Delete
*
[29] Fix | Delete
* ```
[30] Fix | Delete
* $container->register( MyClass::class, $container->factory( $mycallback ) );
[31] Fix | Delete
* ```
[32] Fix | Delete
*
[33] Fix | Delete
* @param Closure $instantiation_callback This will be invoked when the
[34] Fix | Delete
* dependency is required. It will
[35] Fix | Delete
* receive an instance of this
[36] Fix | Delete
* container so the callback can
[37] Fix | Delete
* retrieve dependencies from the
[38] Fix | Delete
* container.
[39] Fix | Delete
*
[40] Fix | Delete
* @return FactoryType An instance of the FactoryType dependency.
[41] Fix | Delete
*/
[42] Fix | Delete
public function factory( Closure $instantiation_callback ) {
[43] Fix | Delete
return new FactoryType( $instantiation_callback );
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* Interface for registering a new dependency with the container.
[48] Fix | Delete
*
[49] Fix | Delete
* By default, the $value will be added as a shared dependency. This means
[50] Fix | Delete
* that it will be a single instance shared among any other classes having
[51] Fix | Delete
* that dependency.
[52] Fix | Delete
*
[53] Fix | Delete
* If you want a new instance every time it's required, then wrap the value
[54] Fix | Delete
* in a call to the factory method (@see Container::factory for example)
[55] Fix | Delete
*
[56] Fix | Delete
* Note: Currently if the provided id already is registered in the container,
[57] Fix | Delete
* the provided value is ignored.
[58] Fix | Delete
*
[59] Fix | Delete
* @param string $id A unique string identifier for the provided value.
[60] Fix | Delete
* Typically it's the fully qualified name for the
[61] Fix | Delete
* dependency.
[62] Fix | Delete
* @param mixed $value The value for the dependency. Typically, this is a
[63] Fix | Delete
* closure that will create the class instance needed.
[64] Fix | Delete
*/
[65] Fix | Delete
public function register( $id, $value ) {
[66] Fix | Delete
if ( empty( $this->registry[ $id ] ) ) {
[67] Fix | Delete
if ( ! $value instanceof FactoryType ) {
[68] Fix | Delete
$value = new SharedType( $value );
[69] Fix | Delete
}
[70] Fix | Delete
$this->registry[ $id ] = $value;
[71] Fix | Delete
}
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
/**
[75] Fix | Delete
* Interface for retrieving the dependency stored in the container for the
[76] Fix | Delete
* given identifier.
[77] Fix | Delete
*
[78] Fix | Delete
* @param string $id The identifier for the dependency being retrieved.
[79] Fix | Delete
* @throws Exception If there is no dependency for the given identifier in
[80] Fix | Delete
* the container.
[81] Fix | Delete
*
[82] Fix | Delete
* @return mixed Typically a class instance.
[83] Fix | Delete
*/
[84] Fix | Delete
public function get( $id ) {
[85] Fix | Delete
if ( ! isset( $this->registry[ $id ] ) ) {
[86] Fix | Delete
// this is a developer facing exception, hence it is not localized.
[87] Fix | Delete
throw new Exception(
[88] Fix | Delete
sprintf(
[89] Fix | Delete
'Cannot construct an instance of %s because it has not been registered.',
[90] Fix | Delete
$id
[91] Fix | Delete
)
[92] Fix | Delete
);
[93] Fix | Delete
}
[94] Fix | Delete
return $this->registry[ $id ]->get( $this );
[95] Fix | Delete
}
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function