-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-container.php
213 lines (163 loc) · 5.37 KB
/
class-container.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* The main Container class!
*
* This class is a basic implementation of a Container. It is inspired by PSR-11
* Container Interface but with only the things stripped down with a bare
* minimum feature for maximum compatibility within the WordPress ecosystem!
*
* This Container is written as a stand-alone "dwtfyw" class for WordPress.
*
* If you're a plugin developer, just copy and paste this class and include it
* in your bootstrap file or whatever. Just remember to rename the
* namespace to avoid conflicts or name collisions with other code.
*
* Inspired from various PSR-11 implementations.
*
* @package DIC\WP
*/
namespace DIC\WP;
/**
* Feel free to modify this file especially the namespace above ☝☝☝ for your own needs.
*
* @version 0.0.1
*/
class Container {
/**
* The fully qualified class names entries.
*
* @var array $entries
**/
private $entries = array();
/**
* Retrieve a specific instance of the class from the Container's entries.
*
* @param string $class_name The fully qualified class name of the an object.
*
* @return mixed The specific instance of the class found from the entries.
*/
public function get( $class_name = '' ) {
$cache_entries = wp_cache_get( self::class, self::class . '_group' );
if ( ! empty( $cache_entries[ $class_name ] ) ) {
return $cache_entries[ $class_name ]( $this );
}
if ( $this->has( $class_name ) ) {
return $this->entries[ $class_name ]( $this );
}
return $this->resolve( $class_name );
}
/**
* Determine if the container entries contains the specific class name.
*
* @param string $class_name The fully qualified class name of the an object.
*
* @return bool True if the class already exists. Returns false, otherwise.
*/
public function has( $class_name = '' ) {
return isset( $this->entries[ $class_name ] );
}
/**
* Sets a specific class dependency from the return value of the second callable parameter.
*
* @param string $class_name The class name to resolve.
* @param callable $func The return function to call when resolving the class.
*
* @throws \Exception When an invalid parameter is passed.
*
* @return self
*/
public function set( $class_name = '', callable $func = null ) {
if ( empty( $class_name ) ) {
throw new \Exception( 'Class name cannot be empty.' );
}
if ( empty( $func ) || ! is_callable( $func ) ) {
throw new \Exception( 'The second parameter $func must be callable type' );
}
$this->entries[ $class_name ] = $func;
wp_cache_set( self::class, $this->entries, self::class . '_group' );
return $this;
}
/**
* Sets specific class dependencies using array.
*
* @param array $definitions E.g. [ MyClass::class => fn(), ... ].
*
* @return self;
*/
public function set_definitions( $definitions = array() ) {
foreach ( $definitions as $class => $callback ) {
$this->set( $class, $callback );
}
return $this;
}
/**
* Recursively resolves constructor arguments from dependencies.
*
* @throws \Exception When the container could not resolve any depedency.
*
* @param string $class_name The fully qualified class name.
*/
public function resolve( $class_name = '' ) {
$reflection_class = new \ReflectionClass( $class_name );
$constructor = $reflection_class->getConstructor();
if ( ! $constructor ) {
return new $class_name();
}
if ( ! $constructor->isPublic() ) {
throw new \Exception( 'Unsupported non-public constructor method for the class ' . $class_name . ' ', 500 );
}
$parameters = $constructor->getParameters();
if ( ! $parameters ) {
return new $class_name();
}
$dependencies = array_map( array( $this, $this->get_resolver() ), $parameters );
return $reflection_class->newInstanceArgs( $dependencies );
}
/**
* Retrieves the resolver based on the version of the PHP installed.
*
* @return string The resolver method.
*/
protected function get_resolver() {
if ( version_compare( PHP_VERSION, '7.0.0', '>=' ) ) {
return 'php_7_resolver';
}
return 'php_5_resolver';
}
/**
* PHP 5 resolver.
*
* @param object $param The parameters, as a ReflectionParameter objects.
*
* @throws \Exception When an error has occured.
* @return mixed The instance of the class from the entries.
*/
protected function php_5_resolver( $param ) {
if ( empty( $param->getClass() ) ) {
throw new \Exception( 'Cannot resolve constructor parameter with no type hint $' . $param->getName(), 500 );
}
return $this->get( $param->getClass()->getName() );
}
/**
* PHP 7 resolver.
*
* @param object $param — The parameters, as a ReflectionParameter objects.
*
* @throws \Exception - When an error has occured.
*
* @return mixed The instance of the class from the entries.
*/
protected function php_7_resolver( $param ) {
$type = $param->getType();
if ( ! $type ) {
throw new \Exception( 'Cannot resolve constructor dependencies from ' . $param->getClass()->getName(), 500 );
}
if ( class_exists( '\ReflectionUnionType' ) && $type instanceof \ReflectionUnionType ) {
throw new \Exception( 'Cannot resolve constructor dependencies of union types from ' . $param->getClass()->getName(), 500 );
}
if ( $type instanceof \ReflectionNamedType && ! $type->isBuiltin() ) {
return $this->get( $type->getName() );
}
throw new \Exception( 'Cannot resolve dependencies. Unknown error has occured.', 500 );
}
}