| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Settings; |
| 4 |
|
| 5 |
use MediaWiki\Config\Config; |
| 6 |
use MediaWiki\Config\HashConfig; |
| 7 |
use MediaWiki\Config\IterableConfig; |
| 8 |
use MediaWiki\HookContainer\HookContainer; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Registration\ExtensionRegistry; |
| 11 |
use MediaWiki\Settings\Cache\CacheableSource; |
| 12 |
use MediaWiki\Settings\Cache\CachedSource; |
| 13 |
use MediaWiki\Settings\Config\ConfigBuilder; |
| 14 |
use MediaWiki\Settings\Config\ConfigSchema; |
| 15 |
use MediaWiki\Settings\Config\ConfigSchemaAggregator; |
| 16 |
use MediaWiki\Settings\Config\GlobalConfigBuilder; |
| 17 |
use MediaWiki\Settings\Config\PhpIniSink; |
| 18 |
use MediaWiki\Settings\Source\ArraySource; |
| 19 |
use MediaWiki\Settings\Source\FileSource; |
| 20 |
use MediaWiki\Settings\Source\SettingsFileUtils; |
| 21 |
use MediaWiki\Settings\Source\SettingsIncludeLocator; |
| 22 |
use MediaWiki\Settings\Source\SettingsSource; |
| 23 |
use RuntimeException; |
| 24 |
use StatusValue; |
| 25 |
use Wikimedia\ObjectCache\BagOStuff; |
| 26 |
use function array_key_exists; |
| 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 |
class SettingsBuilder { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private const STAGE_LOADING = 1; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private const STAGE_REGISTRATION = 10; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
private const STAGE_READ_ONLY = 100; |
| 74 |
|
| 75 |
|
| 76 |
private $baseDir; |
| 77 |
|
| 78 |
|
| 79 |
private $extensionRegistry; |
| 80 |
|
| 81 |
|
| 82 |
private $cache; |
| 83 |
|
| 84 |
|
| 85 |
private $configSink; |
| 86 |
|
| 87 |
|
| 88 |
private $obsoleteConfig; |
| 89 |
|
| 90 |
|
| 91 |
private $config; |
| 92 |
|
| 93 |
|
| 94 |
private $currentBatch; |
| 95 |
|
| 96 |
|
| 97 |
private $configSchema; |
| 98 |
|
| 99 |
|
| 100 |
private $phpIniSink; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
private $settingsConfig; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private $stage = self::STAGE_LOADING; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
private $defaultsNeedMerging = false; |
| 131 |
|
| 132 |
|
| 133 |
private $warnings = []; |
| 134 |
|
| 135 |
private static bool $accessDisabledForUnitTests = false; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public static function getInstance(): self { |
| 147 |
static $instance = null; |
| 148 |
|
| 149 |
if ( self::$accessDisabledForUnitTests ) { |
| 150 |
throw new RuntimeException( 'Access is disabled in unit tests' ); |
| 151 |
} |
| 152 |
|
| 153 |
if ( !$instance ) { |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
$instance = new SettingsBuilder( |
| 160 |
MW_INSTALL_PATH, |
| 161 |
ExtensionRegistry::getInstance(), |
| 162 |
new GlobalConfigBuilder( 'wg' ), |
| 163 |
new PhpIniSink() |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
return $instance; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public static function disableAccessForUnitTests(): void { |
| 174 |
if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
| 175 |
throw new RuntimeException( 'Can only be called in tests' ); |
| 176 |
} |
| 177 |
self::$accessDisabledForUnitTests = true; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public static function enableAccessAfterUnitTests(): void { |
| 184 |
if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
| 185 |
throw new RuntimeException( 'Can only be called in tests' ); |
| 186 |
} |
| 187 |
self::$accessDisabledForUnitTests = false; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public function __construct( |
| 201 |
string $baseDir, |
| 202 |
ExtensionRegistry $extensionRegistry, |
| 203 |
ConfigBuilder $configSink, |
| 204 |
PhpIniSink $phpIniSink, |
| 205 |
?BagOStuff $cache = null |
| 206 |
) { |
| 207 |
$this->baseDir = $baseDir; |
| 208 |
$this->extensionRegistry = $extensionRegistry; |
| 209 |
$this->cache = $cache; |
| 210 |
$this->configSink = $configSink; |
| 211 |
$this->obsoleteConfig = []; |
| 212 |
$this->configSchema = new ConfigSchemaAggregator(); |
| 213 |
$this->phpIniSink = $phpIniSink; |
| 214 |
$this->settingsConfig = [ |
| 215 |
MainConfigNames::ExtensionDirectory => "$baseDir/extensions", |
| 216 |
MainConfigNames::StyleDirectory => "$baseDir/skins", |
| 217 |
]; |
| 218 |
$this->reset(); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public function load( SettingsSource $source ): self { |
| 229 |
$this->assertStillLoading( __METHOD__ ); |
| 230 |
|
| 231 |
|
| 232 |
$this->currentBatch[] = $this->wrapSource( $source ); |
| 233 |
|
| 234 |
return $this; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public function loadArray( array $newSettings ): self { |
| 245 |
return $this->load( new ArraySource( $newSettings ) ); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private function loadArrayInternal( array $newSettings, string $func ): self { |
| 258 |
$this->assertNotReadOnly( $func ); |
| 259 |
|
| 260 |
$source = new ArraySource( $newSettings ); |
| 261 |
$this->currentBatch[] = $this->wrapSource( $source ); |
| 262 |
|
| 263 |
return $this; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public function loadFile( string $path ): self { |
| 273 |
return $this->load( $this->makeSource( $path ) ); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public function fileExists( string $path ): bool { |
| 284 |
$path = SettingsFileUtils::resolveRelativeLocation( $path, $this->baseDir ); |
| 285 |
return file_exists( $path ); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
private function wrapSource( SettingsSource $source ): SettingsSource { |
| 294 |
if ( $this->cache !== null && $source instanceof CacheableSource ) { |
| 295 |
$source = new CachedSource( $this->cache, $source ); |
| 296 |
} |
| 297 |
return $source; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
private function makeSource( $location ): SettingsSource { |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
$path = SettingsFileUtils::resolveRelativeLocation( $location, $this->baseDir ); |
| 309 |
|
| 310 |
return $this->wrapSource( new FileSource( $path ) ); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public function validate(): StatusValue { |
| 321 |
$config = $this->getConfig(); |
| 322 |
return $this->configSchema->validateConfig( $config ); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public function detectDeprecatedConfig(): array { |
| 339 |
$config = $this->getConfig(); |
| 340 |
$keys = $this->getDefinedConfigKeys(); |
| 341 |
$deprecated = []; |
| 342 |
|
| 343 |
foreach ( $keys as $key ) { |
| 344 |
$sch = $this->configSchema->getSchemaFor( $key ); |
| 345 |
if ( !isset( $sch['deprecated'] ) ) { |
| 346 |
continue; |
| 347 |
} |
| 348 |
|
| 349 |
$default = $sch['default'] ?? null; |
| 350 |
$value = $config->get( $key ); |
| 351 |
|
| 352 |
if ( $value !== $default ) { |
| 353 |
$deprecated[$key] = $sch['deprecated']; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
return $deprecated; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
public function detectObsoleteConfig(): array { |
| 373 |
$config = $this->getConfig(); |
| 374 |
$obsolete = []; |
| 375 |
|
| 376 |
foreach ( $this->obsoleteConfig as $key => $msg ) { |
| 377 |
if ( $config->has( $key ) ) { |
| 378 |
$obsolete[$key] = $msg; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
return $obsolete; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public function getDefaultConfig(): IterableConfig { |
| 394 |
$this->apply(); |
| 395 |
$defaults = $this->configSchema->getDefaults(); |
| 396 |
$nulls = array_fill_keys( $this->configSchema->getDefinedKeys(), null ); |
| 397 |
|
| 398 |
return new HashConfig( array_merge( $nulls, $defaults ) ); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public function getConfigSchema(): ConfigSchema { |
| 409 |
$this->apply(); |
| 410 |
return $this->configSchema; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public function getDefinedConfigKeys(): array { |
| 419 |
$this->apply(); |
| 420 |
return $this->configSchema->getDefinedKeys(); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public function apply(): self { |
| 433 |
if ( !$this->currentBatch ) { |
| 434 |
return $this; |
| 435 |
} |
| 436 |
|
| 437 |
$this->assertNotReadOnly( __METHOD__ ); |
| 438 |
$this->config = null; |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
$allSettings = $this->loadRecursive( $this->currentBatch ); |
| 443 |
|
| 444 |
foreach ( $allSettings as $settings ) { |
| 445 |
$this->applySettings( $settings ); |
| 446 |
} |
| 447 |
$this->reset(); |
| 448 |
return $this; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
private function loadRecursive( array $batch, array $stack = [] ): array { |
| 460 |
$allSettings = []; |
| 461 |
|
| 462 |
|
| 463 |
foreach ( $batch as $source ) { |
| 464 |
$sourceName = (string)$source; |
| 465 |
|
| 466 |
if ( in_array( $sourceName, $stack ) ) { |
| 467 |
throw new SettingsBuilderException( |
| 468 |
'Recursive include chain detected: ' . implode( ', ', $stack ) |
| 469 |
); |
| 470 |
} |
| 471 |
|
| 472 |
$settings = $source->load(); |
| 473 |
$settings['source-name'] = $sourceName; |
| 474 |
|
| 475 |
$allSettings[] = $settings; |
| 476 |
|
| 477 |
$nextBatch = []; |
| 478 |
foreach ( $settings['includes'] ?? [] as $location ) { |
| 479 |
|
| 480 |
|
| 481 |
if ( $source instanceof SettingsIncludeLocator ) { |
| 482 |
$location = $source->locateInclude( $location ); |
| 483 |
} |
| 484 |
|
| 485 |
$nextBatch[] = $this->makeSource( $location ); |
| 486 |
} |
| 487 |
|
| 488 |
$nextStack = array_merge( $stack, [ $settings['source-name'] ] ); |
| 489 |
$nextSettings = $this->loadRecursive( $nextBatch, $nextStack ); |
| 490 |
$allSettings = array_merge( $allSettings, $nextSettings ); |
| 491 |
} |
| 492 |
|
| 493 |
return $allSettings; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
private function updateSettingsConfig( $config ): string { |
| 504 |
|
| 505 |
foreach ( $this->settingsConfig as $key => $dummy ) { |
| 506 |
if ( array_key_exists( $key, $config ) ) { |
| 507 |
$this->settingsConfig[ $key ] = $config[ $key ]; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
return $key; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
public function assumeDirtyConfig(): SettingsBuilder { |
| 530 |
$this->defaultsNeedMerging = true; |
| 531 |
return $this; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
private function applySchemas( array $settings ) { |
| 547 |
$defaults = []; |
| 548 |
|
| 549 |
if ( isset( $settings['config-schema-inverse'] ) ) { |
| 550 |
$defaults = $settings['config-schema-inverse']['default'] ?? []; |
| 551 |
$this->configSchema->addDefaults( |
| 552 |
$defaults, |
| 553 |
$settings['source-name'] |
| 554 |
); |
| 555 |
$this->configSchema->addMergeStrategies( |
| 556 |
$settings['config-schema-inverse']['mergeStrategy'] ?? [], |
| 557 |
$settings['source-name'] |
| 558 |
); |
| 559 |
$this->configSchema->addTypes( |
| 560 |
$settings['config-schema-inverse']['type'] ?? [], |
| 561 |
$settings['source-name'] |
| 562 |
); |
| 563 |
$this->configSchema->addDynamicDefaults( |
| 564 |
$settings['config-schema-inverse']['dynamicDefault'] ?? [], |
| 565 |
$settings['source-name'] |
| 566 |
); |
| 567 |
} |
| 568 |
|
| 569 |
if ( isset( $settings['config-schema'] ) ) { |
| 570 |
foreach ( $settings['config-schema'] as $key => $schema ) { |
| 571 |
$this->configSchema->addSchema( $key, $schema ); |
| 572 |
|
| 573 |
if ( $this->configSchema->hasDefaultFor( $key ) ) { |
| 574 |
$defaults[$key] = $this->configSchema->getDefaultFor( $key ); |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
if ( $this->defaultsNeedMerging ) { |
| 580 |
$mergeStrategies = $this->configSchema->getMergeStrategies(); |
| 581 |
$this->configSink->setMultiDefault( $defaults, $mergeStrategies ); |
| 582 |
} else { |
| 583 |
|
| 584 |
$this->configSink->setMulti( $defaults ); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
private function applySettings( array $settings ) { |
| 594 |
|
| 595 |
|
| 596 |
if ( isset( $settings['config'] ) ) { |
| 597 |
$this->updateSettingsConfig( $settings['config'] ); |
| 598 |
} |
| 599 |
if ( isset( $settings['config-overrides'] ) ) { |
| 600 |
$this->updateSettingsConfig( $settings['config-overrides'] ); |
| 601 |
} |
| 602 |
|
| 603 |
$this->applySchemas( $settings ); |
| 604 |
|
| 605 |
if ( isset( $settings['config'] ) ) { |
| 606 |
$mergeStrategies = $this->configSchema->getMergeStrategies(); |
| 607 |
$this->configSink->setMulti( $settings['config'], $mergeStrategies ); |
| 608 |
} |
| 609 |
|
| 610 |
if ( isset( $settings['config-overrides'] ) ) { |
| 611 |
|
| 612 |
$this->configSink->setMulti( $settings['config-overrides'] ); |
| 613 |
} |
| 614 |
|
| 615 |
if ( isset( $settings['obsolete-config'] ) ) { |
| 616 |
$this->obsoleteConfig = array_merge( $this->obsoleteConfig, $settings['obsolete-config'] ); |
| 617 |
} |
| 618 |
|
| 619 |
if ( isset( $settings['config'] ) || isset( $settings['config-overrides'] ) ) { |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
$this->defaultsNeedMerging = true; |
| 625 |
} |
| 626 |
|
| 627 |
foreach ( $settings['php-ini'] ?? [] as $option => $value ) { |
| 628 |
$this->phpIniSink->set( |
| 629 |
$option, |
| 630 |
$value |
| 631 |
); |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
if ( isset( $settings['extensions'] ) ) { |
| 641 |
$extDir = $this->settingsConfig[MainConfigNames::ExtensionDirectory]; |
| 642 |
foreach ( $settings['extensions'] ?? [] as $ext ) { |
| 643 |
$path = "$extDir/$ext/extension.json"; |
| 644 |
$this->extensionRegistry->queue( $path ); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
if ( isset( $settings['skins'] ) ) { |
| 649 |
$skinDir = $this->settingsConfig[MainConfigNames::StyleDirectory]; |
| 650 |
foreach ( $settings['skins'] ?? [] as $skin ) { |
| 651 |
$path = "$skinDir/$skin/skin.json"; |
| 652 |
$this->extensionRegistry->queue( $path ); |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
public function putConfigValue( string $key, $value ): self { |
| 671 |
return $this->putConfigValues( [ $key => $value ] ); |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
public function putConfigValues( array $values ): self { |
| 687 |
return $this->loadArrayInternal( [ 'config' => $values ], __METHOD__ ); |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
public function overrideConfigValue( string $key, $value ): self { |
| 703 |
return $this->overrideConfigValues( [ $key => $value ] ); |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
public function overrideConfigValues( array $values ): self { |
| 718 |
return $this->loadArrayInternal( [ 'config-overrides' => $values ], __METHOD__ ); |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
public function registerHookHandlers( array $handlers ): self { |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
return $this->loadArrayInternal( [ 'config' => [ 'Hooks' => $handlers ] ], __METHOD__ ); |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
public function registerHookHandler( string $hook, $handler ): self { |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
return $this->loadArray( [ 'config' => [ 'Hooks' => [ $hook => [ $handler ] ] ] ] ); |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
public function getConfig(): Config { |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
if ( $this->config && !$this->currentBatch ) { |
| 767 |
return $this->config; |
| 768 |
} |
| 769 |
|
| 770 |
$this->apply(); |
| 771 |
$this->config = $this->configSink->build(); |
| 772 |
|
| 773 |
return $this->config; |
| 774 |
} |
| 775 |
|
| 776 |
private function reset() { |
| 777 |
$this->currentBatch = []; |
| 778 |
} |
| 779 |
|
| 780 |
private function assertNotReadOnly( string $func ): void { |
| 781 |
if ( $this->stage === self::STAGE_READ_ONLY ) { |
| 782 |
throw new SettingsBuilderException( |
| 783 |
"$func not supported in operation stage." |
| 784 |
); |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
private function assertStillLoading( string $func ): void { |
| 789 |
if ( $this->stage !== self::STAGE_LOADING ) { |
| 790 |
throw new SettingsBuilderException( |
| 791 |
"$func only supported while still in the loading stage." |
| 792 |
); |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
public function enterReadOnlyStage(): void { |
| 805 |
$this->apply(); |
| 806 |
$this->stage = self::STAGE_READ_ONLY; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
public function enterRegistrationStage(): void { |
| 817 |
$this->apply(); |
| 818 |
$this->stage = self::STAGE_REGISTRATION; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
public function getConfigBuilder(): ConfigBuilder { |
| 826 |
$this->apply(); |
| 827 |
return $this->configSink; |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
public function warning( string $msg ) { |
| 842 |
$this->assertNotReadOnly( __METHOD__ ); |
| 843 |
$this->warnings[] = trim( $msg ); |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
public function getWarnings(): array { |
| 853 |
return $this->warnings; |
| 854 |
} |
| 855 |
|
| 856 |
} |