Repository navigation
Expand file tree
/
Copy pathbootstrap.php
More file actions
186 lines (156 loc) · 5.38 KB
/
Copy pathbootstrap.php
File metadata and controls
186 lines (156 loc) · 5.38 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
<?php
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'EpmLoader', false ) ) {
final class EpmLoader {
/**
* Registered EPM version candidates.
*
* @var array<string, array{version: string, path: string}>
*/
private static array $candidates = array();
/**
* Registered product namespace mappings.
*
* @var array<string, string>
*/
private static array $products = array();
/**
* Whether the finalization hook has been registered.
*/
private static bool $hook_registered = false;
/**
* Whether the autoloader has been initialized.
*/
private static bool $initialized = false;
/**
* Register an EPM version candidate and product namespace.
*
* @param string $version EPM version.
* @param string $namespace Product namespace.
* @param string $plugin_path Path to the product's root directory.
*
* @return void
*/
public static function register(
string $version,
string $namespace,
string $plugin_path
): void {
if ( self::$initialized ) {
return;
}
$namespace = trim( $namespace, '\\' ) . '\\';
$plugin_path = realpath( $plugin_path );
if ( false === $plugin_path ) {
return;
}
$epm_path = realpath( $plugin_path . '/epm' );
$product_path = realpath( $plugin_path . '/src' );
if (
false === $epm_path
|| false === $product_path
|| ! is_dir( $epm_path . '/src' )
|| ! is_dir( $product_path )
|| ! preg_match(
'/^\\d+\\.\\d+\\.\\d+(?:-[0-9A-Za-z.-]+)?(?:\\+[0-9A-Za-z.-]+)?$/',
$version
)
|| '' === $namespace
) {
return;
}
self::$candidates[ $epm_path ] = array(
'version' => $version,
'path' => $epm_path,
);
self::$products[ $namespace ] = $product_path;
if ( self::$hook_registered ) {
return;
}
self::$hook_registered = true;
add_action(
'after_setup_theme',
array( self::class, 'initialize' ),
PHP_INT_MAX
);
}
/**
* Initialize the autoloader using the highest registered EPM version.
*
* @return void
*/
public static function initialize(): void {
if ( self::$initialized || empty( self::$candidates ) ) {
return;
}
$candidates = array_values( self::$candidates );
usort(
$candidates,
static function ( array $a, array $b ): int {
return version_compare( $b['version'], $a['version'] );
}
);
$selected = $candidates[0];
$epm_src = $selected['path'] . '/src';
if ( ! is_dir( $epm_src ) ) {
return;
}
self::$initialized = true;
spl_autoload_register(
static function ( string $class ) use ( $epm_src ): void {
self::load_class( $class, array(
'EPM\\' => $epm_src,
) );
}
);
foreach ( self::$products as $namespace => $src ) {
spl_autoload_register(
static function ( string $class ) use ( $namespace, $src ): void {
self::load_class( $class, array(
$namespace => $src,
) );
}
);
}
}
/**
* Load a class using a namespace-to-directory mapping.
*
* @param string $class Fully qualified class name.
* @param array<string,string> $prefixes Namespace prefixes and source directories.
*
* @return void
*/
private static function load_class( string $class, array $prefixes ): void {
foreach ( $prefixes as $prefix => $directory ) {
if ( 0 !== strpos( $class, $prefix ) ) {
continue;
}
$relative_class = substr( $class, strlen( $prefix ) );
$relative_path = str_replace( '\\', DIRECTORY_SEPARATOR, $relative_class );
$file = $directory . DIRECTORY_SEPARATOR . $relative_path . '.php';
if ( is_file( $file ) && is_readable( $file ) ) {
require_once $file;
}
return;
}
}
}
add_action( 'wp_loaded', static function (): void {
EPM\App::get_instance()->run();
} );
}
/*
* Register the loader from the product's main plugin file, not here.
*
* The product must keep its directories named /epm and /src.
* Pass only the product root path; the loader resolves both directories internally.
*
* Example (in the main plugin file):
*
* EpmLoader::register(
* '1.4.0',
* 'Ephpicman\\MyPlugin\\',
* __DIR__
* );
*/