2020-06-11 10:35:40 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2020-06-18 12:15:08 +00:00
|
|
|
* Container class file.
|
2020-06-11 10:35:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Automattic\WooCommerce;
|
|
|
|
|
2020-07-02 07:54:49 +00:00
|
|
|
use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProxiesServiceProvider;
|
|
|
|
use Automattic\WooCommerce\Internal\DependencyManagement\ExtendedContainer;
|
2020-06-11 10:35:40 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-18 12:15:08 +00:00
|
|
|
* PSR11 compliant dependency injection container for WooCommerce.
|
2020-06-11 10:35:40 +00:00
|
|
|
*
|
2020-06-18 12:15:08 +00:00
|
|
|
* Classes in the `src` directory should specify dependencies from that directory via constructor arguments
|
|
|
|
* with type hints. If an instance of the container itself is needed, the type hint to use is \Psr\Container\ContainerInterface.
|
2020-06-11 10:35:40 +00:00
|
|
|
*
|
2020-06-18 12:15:08 +00:00
|
|
|
* Classes in the `src` directory should interact with anything outside (especially code in the `includes` directory
|
|
|
|
* and WordPress functions) by using the classes in the `Proxies` directory. The exception is idempotent
|
|
|
|
* functions (e.g. `wp_parse_url`), those can be used directly.
|
2020-06-11 10:35:40 +00:00
|
|
|
*
|
2020-06-18 12:15:08 +00:00
|
|
|
* Classes in the `includes` directory should use the `wc_get_container` function to get the instance of the container when
|
|
|
|
* they need to get an instance of a class from the `src` directory.
|
2020-06-11 10:35:40 +00:00
|
|
|
*
|
2020-06-18 12:15:08 +00:00
|
|
|
* Class registration should be done via service providers that inherit from Automattic\WooCommerce\Tools\DependencyManagement
|
|
|
|
* and those should go in the `src\Tools\DependencyManagement\ServiceProviders` folder unless there's a good reason
|
2020-06-23 12:04:41 +00:00
|
|
|
* to put them elsewhere. All the service provider class names must be in the `SERVICE_PROVIDERS` constant.
|
2020-06-11 10:35:40 +00:00
|
|
|
*/
|
2020-06-18 12:15:08 +00:00
|
|
|
final class Container implements \Psr\Container\ContainerInterface {
|
2020-06-11 10:35:40 +00:00
|
|
|
|
2020-06-23 12:04:41 +00:00
|
|
|
/**
|
|
|
|
* The root namespace of all WooCommerce classes in the `src` directory.
|
|
|
|
*/
|
|
|
|
const WOOCOMMERCE_ROOT_NAMESPACE = 'Automattic\\WooCommerce';
|
|
|
|
|
2020-06-11 10:35:40 +00:00
|
|
|
/**
|
2020-06-18 12:15:08 +00:00
|
|
|
* The list of service provider classes to register.
|
2020-06-11 10:35:40 +00:00
|
|
|
*
|
2020-06-18 12:15:08 +00:00
|
|
|
* @var string[]
|
2020-06-11 10:35:40 +00:00
|
|
|
*/
|
2020-06-18 12:15:08 +00:00
|
|
|
private $service_providers = array(
|
2020-06-22 09:41:11 +00:00
|
|
|
ProxiesServiceProvider::class,
|
2020-06-18 12:15:08 +00:00
|
|
|
);
|
2020-06-11 10:35:40 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-18 12:15:08 +00:00
|
|
|
* The underlying container.
|
2020-06-11 10:35:40 +00:00
|
|
|
*
|
2020-06-22 09:41:11 +00:00
|
|
|
* @var \League\Container\Container
|
2020-06-18 12:15:08 +00:00
|
|
|
*/
|
|
|
|
private $container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class constructor.
|
2020-06-11 10:35:40 +00:00
|
|
|
*/
|
2020-06-18 12:15:08 +00:00
|
|
|
public function __construct() {
|
2020-06-23 12:04:41 +00:00
|
|
|
$this->container = new ExtendedContainer();
|
2020-06-18 12:15:08 +00:00
|
|
|
|
|
|
|
// Add ourselves as the shared instance of ContainerInterface,
|
|
|
|
// register everything else using service providers.
|
|
|
|
|
2020-06-22 09:41:11 +00:00
|
|
|
$this->container->share( \Psr\Container\ContainerInterface::class, $this );
|
2020-06-11 10:35:40 +00:00
|
|
|
|
2020-06-18 12:15:08 +00:00
|
|
|
foreach ( $this->service_providers as $service_provider_class ) {
|
|
|
|
$this->container->addServiceProvider( $service_provider_class );
|
|
|
|
}
|
|
|
|
}
|
2020-06-11 10:35:40 +00:00
|
|
|
|
2020-06-18 12:15:08 +00:00
|
|
|
/**
|
|
|
|
* Finds an entry of the container by its identifier and returns it.
|
|
|
|
*
|
|
|
|
* @param string $id Identifier of the entry to look for.
|
|
|
|
*
|
|
|
|
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
|
|
|
|
* @throws Psr\Container\ContainerExceptionInterface Error while retrieving the entry.
|
|
|
|
*
|
|
|
|
* @return mixed Entry.
|
|
|
|
*/
|
|
|
|
public function get( $id ) {
|
|
|
|
return $this->container->get( $id );
|
2020-06-11 10:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-18 12:15:08 +00:00
|
|
|
* Returns true if the container can return an entry for the given identifier.
|
|
|
|
* Returns false otherwise.
|
|
|
|
*
|
|
|
|
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
|
|
|
|
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
|
|
|
|
*
|
|
|
|
* @param string $id Identifier of the entry to look for.
|
2020-06-11 10:35:40 +00:00
|
|
|
*
|
2020-06-18 12:15:08 +00:00
|
|
|
* @return bool
|
2020-06-11 10:35:40 +00:00
|
|
|
*/
|
2020-06-18 12:15:08 +00:00
|
|
|
public function has( $id ) {
|
|
|
|
return $this->container->has( $id );
|
2020-06-11 10:35:40 +00:00
|
|
|
}
|
|
|
|
}
|