Load feature flags also from DB
[MAILPOET-2008]
This commit is contained in:
@ -73,6 +73,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Cron\CronTrigger::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\CronTrigger::class)->setPublic(true);
|
||||||
// Features
|
// Features
|
||||||
$container->autowire(\MailPoet\Features\FeaturesController::class);
|
$container->autowire(\MailPoet\Features\FeaturesController::class);
|
||||||
|
$container->autowire(\MailPoet\Features\FeatureFlagsController::class);
|
||||||
// Form
|
// Form
|
||||||
$container->autowire(\MailPoet\Form\Util\FieldNameObfuscator::class)->setPublic(true);
|
$container->autowire(\MailPoet\Form\Util\FieldNameObfuscator::class)->setPublic(true);
|
||||||
// Listing
|
// Listing
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace MailPoet\Features;
|
namespace MailPoet\Features;
|
||||||
|
|
||||||
|
use MailPoet\Models\FeatureFlag;
|
||||||
|
|
||||||
class FeaturesController {
|
class FeaturesController {
|
||||||
|
|
||||||
// Define features below in the following form:
|
// Define features below in the following form:
|
||||||
@ -12,11 +14,42 @@ class FeaturesController {
|
|||||||
public static $defaults = [
|
public static $defaults = [
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
private $flags;
|
||||||
|
|
||||||
/** @return bool */
|
/** @return bool */
|
||||||
function isSupported($feature) {
|
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'");
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user