Admin
1 year ago
Blocks
1 year ago
Caches
11 months ago
Caching
1 year ago
Checkout
1 year ago
Database
1 year ago
Enums
1 year ago
Internal
11 months ago
LayoutTemplates
2 years ago
Proxies
2 years ago
StoreApi
1 year ago
Utilities
1 year ago
Autoloader.php
1 year ago
Container.php
1 year ago
Deprecated.php
1 year ago
Packages.php
1 year ago
Container.php
185 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\Admin\Features\Features; |
| 11 | use Automattic\WooCommerce\Internal\DependencyManagement\ContainerException; |
| 12 | use Automattic\WooCommerce\Internal\DependencyManagement\ExtendedContainer; |
| 13 | use Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer; |
| 14 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\AddressProviderServiceProvider; |
| 15 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\AdminSettingsServiceProvider; |
| 16 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\CostOfGoodsSoldServiceProvider; |
| 17 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\COTMigrationServiceProvider; |
| 18 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\DownloadPermissionsAdjusterServiceProvider; |
| 19 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\AssignDefaultCategoryServiceProvider; |
| 20 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\EmailPreviewServiceProvider; |
| 21 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\EnginesServiceProvider; |
| 22 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\FeaturesServiceProvider; |
| 23 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\LoggingServiceProvider; |
| 24 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\MarketingServiceProvider; |
| 25 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\MarketplaceServiceProvider; |
| 26 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\OrdersControllersServiceProvider; |
| 27 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\OrderAdminServiceProvider; |
| 28 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\OrderMetaBoxServiceProvider; |
| 29 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ObjectCacheServiceProvider; |
| 30 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\OrdersDataStoreServiceProvider; |
| 31 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\OptionSanitizerServiceProvider; |
| 32 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\OrderAttributionServiceProvider; |
| 33 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProductAttributesLookupServiceProvider; |
| 34 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProductDownloadsServiceProvider; |
| 35 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProductImageBySKUServiceProvider; |
| 36 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProductReviewsServiceProvider; |
| 37 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProxiesServiceProvider; |
| 38 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\RestockRefundedItemsAdjusterServiceProvider; |
| 39 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\AdminSuggestionsServiceProvider; |
| 40 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\UtilsClassesServiceProvider; |
| 41 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\BatchProcessingServiceProvider; |
| 42 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\LayoutTemplatesServiceProvider; |
| 43 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ComingSoonServiceProvider; |
| 44 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\StatsServiceProvider; |
| 45 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ImportExportServiceProvider; |
| 46 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\EmailEditorServiceProvider; |
| 47 | use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProductFiltersServiceProvider; |
| 48 | |
| 49 | /** |
| 50 | * PSR11 compliant dependency injection container for WooCommerce. |
| 51 | * |
| 52 | * Classes in the `src` directory should specify dependencies from that directory via an 'init' method having arguments |
| 53 | * with type hints. If an instance of the container itself is needed, the type hint to use is \Psr\Container\ContainerInterface. |
| 54 | * |
| 55 | * Classes in the `src` directory should interact with anything outside (especially code in the `includes` directory |
| 56 | * and WordPress functions) by using the classes in the `Proxies` directory. The exception is idempotent |
| 57 | * functions (e.g. `wp_parse_url`), those can be used directly. |
| 58 | * |
| 59 | * Classes in the `includes` directory should use the `wc_get_container` function to get the instance of the container when |
| 60 | * they need to get an instance of a class from the `src` directory. |
| 61 | * |
| 62 | * Class registration should be done via service providers that inherit from Automattic\WooCommerce\Internal\DependencyManagement |
| 63 | * and those should go in the `src\Internal\DependencyManagement\ServiceProviders` folder unless there's a good reason |
| 64 | * to put them elsewhere. All the service provider class names must be in the `SERVICE_PROVIDERS` constant. |
| 65 | * |
| 66 | * IMPORTANT NOTE: By default an instance of RuntimeContainer will be used as the underlying container, |
| 67 | * but it's possible to use the old ExtendedContainer (backed by the PHP League's container package) instead, |
| 68 | * see RuntimeContainer::should_use() for configuration instructions. |
| 69 | * The League's container, the ExtendedContainer class and the related support code will be removed in WooCommerce 10.0. |
| 70 | */ |
| 71 | final class Container { |
| 72 | /** |
| 73 | * The underlying container. |
| 74 | * |
| 75 | * @var RuntimeContainer |
| 76 | */ |
| 77 | private $container; |
| 78 | |
| 79 | /** |
| 80 | * Class constructor. |
| 81 | */ |
| 82 | public function __construct() { |
| 83 | if ( RuntimeContainer::should_use() ) { |
| 84 | // When the League container was in use we allowed to retrieve the container itself |
| 85 | // by using 'Psr\Container\ContainerInterface' as the class identifier, |
| 86 | // we continue allowing that for compatibility. |
| 87 | $this->container = new RuntimeContainer( |
| 88 | array( |
| 89 | __CLASS__ => $this, |
| 90 | 'Psr\Container\ContainerInterface' => $this, |
| 91 | ) |
| 92 | ); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | $this->container = new ExtendedContainer(); |
| 97 | |
| 98 | // Add ourselves as the shared instance of ContainerInterface, |
| 99 | // register everything else using service providers. |
| 100 | |
| 101 | $this->container->share( __CLASS__, $this ); |
| 102 | |
| 103 | foreach ( $this->get_service_providers() as $service_provider_class ) { |
| 104 | $this->container->addServiceProvider( $service_provider_class ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Finds an entry of the container by its identifier and returns it. |
| 110 | * See the comment about ContainerException in RuntimeContainer::get. |
| 111 | * |
| 112 | * @template T |
| 113 | * @param string|class-string<T> $id Identifier of the entry to look for. |
| 114 | * |
| 115 | * @return T Resolved entry. |
| 116 | * |
| 117 | * @throws NotFoundExceptionInterface No entry was found for the supplied identifier (only when using ExtendedContainer). |
| 118 | * @throws Psr\Container\ContainerExceptionInterface Error while retrieving the entry. |
| 119 | * @throws ContainerException Error when resolving the class to an object instance, or (when using RuntimeContainer) class not found. |
| 120 | * @throws \Exception Exception thrown in the constructor or in the 'init' method of one of the resolved classes. |
| 121 | */ |
| 122 | public function get( string $id ) { |
| 123 | return $this->container->get( $id ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns true if the container can return an entry for the given identifier. |
| 128 | * Returns false otherwise. |
| 129 | * |
| 130 | * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
| 131 | * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
| 132 | * |
| 133 | * @param class-string $id Identifier of the entry to look for. |
| 134 | * |
| 135 | * @return bool |
| 136 | */ |
| 137 | public function has( string $id ): bool { |
| 138 | return $this->container->has( $id ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * The list of service provider classes to register. |
| 143 | * |
| 144 | * @return array<int,class-string> |
| 145 | */ |
| 146 | private function get_service_providers(): array { |
| 147 | return array( |
| 148 | AssignDefaultCategoryServiceProvider::class, |
| 149 | DownloadPermissionsAdjusterServiceProvider::class, |
| 150 | EmailPreviewServiceProvider::class, |
| 151 | OptionSanitizerServiceProvider::class, |
| 152 | OrdersDataStoreServiceProvider::class, |
| 153 | ProductAttributesLookupServiceProvider::class, |
| 154 | ProductDownloadsServiceProvider::class, |
| 155 | ProductImageBySKUServiceProvider::class, |
| 156 | ProductReviewsServiceProvider::class, |
| 157 | ProxiesServiceProvider::class, |
| 158 | RestockRefundedItemsAdjusterServiceProvider::class, |
| 159 | UtilsClassesServiceProvider::class, |
| 160 | COTMigrationServiceProvider::class, |
| 161 | OrdersControllersServiceProvider::class, |
| 162 | OrderAttributionServiceProvider::class, |
| 163 | ObjectCacheServiceProvider::class, |
| 164 | BatchProcessingServiceProvider::class, |
| 165 | OrderMetaBoxServiceProvider::class, |
| 166 | OrderAdminServiceProvider::class, |
| 167 | FeaturesServiceProvider::class, |
| 168 | MarketingServiceProvider::class, |
| 169 | MarketplaceServiceProvider::class, |
| 170 | LayoutTemplatesServiceProvider::class, |
| 171 | LoggingServiceProvider::class, |
| 172 | EnginesServiceProvider::class, |
| 173 | ComingSoonServiceProvider::class, |
| 174 | StatsServiceProvider::class, |
| 175 | ImportExportServiceProvider::class, |
| 176 | CostOfGoodsSoldServiceProvider::class, |
| 177 | AdminSettingsServiceProvider::class, |
| 178 | AdminSuggestionsServiceProvider::class, |
| 179 | EmailEditorServiceProvider::class, |
| 180 | ProductFiltersServiceProvider::class, |
| 181 | AddressProviderServiceProvider::class, |
| 182 | ); |
| 183 | } |
| 184 | } |
| 185 |