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