| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dependency Injection Container. |
| 4 |
* |
| 5 |
* @package zip-ai |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ZipAI\MCP\Classes\Core; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Container Class. |
| 17 |
*/ |
| 18 |
class Container { |
| 19 |
|
| 20 |
/** |
| 21 |
* The bindings. |
| 22 |
* |
| 23 |
* @var array<string,array{concrete:\Closure|string,shared:bool}> |
| 24 |
*/ |
| 25 |
protected $bindings = array(); |
| 26 |
|
| 27 |
/** |
| 28 |
* The shared instances. |
| 29 |
* |
| 30 |
* @var array<string,object> |
| 31 |
*/ |
| 32 |
protected $instances = array(); |
| 33 |
|
| 34 |
/** |
| 35 |
* Register a binding with the container. |
| 36 |
* |
| 37 |
* @param string $abstract The abstract type or name. |
| 38 |
* @param \Closure|string|null $concrete The concrete implementation or closure. |
| 39 |
* @param bool $shared Whether the binding is shared (singleton). |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function bind( $abstract, $concrete = null, $shared = false ) { |
| 43 |
if ( is_null( $concrete ) ) { |
| 44 |
$concrete = $abstract; |
| 45 |
} |
| 46 |
|
| 47 |
$this->bindings[ $abstract ] = array( |
| 48 |
'concrete' => $concrete, |
| 49 |
'shared' => $shared, |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Register a shared binding in the container. |
| 55 |
* |
| 56 |
* @param string $abstract The abstract type or name. |
| 57 |
* @param \Closure|string|null $concrete The concrete implementation or closure. |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function singleton( $abstract, $concrete = null ) { |
| 61 |
$this->bind( $abstract, $concrete, true ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Resolve the given type from the container. |
| 66 |
* |
| 67 |
* @param string $abstract The abstract type or name. |
| 68 |
* @return mixed |
| 69 |
*/ |
| 70 |
public function make( $abstract ) { |
| 71 |
// Return shared instance if it exists. |
| 72 |
if ( isset( $this->instances[ $abstract ] ) ) { |
| 73 |
return $this->instances[ $abstract ]; |
| 74 |
} |
| 75 |
|
| 76 |
// If not bound, try to instantiate directly. |
| 77 |
if ( ! isset( $this->bindings[ $abstract ] ) ) { |
| 78 |
return new $abstract(); |
| 79 |
} |
| 80 |
|
| 81 |
$concrete = $this->bindings[ $abstract ]['concrete']; |
| 82 |
$object = null; |
| 83 |
|
| 84 |
if ( $concrete instanceof \Closure ) { |
| 85 |
$object = $concrete( $this ); |
| 86 |
} else { |
| 87 |
$object = $this->make( $concrete ); |
| 88 |
} |
| 89 |
|
| 90 |
// Save shared instance. |
| 91 |
if ( $this->bindings[ $abstract ]['shared'] ) { |
| 92 |
/** |
| 93 |
* Narrowed type for `$object`. |
| 94 |
* |
| 95 |
* @var object $object |
| 96 |
*/ |
| 97 |
$this->instances[ $abstract ] = $object; |
| 98 |
} |
| 99 |
|
| 100 |
return $object; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Check if the given abstract type has been bound. |
| 105 |
* |
| 106 |
* @param string $abstract The abstract type or name. |
| 107 |
* @return bool |
| 108 |
*/ |
| 109 |
public function has( $abstract ) { |
| 110 |
return isset( $this->bindings[ $abstract ] ) || isset( $this->instances[ $abstract ] ); |
| 111 |
} |
| 112 |
} |
| 113 |
|