2020-02-20 16:37:33 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @since 1.35
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Vector\FeatureManagement;
|
|
|
|
|
|
|
|
use Wikimedia\Assert\Assert;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple feature manager.
|
|
|
|
*
|
|
|
|
* NOTE: This API hasn't settled. It may change at any time without warning. Please don't bind to
|
|
|
|
* it unless you absolutely need to.
|
|
|
|
*
|
|
|
|
* @unstable
|
|
|
|
*
|
|
|
|
* @package FeatureManagement
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
final class FeatureManager {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A map of feature name to the array of requirements. A feature is only considered enabled when
|
|
|
|
* all of its requirements are met.
|
|
|
|
*
|
|
|
|
* See FeatureManager::registerFeature for additional detail.
|
|
|
|
*
|
|
|
|
* @var Array<string,string[]>
|
|
|
|
*/
|
|
|
|
private $features = [];
|
|
|
|
|
|
|
|
/**
|
2020-02-28 15:42:05 +00:00
|
|
|
* A map of requirement name to whether the requirement is met.
|
2020-02-20 16:37:33 +00:00
|
|
|
*
|
|
|
|
* @var Array<string,bool>
|
|
|
|
*/
|
2020-02-28 15:42:05 +00:00
|
|
|
private $requirements = [];
|
2020-02-20 16:37:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a feature and its requirements.
|
|
|
|
*
|
|
|
|
* Essentially, a "feature" is a friendly (hopefully) name for some component, however big or
|
|
|
|
* small, that has some requirements. A feature manager allows us to decouple the component's
|
|
|
|
* logic from its requirements, allowing them to vary independently. Moreover, the use of
|
2020-02-28 15:42:05 +00:00
|
|
|
* friendly names wherever possible allows us to define a common language with our non-technical
|
2020-02-20 16:37:33 +00:00
|
|
|
* colleagues.
|
|
|
|
*
|
|
|
|
* ```php
|
2020-02-28 15:42:05 +00:00
|
|
|
* $featureManager->registerFeature( 'featureA', 'requirementA' );
|
2020-02-20 16:37:33 +00:00
|
|
|
* ```
|
|
|
|
*
|
2020-02-28 15:42:05 +00:00
|
|
|
* defines the "featureA" feature, which is enabled when the "requirementA" requirement is met.
|
2020-02-20 16:37:33 +00:00
|
|
|
*
|
|
|
|
* ```php
|
2020-02-28 15:42:05 +00:00
|
|
|
* $featureManager->registerFeature( 'featureB', [ 'requirementA', 'requirementB' ] );
|
2020-02-20 16:37:33 +00:00
|
|
|
* ```
|
|
|
|
*
|
2020-02-28 15:42:05 +00:00
|
|
|
* defines the "featureB" feature, which is enabled when the "requirementA" and "requirementB"
|
|
|
|
* requirements are met. Note well that the feature is only enabled when _all_ requirements are
|
|
|
|
* met, i.e. the requirements are evaluated in order and logically `AND`ed together.
|
2020-02-20 16:37:33 +00:00
|
|
|
*
|
|
|
|
* @param string $feature The name of the feature
|
2020-02-28 15:42:05 +00:00
|
|
|
* @param string|array $requirements The feature's requirements. As above, you can define a
|
|
|
|
* feature that requires a single requirement via the shorthand
|
2020-02-20 16:37:33 +00:00
|
|
|
*
|
|
|
|
* ```php
|
2020-02-28 15:42:05 +00:00
|
|
|
* $featureManager->registerFeature( 'feature', 'requirementA' );
|
|
|
|
* // Equivalent to $featureManager->registerFeature( 'feature', [ 'requirementA' ] );
|
2020-02-20 16:37:33 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @throws \LogicException If the feature is already registered
|
|
|
|
* @throws \Wikimedia\Assert\ParameterAssertionException If the feature's requirements aren't
|
2020-02-28 15:42:05 +00:00
|
|
|
* the name of a single requirement or a list of requirements
|
|
|
|
* @throws \InvalidArgumentException If the feature references a requirement that isn't
|
|
|
|
* registered
|
2020-02-20 16:37:33 +00:00
|
|
|
*/
|
|
|
|
public function registerFeature( $feature, $requirements ) {
|
|
|
|
//
|
|
|
|
// Validation
|
|
|
|
if ( array_key_exists( $feature, $this->features ) ) {
|
2020-02-28 15:42:05 +00:00
|
|
|
throw new \LogicException( sprintf(
|
|
|
|
'Feature "%s" is already registered.',
|
|
|
|
$feature
|
|
|
|
) );
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Assert::parameterType( 'string|array', $requirements, 'requirements' );
|
|
|
|
|
|
|
|
$requirements = (array)$requirements;
|
|
|
|
|
|
|
|
Assert::parameterElementType( 'string', $requirements, 'requirements' );
|
|
|
|
|
2020-02-28 15:42:05 +00:00
|
|
|
foreach ( $requirements as $name ) {
|
|
|
|
if ( !array_key_exists( $name, $this->requirements ) ) {
|
|
|
|
throw new \InvalidArgumentException( sprintf(
|
|
|
|
'Feature "%s" references requirement "%s", which hasn\'t been registered',
|
|
|
|
$feature,
|
|
|
|
$name
|
|
|
|
) );
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutation
|
|
|
|
$this->features[$feature] = $requirements;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets whether the feature's requirements are met.
|
|
|
|
*
|
|
|
|
* @param string $feature
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException If the feature isn't registered
|
|
|
|
*/
|
|
|
|
public function isFeatureEnabled( $feature ) {
|
|
|
|
if ( !array_key_exists( $feature, $this->features ) ) {
|
|
|
|
throw new \InvalidArgumentException( "The feature \"{$feature}\" isn't registered." );
|
|
|
|
}
|
|
|
|
|
|
|
|
$requirements = $this->features[$feature];
|
|
|
|
|
2020-02-28 15:42:05 +00:00
|
|
|
foreach ( $requirements as $name ) {
|
|
|
|
if ( !$this->requirements[$name] ) {
|
2020-02-20 16:37:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-28 15:42:05 +00:00
|
|
|
* Register a requirement.
|
2020-02-20 16:37:33 +00:00
|
|
|
*
|
2020-02-28 15:42:05 +00:00
|
|
|
* A requirement is some condition of the application state that a feature requires to be true
|
|
|
|
* or false.
|
2020-02-20 16:37:33 +00:00
|
|
|
*
|
2020-02-28 15:42:05 +00:00
|
|
|
* At the moment, these conditions can only be evaluated when the requirement is being defined,
|
|
|
|
* i.e. at boot time. At that time, certain objects mightn't have been fully loaded (see
|
|
|
|
* User::isSafeToLoad). See TODO.md for the proposed list of steps to allow this feature
|
2020-02-20 16:37:33 +00:00
|
|
|
* manager to handle that scenario.
|
|
|
|
*
|
2020-02-28 15:42:05 +00:00
|
|
|
* @param string $name The name of the requirement
|
|
|
|
* @param bool $isMet Whether the requirement is met
|
2020-02-20 16:37:33 +00:00
|
|
|
*
|
2020-02-28 15:42:05 +00:00
|
|
|
* @throws \LogicException If the requirement has already been registered
|
2020-02-20 16:37:33 +00:00
|
|
|
*/
|
2020-02-28 15:42:05 +00:00
|
|
|
public function registerRequirement( $name, $isMet ) {
|
|
|
|
if ( array_key_exists( $name, $this->requirements ) ) {
|
|
|
|
throw new \LogicException( "The requirement \"{$name}\" is already registered." );
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 15:42:05 +00:00
|
|
|
Assert::parameterType( 'boolean', $isMet, 'isMet' );
|
2020-02-20 16:37:33 +00:00
|
|
|
|
2020-02-28 15:42:05 +00:00
|
|
|
$this->requirements[$name] = $isMet;
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-28 15:42:05 +00:00
|
|
|
* Gets whether the requirement is met.
|
2020-02-20 16:37:33 +00:00
|
|
|
*
|
2020-02-28 15:42:05 +00:00
|
|
|
* @param string $name The name of the requirement
|
2020-02-20 16:37:33 +00:00
|
|
|
* @return bool
|
|
|
|
*
|
2020-02-28 15:42:05 +00:00
|
|
|
* @throws \InvalidArgumentException If the requirement isn't registered
|
2020-02-20 16:37:33 +00:00
|
|
|
*/
|
2020-02-28 15:42:05 +00:00
|
|
|
public function isRequirementMet( $name ) {
|
|
|
|
if ( !array_key_exists( $name, $this->requirements ) ) {
|
|
|
|
throw new \InvalidArgumentException( "Requirement \"{$name}\" isn't registered." );
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 15:42:05 +00:00
|
|
|
return $this->requirements[$name];
|
2020-02-20 16:37:33 +00:00
|
|
|
}
|
|
|
|
}
|