forked from Yoast/wordpress-seo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.php
237 lines (205 loc) · 4.76 KB
/
loader.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
namespace Yoast\WP\SEO;
use WP_CLI;
use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class that manages loading integrations if and only if all their conditionals are met.
*/
class Loader {
/**
* The registered integrations.
*
* @var string[]
*/
protected $integrations = [];
/**
* The registered integrations.
*
* @var string[]
*/
protected $initializers = [];
/**
* The registered routes.
*
* @var string[]
*/
protected $routes = [];
/**
* The registered commands.
*
* @var string[]
*/
protected $commands = [];
/**
* The registered migrations.
*
* @var string[]
*/
protected $migrations = [];
/**
* The dependency injection container.
*
* @var ContainerInterface
*/
protected $container;
/**
* Loader constructor.
*
* @param ContainerInterface $container The dependency injection container.
*/
public function __construct( ContainerInterface $container ) {
$this->container = $container;
}
/**
* Registers an integration.
*
* @param string $class The class name of the integration to be loaded.
*
* @return void
*/
public function register_integration( $class ) {
$this->integrations[] = $class;
}
/**
* Registers an initializer.
*
* @param string $class The class name of the initializer to be loaded.
*
* @return void
*/
public function register_initializer( $class ) {
$this->initializers[] = $class;
}
/**
* Registers a route.
*
* @param string $class The class name of the route to be loaded.
*
* @return void
*/
public function register_route( $class ) {
$this->routes[] = $class;
}
/**
* Registers a command.
*
* @param string $class The class name of the command to be loaded.
*
* @return void
*/
public function register_command( $class ) {
$this->commands[] = $class;
}
/**
* Registers a migration.
*
* @param string $plugin The plugin the migration belongs to.
* @param string $version The version of the migration.
* @param string $class The class name of the migration to be loaded.
*
* @return void
*/
public function register_migration( $plugin, $version, $class ) {
if ( ! \array_key_exists( $plugin, $this->migrations ) ) {
$this->migrations[ $plugin ] = [];
}
$this->migrations[ $plugin ][ $version ] = $class;
}
/**
* Loads all registered classes if their conditionals are met.
*
* @return void
*/
public function load() {
$this->load_initializers();
if ( ! \did_action( 'init' ) ) {
\add_action( 'init', [ $this, 'load_integrations' ] );
}
else {
$this->load_integrations();
}
\add_action( 'rest_api_init', [ $this, 'load_routes' ] );
if ( \defined( 'WP_CLI' ) && \WP_CLI ) {
$this->load_commands();
}
}
/**
* Returns all registered migrations.
*
* @param string $plugin The plugin to get the migrations for.
*
* @return string[]|false The registered migrations. False if no migrations were registered.
*/
public function get_migrations( $plugin ) {
if ( ! \array_key_exists( $plugin, $this->migrations ) ) {
return false;
}
return $this->migrations[ $plugin ];
}
/**
* Loads all registered commands.
*
* @return void
*/
protected function load_commands() {
foreach ( $this->commands as $class ) {
$command = $this->container->get( $class );
WP_CLI::add_command( $class::get_namespace(), $command );
}
}
/**
* Loads all registered initializers if their conditionals are met.
*
* @return void
*/
protected function load_initializers() {
foreach ( $this->initializers as $class ) {
if ( ! $this->conditionals_are_met( $class ) ) {
continue;
}
$this->container->get( $class )->initialize();
}
}
/**
* Loads all registered integrations if their conditionals are met.
*
* @return void
*/
public function load_integrations() {
foreach ( $this->integrations as $class ) {
if ( ! $this->conditionals_are_met( $class ) ) {
continue;
}
$this->container->get( $class )->register_hooks();
}
}
/**
* Loads all registered routes if their conditionals are met.
*
* @return void
*/
public function load_routes() {
foreach ( $this->routes as $class ) {
if ( ! $this->conditionals_are_met( $class ) ) {
continue;
}
$this->container->get( $class )->register_routes();
}
}
/**
* Checks if all conditionals of a given integration are met.
*
* @param Loadable_Interface $class The class name of the integration.
*
* @return bool Whether or not all conditionals of the integration are met.
*/
protected function conditionals_are_met( $class ) {
$conditionals = $class::get_conditionals();
foreach ( $conditionals as $conditional ) {
if ( ! $this->container->get( $conditional )->is_met() ) {
return false;
}
}
return true;
}
}