| 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 |
|