Checkout
5 years ago
Internal
5 years ago
Proxies
5 years ago
Utilities
5 years ago
Autoloader.php
5 years ago
Container.php
5 years ago
Packages.php
5 years ago
Container.php
96 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Container class file. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce; |
| 7 | |
| 8 | use Automattic\WooCommerce\Internal\DependencyManagement\ExtendedContainer; |
| 9 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\DownloadPermissionsAdjusterServiceProvider; |
| 10 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\AssignDefaultCategoryServiceProvider; |
| 11 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProductAttributesLookupServiceProvider; |
| 12 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProxiesServiceProvider; |
| 13 | |
| 14 | /** |
| 15 | * PSR11 compliant dependency injection container for WooCommerce. |
| 16 | * |
| 17 | * Classes in the `src` directory should specify dependencies from that directory via an 'init' method having arguments |
| 18 | * with type hints. If an instance of the container itself is needed, the type hint to use is \Psr\Container\ContainerInterface. |
| 19 | * |
| 20 | * Classes in the `src` directory should interact with anything outside (especially code in the `includes` directory |
| 21 | * and WordPress functions) by using the classes in the `Proxies` directory. The exception is idempotent |
| 22 | * functions (e.g. `wp_parse_url`), those can be used directly. |
| 23 | * |
| 24 | * Classes in the `includes` directory should use the `wc_get_container` function to get the instance of the container when |
| 25 | * they need to get an instance of a class from the `src` directory. |
| 26 | * |
| 27 | * Class registration should be done via service providers that inherit from Automattic\WooCommerce\Internal\DependencyManagement |
| 28 | * and those should go in the `src\Internal\DependencyManagement\ServiceProviders` folder unless there's a good reason |
| 29 | * to put them elsewhere. All the service provider class names must be in the `SERVICE_PROVIDERS` constant. |
| 30 | */ |
| 31 | final class Container implements \Psr\Container\ContainerInterface { |
| 32 | /** |
| 33 | * The list of service provider classes to register. |
| 34 | * |
| 35 | * @var string[] |
| 36 | */ |
| 37 | private $service_providers = array( |
| 38 | AssignDefaultCategoryServiceProvider::class, |
| 39 | DownloadPermissionsAdjusterServiceProvider::class, |
| 40 | ProductAttributesLookupServiceProvider::class, |
| 41 | ProxiesServiceProvider::class, |
| 42 | ); |
| 43 | |
| 44 | /** |
| 45 | * The underlying container. |
| 46 | * |
| 47 | * @var \League\Container\Container |
| 48 | */ |
| 49 | private $container; |
| 50 | |
| 51 | /** |
| 52 | * Class constructor. |
| 53 | */ |
| 54 | public function __construct() { |
| 55 | $this->container = new ExtendedContainer(); |
| 56 | |
| 57 | // Add ourselves as the shared instance of ContainerInterface, |
| 58 | // register everything else using service providers. |
| 59 | |
| 60 | $this->container->share( \Psr\Container\ContainerInterface::class, $this ); |
| 61 | |
| 62 | foreach ( $this->service_providers as $service_provider_class ) { |
| 63 | $this->container->addServiceProvider( $service_provider_class ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Finds an entry of the container by its identifier and returns it. |
| 69 | * |
| 70 | * @param string $id Identifier of the entry to look for. |
| 71 | * |
| 72 | * @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
| 73 | * @throws Psr\Container\ContainerExceptionInterface Error while retrieving the entry. |
| 74 | * |
| 75 | * @return mixed Entry. |
| 76 | */ |
| 77 | public function get( $id ) { |
| 78 | return $this->container->get( $id ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns true if the container can return an entry for the given identifier. |
| 83 | * Returns false otherwise. |
| 84 | * |
| 85 | * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
| 86 | * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
| 87 | * |
| 88 | * @param string $id Identifier of the entry to look for. |
| 89 | * |
| 90 | * @return bool |
| 91 | */ |
| 92 | public function has( $id ) { |
| 93 | return $this->container->has( $id ); |
| 94 | } |
| 95 | } |
| 96 |