Skip to content

Commit

Permalink
Merge pull request #74 from LowrankLy/2.x-feature-options
Browse files Browse the repository at this point in the history
Add extra options to features
  • Loading branch information
jdecool authored Feb 19, 2025
2 parents 48c34fa + 33540c1 commit 25e87fd
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 21 deletions.
6 changes: 5 additions & 1 deletion src/Command/ListFeatureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function renderTable(OutputInterface $output, array $features): void
{
$table = new Table($output);
$table->setHeaders(['Name', 'Enabled', 'Description']);
$table->setHeaders(['Name', 'Enabled', 'Description', 'Options']);
foreach ($features as $feature) {
$table->addRow([
$feature->getKey(),
$feature->isEnabled() ? 'Yes' : 'No',
$feature->getDescription(),
json_encode($feature->getOptions(), JSON_PRETTY_PRINT),
]);
}

Expand Down Expand Up @@ -111,6 +112,9 @@ private function getCsvLine(array $columns): string
throw new \RuntimeException('Unable to open temporary file');
}

if (isset($columns['options'])) {
$columns['options'] = json_encode($columns['options']);
}
fputcsv($fp, $columns, ',', '"', '\\');

rewind($fp);
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->scalarNode('description')->end()
->booleanNode('enabled')->isRequired()->end()
->variableNode('options')->defaultValue([])->end()
->end()
->end()
->end()
Expand Down
12 changes: 11 additions & 1 deletion src/Model/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ class Feature implements FeatureInterface
/** @var bool */
private $enabled;

/** @var array */
private $options;

/**
* Constructor
*/
public function __construct(string $key, bool $enabled, ?string $description = null)
public function __construct(string $key, bool $enabled, ?string $description = null, array $options = [])
{
$this->key = $key;
$this->enabled = $enabled;
$this->description = $description ?? '';
$this->options = $options;
}

public function getKey(): string
Expand All @@ -54,6 +58,12 @@ public function toArray(): array
'key' => $this->key,
'enabled' => $this->enabled,
'description' => $this->description,
'options' => $this->options,
];
}

public function getOptions(): array
{
return $this->options;
}
}
3 changes: 3 additions & 0 deletions src/Model/FeatureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace Novaway\Bundle\FeatureFlagBundle\Model;

/**
* @method array getOptions()
*/
interface FeatureInterface
{
/**
Expand Down
4 changes: 2 additions & 2 deletions src/Storage/ArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class ArrayStorage extends AbstractStorage
/**
* Constructor
*
* @param array<string, array{enabled: bool, description: ?string}> $features
* @param array<string, array{enabled: bool, description: ?string, options: ?array<mixed>} > $features
*/
public function __construct(array $features = [])
{
ksort($features);

$this->features = [];
foreach ($features as $key => $feature) {
$this->features[$key] = new Feature($key, $feature['enabled'], $feature['description'] ?? '');
$this->features[$key] = new Feature($key, $feature['enabled'], $feature['description'] ?? '', $feature['options'] ?? []);
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/App/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
'bar' => [
'enabled' => false,
'description' => 'Bar feature description',
'options' => [
'foo' => 'bar',
'tableau' => ['test' => 'zaza'],
],
],
'env_var' => '%env(bool:FEATURE_ENVVAR)%',
],
Expand Down
5 changes: 4 additions & 1 deletion tests/Functional/DefaultFeatureStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public function testAccessAllRegisteredFeatures(): void
[
'override' => new Feature('override', false),
'foo' => new Feature('foo', true),
'bar' => new Feature('bar', false, 'Bar feature description'),
'bar' => new Feature('bar', false, 'Bar feature description', [
'foo' => 'bar',
'tableau' => ['test' => 'zaza'],
]),
'env_var' => new Feature('env_var', false),
],
$features,
Expand Down
51 changes: 35 additions & 16 deletions tests/Unit/Command/ListFeatureCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ final class ListFeatureCommandTest extends TestCase
'features' => [],
'output' => [
'table' => <<<OUTPUT
+------+---------+-------------+
| Name | Enabled | Description |
+------+---------+-------------+
+------+---------+-------------+---------+
| Name | Enabled | Description | Options |
+------+---------+-------------+---------+
OUTPUT,
'json' => <<<JSON
Expand All @@ -43,52 +43,71 @@ final class ListFeatureCommandTest extends TestCase
'feature1' => [
'enabled' => true,
'description' => 'Feature 1 description',
'options' => [],
],
'feature2' => [
'enabled' => false,
'description' => 'Feature 2 description',
'options' => [],
],
'feature3' => [
'enabled' => true,
'description' => 'Feature 3 description',
'options' => [
'foo' => 'bar',
'farray' => ['fuu' => 'bor'],
],
],
],
'output' => [
'table' => <<<OUTPUT
+----------+---------+-----------------------+
| Name | Enabled | Description |
+----------+---------+-----------------------+
| feature1 | Yes | Feature 1 description |
| feature2 | No | Feature 2 description |
| feature3 | Yes | Feature 3 description |
+----------+---------+-----------------------+
+----------+---------+-----------------------+----------------------+
| Name | Enabled | Description | Options |
+----------+---------+-----------------------+----------------------+
| feature1 | Yes | Feature 1 description | [] |
| feature2 | No | Feature 2 description | [] |
| feature3 | Yes | Feature 3 description | { |
| | | | "foo": "bar", |
| | | | "farray": { |
| | | | "fuu": "bor" |
| | | | } |
| | | | } |
+----------+---------+-----------------------+----------------------+
OUTPUT,
'json' => <<<JSON
{
"feature1": {
"key": "feature1",
"enabled": true,
"description": "Feature 1 description"
"description": "Feature 1 description",
"options": []
},
"feature2": {
"key": "feature2",
"enabled": false,
"description": "Feature 2 description"
"description": "Feature 2 description",
"options": []
},
"feature3": {
"key": "feature3",
"enabled": true,
"description": "Feature 3 description"
"description": "Feature 3 description",
"options": {
"foo": "bar",
"farray": {
"fuu": "bor"
}
}
}
}
JSON,
'csv' => <<<CSV
Name,Enabled,Description
feature1,1,"Feature 1 description"
feature2,,"Feature 2 description"
feature3,1,"Feature 3 description"
feature1,1,"Feature 1 description",[]
feature2,,"Feature 2 description",[]
feature3,1,"Feature 3 description","{""foo"":""bar"",""farray"":{""fuu"":""bor""}}"
CSV,
],
Expand Down

0 comments on commit 25e87fd

Please sign in to comment.