PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.1
Defender Security – Malware Scanner, Login Security & Firewall v6.2.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / class-container.php

class-container.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.1, at src/component/class-container.php

111 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Dependency injection container.
4 *
5 * @package WP_Defender\Component
6 */
7
8 namespace WP_Defender\Component;
9
10 use ReflectionClass;
11 use RuntimeException;
12 use WP_Defender\Component\Container\Builder;
13
14 /**
15 * Lightweight dependency injection container with constructor autowiring.
16 */
17 class Container {
18
19 /**
20 * Resolved and explicitly registered entries.
21 *
22 * @var array<string, mixed>
23 */
24 private array $entries = array();
25
26 /**
27 * Class instance builder.
28 *
29 * @var Builder
30 */
31 private Builder $builder;
32
33 /**
34 * Register the container itself.
35 */
36 public function __construct() {
37 $this->entries[ self::class ] = $this;
38 $this->builder = new Builder( $this );
39 }
40
41 /**
42 * Find an entry or construct and share a class.
43 *
44 * @template T of object
45 * @param string $id Entry identifier or class name.
46 *
47 * @return mixed
48 * @phpstan-param class-string<T> $id
49 * @phpstan-return T
50 *
51 * @throws RuntimeException Entry does not exist or cannot be resolved.
52 */
53 public function get( string $id ): mixed {
54 if ( array_key_exists( $id, $this->entries ) ) {
55 return $this->entries[ $id ];
56 }
57
58 $entry = $this->builder->build( $id );
59 $this->entries[ $id ] = $entry;
60
61 return $entry;
62 }
63
64 /**
65 * Register or replace an entry.
66 *
67 * @param string $id Entry identifier.
68 * @param mixed $value Entry value.
69 */
70 public function set( string $id, mixed $value ): void {
71 $this->entries[ $id ] = $value;
72 }
73
74 /**
75 * Construct a fresh class instance.
76 *
77 * @template T of object
78 * @param string $id Class name.
79 * @param array<string,mixed> $parameters Named constructor parameters.
80 *
81 * @return object
82 * @phpstan-param class-string<T> $id
83 * @phpstan-return T
84 *
85 * @throws RuntimeException Entry does not exist or cannot be resolved.
86 */
87 public function make( string $id, array $parameters = array() ): object {
88 return $this->builder->build( $id, $parameters );
89 }
90
91 /**
92 * Determine whether an entry is registered or can be instantiated.
93 *
94 * This check does not construct the entry.
95 *
96 * @param string $id Entry identifier or class name.
97 */
98 public function has( string $id ): bool {
99 return array_key_exists( $id, $this->entries ) || $this->is_instantiable( $id );
100 }
101
102 /**
103 * Determine whether a class can be instantiated.
104 *
105 * @param string $id Class name.
106 */
107 private function is_instantiable( string $id ): bool {
108 return class_exists( $id ) && ( new ReflectionClass( $id ) )->isInstantiable();
109 }
110 }
111