-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathModuleInitialization.php
More file actions
249 lines (209 loc) · 6.57 KB
/
ModuleInitialization.php
File metadata and controls
249 lines (209 loc) · 6.57 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* Auto-initialize all Module based classes in the plugin.
*
* @package TenupFramework
*/
declare( strict_types = 1 );
namespace TenupFramework;
use ReflectionClass;
use Spatie\StructureDiscoverer\Cache\FileDiscoverCacheDriver;
use Spatie\StructureDiscoverer\Data\DiscoveredStructure;
use Spatie\StructureDiscoverer\Discover;
/**
* ModuleInitialization class.
*
* @package TenupFramework
*/
class ModuleInitialization {
/**
* The class instance.
*
* @var ?\TenupFramework\ModuleInitialization
*/
private static ?\TenupFramework\ModuleInitialization $instance = null;
/**
* Get the instance of the class.
*/
public static function instance(): \TenupFramework\ModuleInitialization {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Override the constructor, we don't want to init it that way.
*/
private function __construct() {
// no-op. This class is a singleton.
}
/**
* The list of initialized classes.
*
* @var array<ModuleInterface>
*/
protected array $classes = [];
/**
* Get all the TenupFramework plugin classes.
*
* @param string $dir The directory to search for classes.
*
* @return array<string>
*/
public function get_classes( string $dir ): array {
$this->directory_check( $dir );
// Get all classes from this directory and its subdirectories.
$class_finder = Discover::in( $dir );
// Only fetch classes.
$class_finder->classes();
// If we are in production or staging, cache the class loader to improve performance.
// Skip caching if TENUP_FRAMEWORK_DISABLE_CACHE is set to true.
$is_vip_environment = defined( 'VIP_GO_APP_ENVIRONMENT' );
$is_cache_disabled = defined( 'TENUP_FRAMEWORK_DISABLE_CACHE' ) && constant( 'TENUP_FRAMEWORK_DISABLE_CACHE' );
// VIP is a special case that we always want to skip caching for.
if ( $is_vip_environment ) {
$is_cache_disabled = true;
}
$is_production_or_staging_env = in_array( wp_get_environment_type(), [ 'production', 'staging' ], true );
$should_cache = ! $is_vip_environment
&& ! $is_cache_disabled
&& $is_production_or_staging_env;
if ( $should_cache ) {
$class_finder->withCache(
__NAMESPACE__,
new FileDiscoverCacheDriver( $dir . '/class-loader-cache' )
);
}
// @phpstan-ignore-next-line typeCoverage.paramTypeCoverage
$classes = array_filter( $class_finder->get(), fn( $cl ): bool => is_string( $cl ) );
// Return the classes
return $classes;
}
/**
* Check if the directory exists.
*
* @param string $dir The directory to check.
*
* @throws \RuntimeException If the directory does not exist.
*/
protected function directory_check( ?string $dir ): bool {
if ( empty( $dir ) ) {
throw new \RuntimeException( 'Directory is required to initialize classes.' );
}
if ( ! is_dir( $dir ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \RuntimeException( 'Directory "' . $dir . '" does not exist.' );
}
return true;
}
/**
* Initialize all the TenupFramework plugin classes.
*
* @param string $dir The directory to search for classes.
*/
public function init_classes( ?string $dir = '' ): void {
$this->directory_check( $dir );
$load_class_order = [];
// @phpstan-ignore-next-line argument.type
foreach ( $this->get_classes( $dir ) as $class ) {
// Create a slug for the class name.
$slug = $this->slugify_class_name( $class );
// If the class has already been initialized, skip it.
if ( isset( $this->classes[ $slug ] ) ) {
continue;
}
$reflection_class = $this->get_fully_loadable_class( $class );
if ( ! $reflection_class ) {
continue;
}
// Using reflection, check if the class can be initialized.
// If not, skip.
if ( ! $reflection_class->isInstantiable() ) {
continue;
}
// Check if the class implements ModuleInterface before instantiating it
if ( ! $reflection_class->implementsInterface( \TenupFramework\ModuleInterface::class ) ) {
continue;
}
// Initialize the class.
// phpcs:ignore Generic.Commenting.DocComment.MissingShort
/** @var ModuleInterface $instantiated_class */
$instantiated_class = new $class();
do_action( 'tenup_framework_module_init__' . $slug, $instantiated_class );
// Assign the classes into the order they should be initialized.
$load_class_order[ intval( $instantiated_class->load_order() ) ][] = [
'slug' => $slug,
'class' => $instantiated_class,
];
}
// Sort the initialized classes by load order.
ksort( $load_class_order );
// Loop through the classes and initialize them.
foreach ( $load_class_order as $class_objects ) {
foreach ( $class_objects as $class_object ) {
$class = $class_object['class'];
$slug = $class_object['slug'];
// If the class can be registered, register it.
if ( $class->can_register() ) {
// Call its register method.
$class->register();
// Store the class in the list of initialized classes.
$this->classes[ $slug ] = $class;
}
}
}
}
/**
* Retrieves a fully loadable class using reflection.
*
* @param string $class_name The name of the class to load.
*
* @phpstan-ignore missingType.generics
*/
public function get_fully_loadable_class( string $class_name ): false|ReflectionClass {
try {
// Create a new reflection of the class.
// @phpstan-ignore argument.type
return new ReflectionClass( $class_name );
} catch ( \Throwable ) {
// This includes ReflectionException, Error due to missing parent, etc.
return false;
}
}
/**
* Slugify a class name.
*
* @param string $class_name The class name.
*/
protected function slugify_class_name( string $class_name ): string {
return sanitize_title( str_replace( '\\', '-', $class_name ) );
}
/**
* Get a class by its full class name, including namespace.
*
* @param string $class_name The class name & namespace.
*/
public function get_class( string $class_name ): false|ModuleInterface {
$class_name = $this->slugify_class_name( $class_name );
if ( isset( $this->classes[ $class_name ] ) ) {
return $this->classes[ $class_name ];
}
return false;
}
/**
* Get all the initialized classes.
*
* @return array<ModuleInterface>
*/
public function get_all_classes(): array {
return $this->classes;
}
/**
* Get an initialized class by its full class name, including namespace.
*
* @param string $class_name The class name including the namespace.
*/
public static function get_module( string $class_name ): false|ModuleInterface {
return self::instance()->get_class( $class_name );
}
}