Skip to content

Commit

Permalink
Merge pull request #75 from novaway/feature-options
Browse files Browse the repository at this point in the history
Backport Feature extra options
  • Loading branch information
jdecool authored Feb 21, 2025
2 parents 6d93aa7 + 49fd7b3 commit 24852e2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 24 deletions.
7 changes: 6 additions & 1 deletion src/Command/ListFeatureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private function renderTable(SymfonyStyle $output, array $storagesFeatures): voi
$feature['key'],
$feature['enabled'] ? 'Yes' : 'No',
$feature['description'],
json_encode($feature['options'], JSON_PRETTY_PRINT),
]);
}

Expand All @@ -98,7 +99,7 @@ private function renderJson(OutputInterface $output, array $storagesFeatures): v

private function renderCsv(OutputInterface $output, array $storagesFeatures): void
{
$output->writeln($this->getCsvLine(['Manager', 'Name', 'Enabled', 'Description']));
$output->writeln($this->getCsvLine(['Manager', 'Name', 'Enabled', 'Description', 'Options']));

foreach ($storagesFeatures as $storage => $features) {
foreach ($features as $feature) {
Expand All @@ -109,6 +110,10 @@ private function renderCsv(OutputInterface $output, array $storagesFeatures): vo

private function getCsvLine(array $columns): string
{
if (isset($columns['options'])) {
$columns['options'] = json_encode($columns['options'], JSON_PRETTY_PRINT);
}

$fp = fopen('php://temp', 'w+') ?: throw new \RuntimeException('Unable to open temporary file');
fputcsv($fp, $columns);

Expand Down
13 changes: 13 additions & 0 deletions src/Model/FeatureFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
*/
final class FeatureFlag implements Feature
{
/**
* @param array<mixed> $options
*/
public function __construct(
private readonly string $key,
private readonly bool $enabled,
private readonly string $description = '',
private readonly array $options = [],
) {
}

Expand All @@ -36,12 +40,21 @@ public function isEnabled(): bool
return $this->enabled;
}

/**
* @return array<mixed>
*/
public function getOptions(): array
{
return $this->options;
}

public function toArray(): array
{
return [
'key' => $this->key,
'enabled' => $this->enabled,
'description' => $this->description,
'options' => $this->options,
];
}
}
7 changes: 4 additions & 3 deletions src/Storage/ArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class ArrayStorage implements Storage

public function __construct(array $options = [])
{
$this->features = array_map(function (array $feature) {
return new FeatureFlag($feature['name'], $feature['enabled'], $feature['description'] ?? '');
}, $options['features']);
$this->features = array_map(
static fn (array $feature) => new FeatureFlag($feature['name'], $feature['enabled'], $feature['description'] ?? '', $feature['options'] ?? []),
$options['features'],
);
}

public function all(): array
Expand Down
66 changes: 47 additions & 19 deletions tests/Unit/Command/ListFeatureCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class ListFeatureCommandTest extends TestCase
JSON,
'csv' => <<<CSV
Manager,Name,Enabled,Description
Manager,Name,Enabled,Description,Options
CSV,
],
Expand All @@ -53,6 +53,13 @@ final class ListFeatureCommandTest extends TestCase
'name' => 'feature2',
'enabled' => false,
'description' => 'Feature 2 description',
'options' => [
'foo' => 'bar',
'parray' => [
'key1' => 'value1',
'key2' => 'value2',
],
],
],
],
],
Expand All @@ -75,21 +82,27 @@ final class ListFeatureCommandTest extends TestCase
manager1
========
+----------+---------+-----------------------+
| Name | Enabled | Description |
+----------+---------+-----------------------+
| feature1 | Yes | Feature 1 description |
| feature2 | No | Feature 2 description |
+----------+---------+-----------------------+
+----------+---------+-----------------------+---------------------------+
| Name | Enabled | Description | |
+----------+---------+-----------------------+---------------------------+
| feature1 | Yes | Feature 1 description | [] |
| feature2 | No | Feature 2 description | { |
| | | | "foo": "bar", |
| | | | "parray": { |
| | | | "key1": "value1", |
| | | | "key2": "value2" |
| | | | } |
| | | | } |
+----------+---------+-----------------------+---------------------------+
manager2
========
+----------+---------+-----------------------+
| Name | Enabled | Description |
+----------+---------+-----------------------+
| feature3 | Yes | Feature 3 description |
+----------+---------+-----------------------+
+----------+---------+-----------------------+----+
| Name | Enabled | Description | |
+----------+---------+-----------------------+----+
| feature3 | Yes | Feature 3 description | [] |
+----------+---------+-----------------------+----+
OUTPUT,
'json' => <<<JSON
Expand All @@ -98,29 +111,44 @@ final class ListFeatureCommandTest extends TestCase
"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": {
"foo": "bar",
"parray": {
"key1": "value1",
"key2": "value2"
}
}
}
},
"manager2": {
"feature3": {
"key": "feature3",
"enabled": true,
"description": "Feature 3 description"
"description": "Feature 3 description",
"options": []
}
}
}
JSON,
'csv' => <<<CSV
Manager,Name,Enabled,Description
manager1,feature1,1,"Feature 1 description"
manager1,feature2,,"Feature 2 description"
manager2,feature3,1,"Feature 3 description"
Manager,Name,Enabled,Description,Options
manager1,feature1,1,"Feature 1 description",[]
manager1,feature2,,"Feature 2 description","{
""foo"": ""bar"",
""parray"": {
""key1"": ""value1"",
""key2"": ""value2""
}
}"
manager2,feature3,1,"Feature 3 description",[]
CSV,
],
Expand Down
15 changes: 14 additions & 1 deletion tests/Unit/Model/FeatureFlagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,25 @@ final class FeatureFlagTest extends TestCase
{
public function testToArrayResult(): void
{
$feature = new FeatureFlag('foo', true, 'bar');
$feature = new FeatureFlag('foo', true, 'bar', [
'foo' => 'bar',
'parray' => [
'key1' => 'value1',
'key2' => 'value2',
],
]);

static::assertSame([
'key' => 'foo',
'enabled' => true,
'description' => 'bar',
'options' => [
'foo' => 'bar',
'parray' => [
'key1' => 'value1',
'key2' => 'value2',
],
],
], $feature->toArray());
}
}

0 comments on commit 24852e2

Please sign in to comment.