Admin
8 months ago
Blocks
8 months ago
Caches
9 months ago
Caching
1 year ago
Checkout
10 months ago
Database
1 year ago
Enums
9 months ago
Internal
8 months ago
LayoutTemplates
2 years ago
Proxies
11 months ago
StoreApi
9 months ago
Utilities
8 months ago
Autoloader.php
1 year ago
Container.php
11 months ago
Deprecated.php
1 year ago
Packages.php
1 year ago
Container.php
81 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Container class file. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce; |
| 9 | |
| 10 | use Automattic\WooCommerce\Internal\DependencyManagement\ContainerException; |
| 11 | use Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer; |
| 12 | |
| 13 | /** |
| 14 | * PSR11 compliant dependency injection container for WooCommerce. |
| 15 | * |
| 16 | * Classes in the `src` directory should specify dependencies from that directory via an 'init' method having arguments |
| 17 | * with type hints. If an instance of the container itself is needed, the type hint to use is \Psr\Container\ContainerInterface. |
| 18 | * |
| 19 | * Classes in the `src` directory should interact with anything outside (especially code in the `includes` directory |
| 20 | * and WordPress functions) by using the classes in the `Proxies` directory. The exception is idempotent |
| 21 | * functions (e.g. `wp_parse_url`), those can be used directly. |
| 22 | * |
| 23 | * Classes in the `includes` directory should use the `wc_get_container` function to get the instance of the container when |
| 24 | * they need to get an instance of a class from the `src` directory. |
| 25 | * |
| 26 | * Internally, an instance of RuntimeContainer will be used for the actual class resolution. This class uses reflection |
| 27 | * to instantiate classes and figure out dependencies, so there's no need for explicit class registration. |
| 28 | * When running the unit tests suite this will be replaced with an instance of TestingContainer, |
| 29 | * which provides additional functionality. |
| 30 | */ |
| 31 | final class Container { |
| 32 | /** |
| 33 | * The underlying container. |
| 34 | * |
| 35 | * @var RuntimeContainer |
| 36 | */ |
| 37 | private $container; |
| 38 | |
| 39 | /** |
| 40 | * Class constructor. |
| 41 | */ |
| 42 | public function __construct() { |
| 43 | // When the League container was in use we allowed to retrieve the container itself |
| 44 | // by using 'Psr\Container\ContainerInterface' as the class identifier, |
| 45 | // we continue allowing that for compatibility. |
| 46 | $this->container = new RuntimeContainer( |
| 47 | array( |
| 48 | __CLASS__ => $this, |
| 49 | 'Psr\Container\ContainerInterface' => $this, |
| 50 | ) |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns an instance of the specified class. |
| 56 | * See the comment about ContainerException in RuntimeContainer::get. |
| 57 | * |
| 58 | * @param string $id Class name. |
| 59 | * |
| 60 | * @return object Object instance. |
| 61 | * |
| 62 | * @throws ContainerException Error when resolving the class to an object instance, or class not found. |
| 63 | * @throws \Exception Exception thrown in the constructor or in the 'init' method of one of the resolved classes. |
| 64 | */ |
| 65 | public function get( string $id ) { |
| 66 | return $this->container->get( $id ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns true if the container can return an instance of the given class or false otherwise. |
| 71 | * See the comment in RuntimeContainer::has. |
| 72 | * |
| 73 | * @param class-string $id Class name. |
| 74 | * |
| 75 | * @return bool |
| 76 | */ |
| 77 | public function has( string $id ): bool { |
| 78 | return $this->container->has( $id ); |
| 79 | } |
| 80 | } |
| 81 |