Skip to content

[FeatureFlags] Propose a new component #51649

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

Open
wants to merge 25 commits into
base: 7.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
129043f
[FeatureToggle] Import component from private sources
Neirda24 Jul 28, 2023
48819ec
[UPDATE] Fix twig function must be snake_case + gitattributes for bundle
Neirda24 Jul 30, 2023
2f875ea
cleanup before RFC (#1)
Jean-Beru Sep 7, 2023
f2cc75b
[UPDATE] Removed the Random Strategy & Provider
Neirda24 Sep 7, 2023
13d6176
[UPDATE] According to comments
Neirda24 Sep 13, 2023
1d4fb6c
[FabBot] Apply some patches
Neirda24 Sep 13, 2023
a8a2536
[UPDATE] Remove Outer*Interface's
Neirda24 Sep 21, 2023
844ae65
[UPDATE] Use Traversable instead of ArrayIterator
Neirda24 Sep 21, 2023
0473edc
[UPDATE] Remove forgottent Outer*Interface's
Neirda24 Sep 21, 2023
ba54221
[UPDATE] Add StrategyInterface on previous Outer*Strategy's
Neirda24 Sep 21, 2023
4f03511
[FEATURE] Add header, query strategies through request stack and make…
Neirda24 Sep 21, 2023
416e91d
[UPDATE] Add UnanimousStrategy
Neirda24 Sep 21, 2023
17fae0a
[UPDATE] Prefixed some return phpdoc tag with phpstan-
Neirda24 Sep 21, 2023
ad36be4
[UPDATE] Rename to
Neirda24 Sep 21, 2023
858f66d
[UPDATE] Fix phpunit covers annotation
Neirda24 Sep 21, 2023
6a356aa
[UPDATE][fabbot] Coding standard
Neirda24 Sep 22, 2023
cec1e92
[UPDATE][fabbot] usage of void in tests
Neirda24 Sep 22, 2023
7618497
[UPDATE] Improve performance on FeatureCollection to stop at first found
Neirda24 Sep 22, 2023
0ff46e4
[Update] Move Debug files
Jean-Beru Sep 25, 2023
19b5c5f
Rework of providers
Neirda24 Sep 29, 2023
5be52b0
[UPDATE] Fix coding standards according to fabbot
Neirda24 Sep 29, 2023
63f256f
rename to FeatureFlags (#6)
Jean-Beru Sep 29, 2023
1673877
Feature/add debug command
Neirda24 Oct 5, 2023
00bed51
[UPDATE] Add coding standards
Neirda24 Oct 5, 2023
c4842ba
move to FrameworkBundle (#9)
Jean-Beru Oct 12, 2023
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
Prev Previous commit
Next Next commit
[UPDATE] Add UnanimousStrategy
  • Loading branch information
Neirda24 authored and Jean-Beru committed Oct 16, 2023
commit 416e91dfa91cf5f29b50bdd8dd24d601b926c69c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
final class Configuration implements ConfigurationInterface
{
private const KNOWN_STRATEGY_TYPES = ['grant', 'deny', 'not', 'date', 'env', 'request_header', 'request_query', 'request_attribute', 'priority', 'affirmative'];
private const KNOWN_STRATEGY_TYPES = ['grant', 'deny', 'not', 'date', 'env', 'request_header', 'request_query', 'request_attribute', 'priority', 'affirmative', 'unanimous'];

public function getConfigTreeBuilder(): TreeBuilder
{
Expand Down Expand Up @@ -70,7 +70,7 @@ public function getConfigTreeBuilder(): TreeBuilder
'date' => ['from' => null, 'until' => null, 'includeFrom' => false, 'includeUntil' => false],
'not' => ['strategy' => null],
'env', 'request_header', 'request_query', 'request_attribute' => ['name' => null],
'priority', 'affirmative' => ['strategies' => null],
'priority', 'affirmative', 'unanimous' => ['strategies' => null],
default => [],
};

Expand Down Expand Up @@ -115,7 +115,7 @@ public function getConfigTreeBuilder(): TreeBuilder
throw new \InvalidArgumentException('"name" must be provided.');
}
},
'priority', 'affirmative' => static function (array $with): void {
'priority', 'affirmative', 'unanimous' => static function (array $with): void {
if ([] === (array) $with['strategies']) {
throw new \InvalidArgumentException('"strategies" must be provided.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private function generateStrategy(string $type, array $with): Definition
'request_header' => $definition->setArguments(['$headerName' => $with['name']]),
'request_query' => $definition->setArguments(['$queryParameterName' => $with['name']]),
'request_attribute' => $definition->setArguments(['$attributeName' => $with['name']]), // Check if RequestStack class exists
'priority', 'affirmative' => $definition->setArguments([
'priority', 'affirmative', 'unanimous' => $definition->setArguments([
'$strategies' => array_map(
static fn (string $referencedStrategyName): Reference => new Reference($referencedStrategyName), // @phpstan-ignore-line
(array) $with['strategies'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\FeatureToggle\Strategy\GrantStrategy;
use Symfony\Component\FeatureToggle\Strategy\NotStrategy;
use Symfony\Component\FeatureToggle\Strategy\PriorityStrategy;
use Symfony\Component\FeatureToggle\Strategy\UnanimousStrategy;

return static function (ContainerConfigurator $container) {
$prefix = 'feature_toggle.abstract_strategy.';
Expand Down Expand Up @@ -45,4 +46,7 @@
$services->set($prefix.'affirmative', AffirmativeStrategy::class)->abstract()->args([
'$strategies' => abstract_arg('Defined in FeatureToggleExtension'),
]);
$services->set($prefix.'unanimous', UnanimousStrategy::class)->abstract()->args([
'$strategies' => abstract_arg('Defined in FeatureToggleExtension'),
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public function getConfig(): array
'type' => 'affirmative',
'with' => ['strategies' => ['env.feature-strategy', 'grant.feature-strategy']],
],
[
'name' => 'unanimous.feature-strategy',
'type' => 'unanimous',
'with' => ['strategies' => ['env.feature-strategy', 'grant.feature-strategy']],
],
[
'name' => 'not.feature-strategy',
'type' => 'not',
Expand Down
43 changes: 43 additions & 0 deletions src/Symfony/Component/FeatureToggle/Strategy/UnanimousStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\FeatureToggle\Strategy;

use Symfony\Component\FeatureToggle\StrategyResult;

final class UnanimousStrategy implements StrategyInterface
{
/**
* @param iterable<StrategyInterface> $strategies
*/
public function __construct(
private readonly iterable $strategies,
) {
}

public function compute(): StrategyResult
{
$result = StrategyResult::Abstain;
foreach ($this->strategies as $strategy) {
$innerResult = $strategy->compute();

if (StrategyResult::Deny === $innerResult) {
return StrategyResult::Deny;
}

if (StrategyResult::Grant === $innerResult) {
$result = StrategyResult::Grant;
}
}

return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\FeatureToggle\Strategy\AffirmativeStrategy;
use Symfony\Component\FeatureToggle\Strategy\StrategyInterface;
use Symfony\Component\FeatureToggle\StrategyResult;
use function is_a;

/**
* @covers \Symfony\Component\FeatureToggle\Strategy\AffirmativeStrategy
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\FeatureToggle\Tests\Strategy;

use Symfony\Component\FeatureToggle\Strategy\AffirmativeStrategy;
use Symfony\Component\FeatureToggle\Strategy\StrategyInterface;
use Symfony\Component\FeatureToggle\Strategy\UnanimousStrategy;
use Symfony\Component\FeatureToggle\StrategyResult;

/**
* @covers \Symfony\Component\FeatureToggle\Strategy\AffirmativeStrategy
*/
final class UnanimousStrategyTest extends AbstractOuterStrategiesTestCase
{
public static function generatesValidStrategies(): \Generator
{
yield 'no strategies' => [
[],
StrategyResult::Abstain,
];

yield 'if all abstain' => [
[
self::generateStrategy(StrategyResult::Abstain),
self::generateStrategy(StrategyResult::Abstain),
self::generateStrategy(StrategyResult::Abstain),
],
StrategyResult::Abstain,
];

yield 'if one denies after only abstain results' => [
[
self::generateStrategy(StrategyResult::Abstain),
self::generateStrategy(StrategyResult::Abstain),
self::generateStrategy(StrategyResult::Deny),
],
StrategyResult::Deny,
];

yield 'if one grants after only abstain results' => [
[
self::generateStrategy(StrategyResult::Abstain),
self::generateStrategy(StrategyResult::Abstain),
self::generateStrategy(StrategyResult::Grant),
],
StrategyResult::Grant,
];

yield 'if one grants after at least one Deny' => [
[
self::generateStrategy(StrategyResult::Abstain),
self::generateStrategy(StrategyResult::Deny),
self::generateStrategy(StrategyResult::Grant),
],
StrategyResult::Deny,
];

yield 'if one denies after at least one grant' => [
[
self::generateStrategy(StrategyResult::Abstain),
self::generateStrategy(StrategyResult::Grant),
self::generateStrategy(StrategyResult::Deny),
],
StrategyResult::Deny,
];
}

/**
* @dataProvider generatesValidStrategies
*
* @param iterable<StrategyInterface> $strategies
*/
public function testItComputesCorrectly(iterable $strategies, StrategyResult $expected): void
{
$affirmativeStrategy = new UnanimousStrategy($strategies);

self::assertSame($expected, $affirmativeStrategy->compute());
}
}