Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear unregistered features #59

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions sources/ToggleRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,28 @@ public function configureFeature(string $feature, string $strategy, string $meth
'parameters' => $parameters,
]);
}

public function listUnregisteredFeatures(): array
{
$configuredFeatures = array_unique(array_reduce(
$this->strategies,
fn (array $features, TogglingStrategy $strategy) => \array_merge($features, $strategy->listFeatures()),
[]
));
Comment on lines +103 to +107
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can avoid some array_merge() calls by doing the following:

$configuredFeatures = array_unique(array_merge(...array_map(
    fn (TogglingStrategy $strategy) => $strategy->listFeatures(),
    $this->strategies
)));


$registeredFeatures = array_keys($this->registry->getDefinitions());

$unregisteredFeatures = array_values(array_diff($configuredFeatures, $registeredFeatures));

sort($unregisteredFeatures);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this should be part of this API or in the clients of this API: it feels like sorting things should be dealt with at display time...


return $unregisteredFeatures;
}

public function clearFeatureConfiguration(string $feature): void
{
foreach ($this->strategies as $strategy) {
$strategy->clearFeatureConfiguration($feature);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid data loss, I think we should add an assertion to make sure the feature is not registered before removing its configuration.

}
}
}
7 changes: 7 additions & 0 deletions sources/ToggleRouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ public function getFeatureConfiguration(string $feature): array;
* @param mixed $parameters
*/
public function configureFeature(string $feature, string $strategy, string $method, $parameters = []): void;

/**
* @return string[]
*/
public function listUnregisteredFeatures(): array;

public function clearFeatureConfiguration(string $feature): void;
}
43 changes: 43 additions & 0 deletions tests/ToggleRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,49 @@ public function testStrategiesCanBeCombinedWithBooleanOperators(): void
static::assertTrue($router->hasFeature('target', 'feature'));
}

public function testListUnregisteredFeatures(): void
{
$router = $this->configureToggleRouter(
new FeatureDefinition('feature', 'awesome feature', 'onoff or whitelist or percentage'),
$this->configureAllStrategies()
);

$router->configureFeature('feature', 'whitelist', 'allow', 'target');
$router->configureFeature('unregistered', 'whitelist', 'allow', 'target');
$router->configureFeature('unregistered', 'percentage', 'slide', '50');

static::assertSame(['unregistered'], $router->listUnregisteredFeatures());
}

public function testConfigurationCanBeClearedForRegisteredFeatures(): void
Copy link
Owner

@trompette trompette Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure we want this behaviour?

Like I said in my previous comment, users could lose configuration for registered features.

{
$router = $this->configureToggleRouter(
new FeatureDefinition('feature', 'awesome feature', 'onoff or whitelist or percentage'),
$this->configureAllStrategies()
);

$router->configureFeature('feature', 'whitelist', 'allow', 'target');

static::assertTrue($router->hasFeature('target', 'feature'));

$router->clearFeatureConfiguration('feature');

static::assertFalse($router->hasFeature('target', 'feature'));
}

public function testConfigurationCanBeClearedForUnregisteredFeatures(): void
{
$router = $this->configureToggleRouter(null, $this->configureAllStrategies());

$router->configureFeature('feature', 'whitelist', 'allow', 'target');

static::assertSame(['feature'], $router->listUnregisteredFeatures());

$router->clearFeatureConfiguration('feature');

static::assertEmpty($router->listUnregisteredFeatures());
}

/**
* @param array<string, TogglingStrategy> $strategies
*/
Expand Down