-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FeatureFlag] Propose a simple version #53213
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
Jean-Beru
wants to merge
46
commits into
symfony:7.4
Choose a base branch
from
Jean-Beru:rfc/simple-feature-flag
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
91d08d4
[FeatureFlag] Propose a simple version
Jean-Beru 92596b3
extract FrameworkBundle integration
Jean-Beru a9431cb
fix typos, doc and default feature value
Jean-Beru 978f100
review
Jean-Beru d7ee4d6
add some tests
Jean-Beru 2e1e2e5
happy new year
Jean-Beru c6f5df9
cs
Jean-Beru 5daeb43
add base exception
Jean-Beru 2d41a57
refact DataCollector
Jean-Beru b2e690c
add isDisabled
Jean-Beru 11b2a36
fix collector
Jean-Beru 23d41e4
fix forgotten method in FeatureCheckerInterface
Jean-Beru d0b1914
fix interface
Jean-Beru 13ea142
remove exception catching
Jean-Beru 6fd6e9e
[FeatureFlag] Add FrameworkBundle integration
Jean-Beru fc6adc3
exception format
Jean-Beru dc4a8dd
cs
Jean-Beru 911cdc9
add Neirda24 as co-author
Jean-Beru 86f584b
add ContainerInterface dependency
Jean-Beru 70bb733
fix exception namespace
Jean-Beru e1c2f48
fix namespace again
Jean-Beru e945698
remove merge file
Jean-Beru 09fe655
Fix bundle and add details in profiler
Jean-Beru 28763a0
Add Twig functions to UndefinedCallableHandler::FUNCTION_COMPONENTS
Jean-Beru 1b1f3fd
cs
Jean-Beru 66f83b6
rebase
Jean-Beru 55d47fc
review
Jean-Beru e0dea4c
cs
Jean-Beru 07a7d48
tests
Jean-Beru c7630e2
psalm
Jean-Beru 5832442
readme
Jean-Beru 75b95fb
add functional tests
Jean-Beru 3d51ae6
fix framework bundle test
Jean-Beru 11c1227
[FeatureFlag] Use providers
Jean-Beru 1643936
cs
Jean-Beru 6378c62
review
Jean-Beru fe338c1
fix bad rebase
Jean-Beru f8c3041
update duplicate feature message
Jean-Beru 0706557
remove expected value
Jean-Beru 1b20951
replace ProviderInterface::has by a nullable ProviderInterface::get
Jean-Beru d3033bf
update README.md
Jean-Beru 20b994c
remove useless exceptions
Jean-Beru 4fc0981
add .github folder
Jean-Beru a042334
review
Jean-Beru 2f490f8
fix rebase
Jean-Beru 802cd2c
add ResetInterface
Jean-Beru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Symfony/Bridge/Twig/Extension/FeatureFlagExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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\Bridge\Twig\Extension; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
final class FeatureFlagExtension extends AbstractExtension | ||
{ | ||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('feature_is_enabled', [FeatureFlagRuntime::class, 'isEnabled']), | ||
new TwigFunction('feature_get_value', [FeatureFlagRuntime::class, 'getValue']), | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?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\Bridge\Twig\Extension; | ||
|
||
use Symfony\Component\FeatureFlag\FeatureCheckerInterface; | ||
|
||
final class FeatureFlagRuntime | ||
{ | ||
public function __construct( | ||
private readonly FeatureCheckerInterface $featureChecker, | ||
) { | ||
} | ||
|
||
public function isEnabled(string $featureName): bool | ||
{ | ||
return $this->featureChecker->isEnabled($featureName); | ||
} | ||
|
||
public function getValue(string $featureName): mixed | ||
{ | ||
return $this->featureChecker->getValue($featureName); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/FeatureFlagPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?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\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\FeatureFlag\Debug\TraceableFeatureChecker; | ||
|
||
class FeatureFlagPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->hasDefinition('feature_flag.feature_checker')) { | ||
return; | ||
} | ||
|
||
$features = []; | ||
foreach ($container->findTaggedServiceIds('feature_flag.feature') as $serviceId => $tags) { | ||
$className = $this->getServiceClass($container, $serviceId); | ||
$r = $container->getReflectionClass($className); | ||
|
||
if (null === $r) { | ||
throw new \RuntimeException(\sprintf('Invalid service "%s": class "%s" does not exist.', $serviceId, $className)); | ||
} | ||
|
||
foreach ($tags as $tag) { | ||
$featureName = ($tag['feature'] ?? '') ?: $className; | ||
if (\array_key_exists($featureName, $features)) { | ||
throw new \RuntimeException(\sprintf('Feature "%s" already defined in the "feature_flag.provider.in_memory" provider.', $featureName)); | ||
} | ||
|
||
$method = $tag['method'] ?? '__invoke'; | ||
if (!$r->hasMethod($method)) { | ||
Jean-Beru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new \RuntimeException(\sprintf('Invalid feature method "%s": method "%s::%s()" does not exist.', $serviceId, $r->getName(), $method)); | ||
} | ||
if (!$r->getMethod($method)->isPublic()) { | ||
throw new \RuntimeException(\sprintf('Invalid feature method "%s": method "%s::%s()" must be public.', $serviceId, $r->getName(), $method)); | ||
} | ||
|
||
$features[$featureName] = $container->setDefinition( | ||
'.feature_flag.feature', | ||
(new Definition(\Closure::class)) | ||
->setLazy(true) | ||
->setFactory([\Closure::class, 'fromCallable']) | ||
->setArguments([[new Reference($serviceId), $method]]), | ||
); | ||
} | ||
} | ||
|
||
$container->getDefinition('feature_flag.provider.in_memory') | ||
->setArgument('$features', $features) | ||
; | ||
|
||
if (!$container->has('feature_flag.data_collector')) { | ||
return; | ||
} | ||
|
||
foreach ($container->findTaggedServiceIds('feature_flag.feature_checker') as $serviceId => $tags) { | ||
$container->register('debug.'.$serviceId, TraceableFeatureChecker::class) | ||
Jean-Beru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
->setDecoratedService($serviceId) | ||
->setArguments([ | ||
'$decorated' => new Reference('.inner'), | ||
'$dataCollector' => new Reference('feature_flag.data_collector'), | ||
]) | ||
; | ||
} | ||
} | ||
|
||
private function getServiceClass(ContainerBuilder $container, string $serviceId): ?string | ||
{ | ||
while (true) { | ||
$definition = $container->findDefinition($serviceId); | ||
|
||
if (!$definition->getClass() && $definition instanceof ChildDefinition) { | ||
$serviceId = $definition->getParent(); | ||
|
||
continue; | ||
} | ||
|
||
return $definition->getClass(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Symfony/Bundle/FrameworkBundle/Resources/config/feature_flag.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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\DependencyInjection\Loader\Configurator; | ||
|
||
use Symfony\Component\FeatureFlag\FeatureChecker; | ||
use Symfony\Component\FeatureFlag\FeatureCheckerInterface; | ||
use Symfony\Component\FeatureFlag\Provider\ChainProvider; | ||
use Symfony\Component\FeatureFlag\Provider\InMemoryProvider; | ||
use Symfony\Component\FeatureFlag\Provider\ProviderInterface; | ||
|
||
return static function (ContainerConfigurator $container) { | ||
$container->services() | ||
|
||
->set('feature_flag.provider.in_memory', InMemoryProvider::class) | ||
->args([ | ||
'$features' => abstract_arg('Defined in FeatureFlagPass.'), | ||
]) | ||
->tag('feature_flag.provider') | ||
|
||
->set('feature_flag.provider', ChainProvider::class) | ||
->args([ | ||
Jean-Beru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'$providers' => tagged_iterator('feature_flag.provider'), | ||
]) | ||
->alias(ProviderInterface::class, 'feature_flag.provider') | ||
|
||
->set('feature_flag.feature_checker', FeatureChecker::class) | ||
->args([ | ||
'$provider' => service('feature_flag.provider'), | ||
]) | ||
->alias(FeatureCheckerInterface::class, 'feature_flag.feature_checker') | ||
; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.