Load feature flags also from DB

[MAILPOET-2008]
This commit is contained in:
Jan Jakeš
2019-05-08 11:04:25 +02:00
committed by M. Shull
parent 8e335a808f
commit 6323d5dc32
2 changed files with 36 additions and 2 deletions

View File

@ -73,6 +73,7 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\Cron\CronTrigger::class)->setPublic(true);
// Features
$container->autowire(\MailPoet\Features\FeaturesController::class);
$container->autowire(\MailPoet\Features\FeatureFlagsController::class);
// Form
$container->autowire(\MailPoet\Form\Util\FieldNameObfuscator::class)->setPublic(true);
// Listing

View File

@ -2,6 +2,8 @@
namespace MailPoet\Features;
use MailPoet\Models\FeatureFlag;
class FeaturesController {
// Define features below in the following form:
@ -12,11 +14,42 @@ class FeaturesController {
public static $defaults = [
];
/** @var array */
private $flags;
/** @return bool */
function isSupported($feature) {
if (!array_key_exists($feature, self::$defaults)) {
$this->ensureFlagsLoaded();
if (!array_key_exists($feature, $this->flags)) {
throw new \RuntimeException("Unknown feature '$feature'");
}
return self::$defaults[$feature];
return $this->flags[$feature];
}
/** @return array */
function getAllFlags() {
$this->ensureFlagsLoaded();
return $this->flags;
}
private function ensureFlagsLoaded() {
if ($this->flags !== null) {
return;
}
$this->flags = [];
$flagsMap = $this->getValueMap();
foreach (self::$defaults as $name => $default) {
$this->flags[$name] = isset($flagsMap[$name]) ? $flagsMap[$name] : $default;
}
}
private function getValueMap() {
$features = FeatureFlag::selectMany(['name', 'value'])->findMany();
$featuresMap = [];
foreach ($features as $feature) {
$featuresMap[$feature->name] = (bool)$feature->value;
}
return $featuresMap;
}
}