| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * http://www.gnu.org/copyleft/gpl.html |
| 18 | * |
| 19 | * @file |
| 20 | * @since 1.35 |
| 21 | */ |
| 22 | |
| 23 | namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements; |
| 24 | |
| 25 | use MediaWiki\Config\Config; |
| 26 | use MediaWiki\Skins\Vector\FeatureManagement\Requirement; |
| 27 | |
| 28 | /** |
| 29 | * Some application state changes throughout the lifetime of the application, e.g. `wgSitename` or |
| 30 | * `wgFullyInitialised`, which signals whether the application boot process has finished and |
| 31 | * critical resources like database connections are available. |
| 32 | * |
| 33 | * The `DynamicStateRequirement` allows us to define requirements that lazily evaluate the |
| 34 | * application state statically, e.g. |
| 35 | * |
| 36 | * ```lang=php |
| 37 | * $featureManager->registerRequirement( |
| 38 | * new DynamicConfigRequirement( |
| 39 | * $config, |
| 40 | * MainConfigNames::Sitename, |
| 41 | * 'requirementName' |
| 42 | * ) |
| 43 | * ); |
| 44 | * ``` |
| 45 | * |
| 46 | * registers a requirement that will evaluate to true only when `mediawiki/includes/Setup.php` has |
| 47 | * finished executing (after all service wiring has executed). I.e., every call to |
| 48 | * `Requirement->isMet()` reinterrogates the Config object for the current state and returns it. |
| 49 | * Contrast to |
| 50 | * |
| 51 | * ```lang=php |
| 52 | * $featureManager->registerSimpleRequirement( |
| 53 | * 'requirementName', |
| 54 | * (bool)$config->get( MainConfigNames::Sitename ) |
| 55 | * ); |
| 56 | * ``` |
| 57 | * |
| 58 | * wherein state is evaluated only once at registration time and permanently cached. |
| 59 | * |
| 60 | * NOTE: This API hasn't settled. It may change at any time without warning. Please don't bind to |
| 61 | * it unless you absolutely need to |
| 62 | * |
| 63 | * @unstable |
| 64 | * |
| 65 | * @package MediaWiki\Skins\Vector\FeatureManagement\Requirements |
| 66 | * @internal |
| 67 | */ |
| 68 | final class DynamicConfigRequirement implements Requirement { |
| 69 | |
| 70 | private Config $config; |
| 71 | |
| 72 | private string $configName; |
| 73 | |
| 74 | private string $requirementName; |
| 75 | |
| 76 | /** |
| 77 | * @param Config $config |
| 78 | * @param string $configName Any `Config` key. This name is used to query `$config` state. E.g., |
| 79 | * `'DBname'`. See https://www.mediawiki.org/wiki/Manual:Configuration_settings |
| 80 | * @param string $requirementName The name of the requirement presented to FeatureManager. |
| 81 | * This name _usually_ matches the `$configName` parameter for simplicity but allows for |
| 82 | * abstraction as needed. See `Requirement->getName()`. |
| 83 | */ |
| 84 | public function __construct( Config $config, string $configName, string $requirementName ) { |
| 85 | $this->config = $config; |
| 86 | $this->configName = $configName; |
| 87 | $this->requirementName = $requirementName; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @inheritDoc |
| 92 | */ |
| 93 | public function getName(): string { |
| 94 | return $this->requirementName; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @inheritDoc |
| 99 | */ |
| 100 | public function isMet(): bool { |
| 101 | return (bool)$this->config->get( $this->configName ); |
| 102 | } |
| 103 | } |