| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class that manages loading integrations if and only if all their conditionals are met. |
| 10 |
*/ |
| 11 |
class Loader { |
| 12 |
|
| 13 |
/** |
| 14 |
* The registered integrations. |
| 15 |
* |
| 16 |
* @var string[] |
| 17 |
*/ |
| 18 |
protected $integrations = []; |
| 19 |
|
| 20 |
/** |
| 21 |
* The registered integrations. |
| 22 |
* |
| 23 |
* @var string[] |
| 24 |
*/ |
| 25 |
protected $initializers = []; |
| 26 |
|
| 27 |
/** |
| 28 |
* The registered routes. |
| 29 |
* |
| 30 |
* @var string[] |
| 31 |
*/ |
| 32 |
protected $routes = []; |
| 33 |
|
| 34 |
/** |
| 35 |
* The registered commands. |
| 36 |
* |
| 37 |
* @var string[] |
| 38 |
*/ |
| 39 |
protected $commands = []; |
| 40 |
|
| 41 |
/** |
| 42 |
* The registered migrations. |
| 43 |
* |
| 44 |
* @var string[] |
| 45 |
*/ |
| 46 |
protected $migrations = []; |
| 47 |
|
| 48 |
/** |
| 49 |
* The dependency injection container. |
| 50 |
* |
| 51 |
* @var ContainerInterface |
| 52 |
*/ |
| 53 |
protected $container; |
| 54 |
|
| 55 |
/** |
| 56 |
* Loader constructor. |
| 57 |
* |
| 58 |
* @param ContainerInterface $container The dependency injection container. |
| 59 |
*/ |
| 60 |
public function __construct( ContainerInterface $container ) { |
| 61 |
$this->container = $container; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Registers an integration. |
| 66 |
* |
| 67 |
* @param string $class The class name of the integration to be loaded. |
| 68 |
* |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function register_integration( $class ) { |
| 72 |
$this->integrations[] = $class; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Registers an initializer. |
| 77 |
* |
| 78 |
* @param string $class The class name of the initializer to be loaded. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function register_initializer( $class ) { |
| 83 |
$this->initializers[] = $class; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Registers a route. |
| 88 |
* |
| 89 |
* @param string $class The class name of the route to be loaded. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function register_route( $class ) { |
| 94 |
$this->routes[] = $class; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Registers a command. |
| 99 |
* |
| 100 |
* @param string $class The class name of the command to be loaded. |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
public function register_command( $class ) { |
| 105 |
$this->commands[] = $class; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Registers a migration. |
| 110 |
* |
| 111 |
* @param string $plugin The plugin the migration belongs to. |
| 112 |
* @param string $version The version of the migration. |
| 113 |
* @param string $class The class name of the migration to be loaded. |
| 114 |
* |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public function register_migration( $plugin, $version, $class ) { |
| 118 |
if ( ! \array_key_exists( $plugin, $this->migrations ) ) { |
| 119 |
$this->migrations[ $plugin ] = []; |
| 120 |
} |
| 121 |
|
| 122 |
$this->migrations[ $plugin ][ $version ] = $class; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Loads all registered classes if their conditionals are met. |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
public function load() { |
| 131 |
$this->load_initializers(); |
| 132 |
|
| 133 |
if ( ! \did_action( 'init' ) ) { |
| 134 |
\add_action( 'init', [ $this, 'load_integrations' ] ); |
| 135 |
} |
| 136 |
else { |
| 137 |
$this->load_integrations(); |
| 138 |
} |
| 139 |
|
| 140 |
\add_action( 'rest_api_init', [ $this, 'load_routes' ] ); |
| 141 |
|
| 142 |
if ( \defined( 'WP_CLI' ) && \WP_CLI ) { |
| 143 |
$this->load_commands(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns all registered migrations. |
| 149 |
* |
| 150 |
* @param string $plugin The plugin to get the migrations for. |
| 151 |
* |
| 152 |
* @return string[]|false The registered migrations. False if no migrations were registered. |
| 153 |
*/ |
| 154 |
public function get_migrations( $plugin ) { |
| 155 |
if ( ! \array_key_exists( $plugin, $this->migrations ) ) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
return $this->migrations[ $plugin ]; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Loads all registered commands. |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
protected function load_commands() { |
| 168 |
foreach ( $this->commands as $class ) { |
| 169 |
$command = $this->container->get( $class ); |
| 170 |
|
| 171 |
WP_CLI::add_command( $class::get_namespace(), $command ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Loads all registered initializers if their conditionals are met. |
| 177 |
* |
| 178 |
* @return void |
| 179 |
*/ |
| 180 |
protected function load_initializers() { |
| 181 |
foreach ( $this->initializers as $class ) { |
| 182 |
if ( ! $this->conditionals_are_met( $class ) ) { |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
$this->container->get( $class )->initialize(); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Loads all registered integrations if their conditionals are met. |
| 192 |
* |
| 193 |
* @return void |
| 194 |
*/ |
| 195 |
public function load_integrations() { |
| 196 |
foreach ( $this->integrations as $class ) { |
| 197 |
if ( ! $this->conditionals_are_met( $class ) ) { |
| 198 |
continue; |
| 199 |
} |
| 200 |
|
| 201 |
$this->container->get( $class )->register_hooks(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Loads all registered routes if their conditionals are met. |
| 207 |
* |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
public function load_routes() { |
| 211 |
foreach ( $this->routes as $class ) { |
| 212 |
if ( ! $this->conditionals_are_met( $class ) ) { |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
$this->container->get( $class )->register_routes(); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Checks if all conditionals of a given integration are met. |
| 222 |
* |
| 223 |
* @param Loadable_Interface $class The class name of the integration. |
| 224 |
* |
| 225 |
* @return bool Whether or not all conditionals of the integration are met. |
| 226 |
*/ |
| 227 |
protected function conditionals_are_met( $class ) { |
| 228 |
$conditionals = $class::get_conditionals(); |
| 229 |
foreach ( $conditionals as $conditional ) { |
| 230 |
if ( ! $this->container->get( $conditional )->is_met() ) { |
| 231 |
return false; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
return true; |
| 236 |
} |
| 237 |
} |
| 238 |
|