init
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
|
||||
|
||||
namespace MailPoet\Features;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Entities\FeatureFlagEntity;
|
||||
|
||||
class FeatureFlagsController {
|
||||
|
||||
/** @var FeaturesController */
|
||||
private $featuresController;
|
||||
|
||||
/** @var FeatureFlagsRepository */
|
||||
private $featureFlagsRepository;
|
||||
|
||||
public function __construct(
|
||||
FeaturesController $featuresController,
|
||||
FeatureFlagsRepository $featureFlagsRepository
|
||||
) {
|
||||
$this->featuresController = $featuresController;
|
||||
$this->featureFlagsRepository = $featureFlagsRepository;
|
||||
}
|
||||
|
||||
public function set($name, $value) {
|
||||
if (!$this->featuresController->exists($name)) {
|
||||
throw new \RuntimeException("Feature '$name' does not exist'");
|
||||
}
|
||||
|
||||
$this->featureFlagsRepository->createOrUpdate(['name' => $name, 'value' => $value]);
|
||||
}
|
||||
|
||||
public function getAll() {
|
||||
$flags = $this->featureFlagsRepository->findAll();
|
||||
$flagsMap = array_combine(
|
||||
array_map(
|
||||
function (FeatureFlagEntity $flag) {
|
||||
return $flag->getName();
|
||||
},
|
||||
$flags
|
||||
),
|
||||
$flags
|
||||
);
|
||||
|
||||
$output = [];
|
||||
foreach ($this->featuresController->getDefaults() as $name => $default) {
|
||||
$output[] = [
|
||||
'name' => $name,
|
||||
'value' => isset($flagsMap[$name]) ? (bool)$flagsMap[$name]->getValue() : $default,
|
||||
'default' => $default,
|
||||
];
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
|
||||
|
||||
namespace MailPoet\Features;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoet\Doctrine\Repository;
|
||||
use MailPoet\Entities\FeatureFlagEntity;
|
||||
|
||||
/**
|
||||
* @extends Repository<FeatureFlagEntity>
|
||||
*/
|
||||
class FeatureFlagsRepository extends Repository {
|
||||
protected function getEntityClassName() {
|
||||
return FeatureFlagEntity::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @throws \RuntimeException
|
||||
* @throws \InvalidArgumentException
|
||||
* @return FeatureFlagEntity
|
||||
*/
|
||||
public function createOrUpdate(array $data = []) {
|
||||
if (!$data['name']) {
|
||||
throw new \InvalidArgumentException('Missing name');
|
||||
}
|
||||
$featureFlag = $this->findOneBy([
|
||||
'name' => $data['name'],
|
||||
]);
|
||||
if (!$featureFlag) {
|
||||
$featureFlag = new FeatureFlagEntity($data['name']);
|
||||
$this->persist($featureFlag);
|
||||
}
|
||||
|
||||
if (array_key_exists('value', $data)) {
|
||||
$featureFlag->setValue($data['value']);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->flush();
|
||||
} catch (\Exception $e) {
|
||||
throw new \RuntimeException("Error when saving feature " . $data['name']);
|
||||
}
|
||||
return $featureFlag;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
|
||||
|
||||
namespace MailPoet\Features;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
|
||||
use MailPoetVendor\Doctrine\DBAL\Exception\TableNotFoundException;
|
||||
|
||||
class FeaturesController {
|
||||
const FEATURE_BRAND_TEMPLATES = 'brand_templates';
|
||||
|
||||
// Define feature defaults in the array below in the following form:
|
||||
// self::FEATURE_NAME_OF_FEATURE => true,
|
||||
private $defaults = [
|
||||
self::FEATURE_BRAND_TEMPLATES => false,
|
||||
];
|
||||
|
||||
/** @var array|null */
|
||||
private $flags;
|
||||
|
||||
/** @var FeatureFlagsRepository */
|
||||
private $featureFlagsRepository;
|
||||
|
||||
public function __construct(
|
||||
FeatureFlagsRepository $featureFlagsRepository
|
||||
) {
|
||||
$this->featureFlagsRepository = $featureFlagsRepository;
|
||||
}
|
||||
|
||||
/** @return bool */
|
||||
public function isSupported($feature) {
|
||||
if (!$this->exists($feature)) {
|
||||
throw new \RuntimeException("Unknown feature '$feature'");
|
||||
}
|
||||
// ensure controller works even if used before migrator, return default value in such case
|
||||
try {
|
||||
$this->ensureFlagsLoaded();
|
||||
} catch (TableNotFoundException $e) {
|
||||
return $this->defaults[$feature];
|
||||
}
|
||||
return ($this->flags ?? [])[$feature];
|
||||
}
|
||||
|
||||
/** @return bool */
|
||||
public function exists($feature) {
|
||||
return array_key_exists($feature, $this->defaults);
|
||||
}
|
||||
|
||||
/** @return array */
|
||||
public function getDefaults() {
|
||||
return $this->defaults;
|
||||
}
|
||||
|
||||
/** @return array */
|
||||
public function getAllFlags() {
|
||||
$this->ensureFlagsLoaded();
|
||||
return $this->flags ?? [];
|
||||
}
|
||||
|
||||
public function resetCache(): void {
|
||||
$this->flags = null;
|
||||
}
|
||||
|
||||
private function ensureFlagsLoaded() {
|
||||
if ($this->flags !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$flagsMap = $this->getValueMap();
|
||||
$this->flags = [];
|
||||
foreach ($this->defaults as $name => $default) {
|
||||
$this->flags[$name] = isset($flagsMap[$name]) ? $flagsMap[$name] : $default;
|
||||
}
|
||||
}
|
||||
|
||||
private function getValueMap() {
|
||||
$features = $this->featureFlagsRepository->findAll();
|
||||
$featuresMap = [];
|
||||
foreach ($features as $feature) {
|
||||
$featuresMap[$feature->getName()] = (bool)$feature->getValue();
|
||||
}
|
||||
return $featuresMap;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<?php
|
||||
Reference in New Issue
Block a user