PluginProbe
ZIP AI – AI Website Builder & AI Agent (Beta) / 0.0.5
ZIP AI – AI Website Builder & AI Agent (Beta) v0.0.5
0.0.10 0.0.9 trunk 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8
zip-ai / classes / core / container.php

container.php in ZIP AI – AI Website Builder & AI Agent (Beta) 0.0.5, at classes/core/container.php

106 lines 2.2 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 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
24 */
25 protected $bindings = array();
26
27 /**
28 * The shared instances.
29 *
30 * @var array
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 mixed $concrete The concrete implementation or closure.
39 * @param bool $shared Whether the binding is shared (singleton).
40 */
41 public function bind( $abstract, $concrete = null, $shared = false ) {
42 if ( is_null( $concrete ) ) {
43 $concrete = $abstract;
44 }
45
46 $this->bindings[ $abstract ] = array(
47 'concrete' => $concrete,
48 'shared' => $shared,
49 );
50 }
51
52 /**
53 * Register a shared binding in the container.
54 *
55 * @param string $abstract The abstract type or name.
56 * @param mixed $concrete The concrete implementation or closure.
57 */
58 public function singleton( $abstract, $concrete = null ) {
59 $this->bind( $abstract, $concrete, true );
60 }
61
62 /**
63 * Resolve the given type from the container.
64 *
65 * @param string $abstract The abstract type or name.
66 * @return mixed
67 */
68 public function make( $abstract ) {
69 // Return shared instance if it exists.
70 if ( isset( $this->instances[ $abstract ] ) ) {
71 return $this->instances[ $abstract ];
72 }
73
74 // If not bound, try to instantiate directly.
75 if ( ! isset( $this->bindings[ $abstract ] ) ) {
76 return new $abstract();
77 }
78
79 $concrete = $this->bindings[ $abstract ]['concrete'];
80 $object = null;
81
82 if ( $concrete instanceof \Closure ) {
83 $object = $concrete( $this );
84 } else {
85 $object = $this->make( $concrete );
86 }
87
88 // Save shared instance.
89 if ( $this->bindings[ $abstract ]['shared'] ) {
90 $this->instances[ $abstract ] = $object;
91 }
92
93 return $object;
94 }
95
96 /**
97 * Check if the given abstract type has been bound.
98 *
99 * @param string $abstract The abstract type or name.
100 * @return bool
101 */
102 public function has( $abstract ) {
103 return isset( $this->bindings[ $abstract ] ) || isset( $this->instances[ $abstract ] );
104 }
105 }
106