AbstractDependencyType.php
2 years ago
Container.php
2 years ago
FactoryType.php
2 years ago
SharedType.php
2 years ago
AbstractDependencyType.php
55 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Registry; |
| 3 | |
| 4 | /** |
| 5 | * An abstract class for dependency types. |
| 6 | * |
| 7 | * Dependency types are instances of a dependency used by the |
| 8 | * Dependency Injection Container for storing dependencies to invoke as they |
| 9 | * are needed. |
| 10 | * |
| 11 | * @since 2.5.0 |
| 12 | */ |
| 13 | abstract class AbstractDependencyType { |
| 14 | |
| 15 | /** |
| 16 | * Holds a callable or value provided for this type. |
| 17 | * |
| 18 | * @var mixed |
| 19 | */ |
| 20 | private $callable_or_value; |
| 21 | |
| 22 | /** |
| 23 | * Constructor |
| 24 | * |
| 25 | * @param mixed $callable_or_value A callable or value for the dependency |
| 26 | * type instance. |
| 27 | */ |
| 28 | public function __construct( $callable_or_value ) { |
| 29 | $this->callable_or_value = $callable_or_value; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Resolver for the internal dependency value. |
| 34 | * |
| 35 | * @param Container $container The Dependency Injection Container. |
| 36 | * |
| 37 | * @return mixed |
| 38 | */ |
| 39 | protected function resolve_value( Container $container ) { |
| 40 | $callback = $this->callable_or_value; |
| 41 | return \is_callable( $callback ) |
| 42 | ? $callback( $container ) |
| 43 | : $callback; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Retrieves the value stored internally for this DependencyType |
| 48 | * |
| 49 | * @param Container $container The Dependency Injection Container. |
| 50 | * |
| 51 | * @return void |
| 52 | */ |
| 53 | abstract public function get( Container $container ); |
| 54 | } |
| 55 |