Load feature flags also from DB
[MAILPOET-2008]
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user