PluginProbe
WooCommerce / 11.1.0-rc.2
WooCommerce v11.1.0-rc.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Container.php
Container.php
83 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @template T of object
59 * @param string $id Class name.
60 * @phpstan-param class-string<T> $id
61 *
62 * @return T Object instance.
63 *
64 * @throws ContainerException Error when resolving the class to an object instance, or class not found.
65 * @throws \Exception Exception thrown in the constructor or in the 'init' method of one of the resolved classes.
66 */
67 public function get( string $id ) {
68 return $this->container->get( $id );
69 }
70
71 /**
72 * Returns true if the container can return an instance of the given class or false otherwise.
73 * See the comment in RuntimeContainer::has.
74 *
75 * @param class-string $id Class name.
76 *
77 * @return bool
78 */
79 public function has( string $id ): bool {
80 return $this->container->has( $id );
81 }
82 }
83