-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright 2022 to Present Joshua Estes | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do | ||
so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<div align="center"> | ||
<img src="https://raw.githubusercontent.com/SonsOfPHP/.github/main/assets/top-rocker.png" /> | ||
</div> | ||
<br/> | ||
<div align="center"> | ||
<a href="https://codecov.io/github/SonsOfPHP/sonsofphp"><img src="https://codecov.io/github/SonsOfPHP/sonsofphp/graph/badge.svg?token=VZ2FVOUKUW" /></a> | ||
<img src="https://img.shields.io/packagist/l/sonsofphp/sonsofphp" /> | ||
<img src="https://img.shields.io/packagist/v/sonsofphp/sonsofphp" /> | ||
</div> | ||
|
||
# Sons of PHP | ||
|
||
Sons of PHP is builds reusable components and tools using PHP. | ||
|
||
* Documentation can be found at [docs.SonsOfPHP.com][docs] | ||
* Please [Report Issues][issues] and send [Pull Requests][pull-requests] in the [Mother Repository][mother-repo] | ||
* You can get more help by posting questions in the [GitHub Discussions][discussions] | ||
|
||
[mother-repo]: <https://github.com/SonsOfPHP/sonsofphp> "Sons of PHP Mother Repository" | ||
[discussions]: https://github.com/orgs/SonsOfPHP/discussions | ||
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues | ||
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls | ||
[docs]: https://docs.sonsofphp.com |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"name": "sonsofphp/feature-toggle-bundle", | ||
"type": "symfony-bundle", | ||
"description": "Sons of PHP | Feature Toggle Bundle", | ||
"keywords": [ | ||
"sonsofphp", | ||
"sons of php", | ||
"feature", | ||
"toggle", | ||
"feature toggle", | ||
"symfony" | ||
], | ||
"homepage": "https://github.com/SonsOfPHP/feature-toggle-bundle", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Joshua Estes", | ||
"email": "joshua@sonsofphp.com" | ||
} | ||
], | ||
"support": { | ||
"issues": "https://github.com/SonsOfPHP/sonsofphp/issues", | ||
"forum": "https://github.com/orgs/SonsOfPHP/discussions", | ||
"docs": "https://docs.sonsofphp.com/symfony-bundles/feature-toggle" | ||
}, | ||
"funding": [ | ||
{ | ||
"type": "github", | ||
"url": "https://github.com/sponsors/JoshuaEstes" | ||
}, | ||
{ | ||
"type": "tidelift", | ||
"url": "https://tidelift.com/subscription/pkg/packagist-sonsofphp-sonsofphp" | ||
} | ||
], | ||
"require": { | ||
"php": ">=8.2", | ||
"sonsofphp/feature-toggle": "^0.3@dev" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SonsOfPHP\\Bundle\\FeatureToggleBundle\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"SonsOfPHP\\Bundle\\FeatureToggleBundle\\Tests\\": "tests/" | ||
} | ||
}, | ||
"extra": { | ||
"sort-packages": true, | ||
"branch-alias": { | ||
"dev-main": "0.3.x-dev" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
services: | ||
SonsOfPHP\Bundle\FeatureToggleBundle\Command\DebugCommand: | ||
arguments: ['@sons_of_php.feature_toggle.provider'] | ||
tags: | ||
- { name: 'console.command' } | ||
|
||
SonsOfPHP\Bundle\FeatureToggleBundle\Twig\Extension\FeatureToggleExtension: | ||
tags: | ||
- { name: 'twig.extension' } | ||
|
||
SonsOfPHP\Bundle\FeatureToggleBundle\Twig\Runtime\FeatureToggleExtensionRuntime: | ||
arguments: ['@sons_of_php.feature_toggle.provider'] | ||
tags: | ||
- { name: 'twig.runtime' } | ||
|
||
SonsOfPHP\Component\FeatureToggle\Toggle\AlwaysDisabledToggle: | ||
tags: | ||
- { name: 'sons_of_php.feature_toggle.toggle' } | ||
|
||
sons_of_php.feature_toggle.toggle.disabled: | ||
alias: SonsOfPHP\Component\FeatureToggle\Toggle\AlwaysDisabledToggle | ||
public: true | ||
|
||
SonsOfPHP\Component\FeatureToggle\Toggle\AlwaysEnabledToggle: | ||
tags: | ||
- { name: 'sons_of_php.feature_toggle.toggle' } | ||
|
||
sons_of_php.feature_toggle.toggle.enabled: | ||
alias: SonsOfPHP\Component\FeatureToggle\Toggle\AlwaysEnabledToggle | ||
public: true | ||
|
||
# The provider contains all the different features and their toggles Default | ||
# provider should be a chain provider so that users can define additional | ||
# providers | ||
SonsOfPHP\Contract\FeatureToggle\FeatureToggleProviderInterface: | ||
class: SonsOfPHP\Component\FeatureToggle\Provider\InMemoryFeatureToggleProvider | ||
|
||
sons_of_php.feature_toggle.provider: | ||
alias: SonsOfPHP\Contract\FeatureToggle\FeatureToggleProviderInterface | ||
public: true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Bundle\FeatureToggleBundle\Attribute; | ||
|
||
final class AsFeature {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Bundle\FeatureToggleBundle\Command; | ||
|
||
use SonsOfPHP\Component\FeatureToggle\Feature; | ||
use SonsOfPHP\Contract\FeatureToggle\FeatureToggleProviderInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
class DebugCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly FeatureToggleProviderInterface $provider, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setName('debug:features') | ||
->setDescription('Debug feature toggles') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$symfonyStyle = new SymfonyStyle($input, $output); | ||
|
||
$header = ['Key', 'Toggle']; | ||
$rows = []; | ||
$toggle = new \ReflectionProperty(Feature::class, 'toggle'); | ||
foreach ($this->provider->all() as $key => $feature) { | ||
$rows[] = [$key, $toggle->getValue($feature)::class]; | ||
} | ||
|
||
$symfonyStyle->table($header, $rows); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Bundle\FeatureToggleBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
final class FeaturePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
Check warning on line 13 in src/SonsOfPHP/Bundle/FeatureToggleBundle/src/DependencyInjection/Compiler/FeaturePass.php Codecov / codecov/patchsrc/SonsOfPHP/Bundle/FeatureToggleBundle/src/DependencyInjection/Compiler/FeaturePass.php#L13
|
||
{ | ||
if (!$container->has('sons_of_php.feature_toggle.provider')) { | ||
return; | ||
Check warning on line 16 in src/SonsOfPHP/Bundle/FeatureToggleBundle/src/DependencyInjection/Compiler/FeaturePass.php Codecov / codecov/patchsrc/SonsOfPHP/Bundle/FeatureToggleBundle/src/DependencyInjection/Compiler/FeaturePass.php#L15-L16
|
||
} | ||
|
||
$provider = $container->findDefinition('sons_of_php.feature_toggle.provider'); | ||
$features = $container->findTaggedServiceIds('sons_of_php.feature_toggle.feature'); | ||
Check warning on line 20 in src/SonsOfPHP/Bundle/FeatureToggleBundle/src/DependencyInjection/Compiler/FeaturePass.php Codecov / codecov/patchsrc/SonsOfPHP/Bundle/FeatureToggleBundle/src/DependencyInjection/Compiler/FeaturePass.php#L19-L20
|
||
|
||
foreach (array_keys($features) as $id) { | ||
$provider->addMethodCall('add', [new Reference($id)]); | ||
Check warning on line 23 in src/SonsOfPHP/Bundle/FeatureToggleBundle/src/DependencyInjection/Compiler/FeaturePass.php Codecov / codecov/patchsrc/SonsOfPHP/Bundle/FeatureToggleBundle/src/DependencyInjection/Compiler/FeaturePass.php#L22-L23
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Bundle\FeatureToggleBundle; | ||
|
||
use SonsOfPHP\Bundle\FeatureToggleBundle\Attribute\AsFeature; | ||
use SonsOfPHP\Bundle\FeatureToggleBundle\DependencyInjection\Compiler\FeaturePass; | ||
use SonsOfPHP\Component\FeatureToggle\Feature; | ||
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\HttpKernel\Bundle\AbstractBundle; | ||
|
||
class SonsOfPHPFeatureToggleBundle extends AbstractBundle | ||
{ | ||
public function configure(DefinitionConfigurator $definition): void | ||
{ | ||
/** | ||
* @todo provider: sons_of_php.feature_toggle.provider | ||
* features: | ||
* key: | ||
* toggle: sons_of_php.feature_toggle.toggle.enabled | ||
* another_key: | ||
* toggle: sons_of_php.feature_toggle.toggle.disabled | ||
*/ | ||
$definition->rootNode()->children() | ||
Check failure on line 28 in src/SonsOfPHP/Bundle/FeatureToggleBundle/src/SonsOfPHPFeatureToggleBundle.php GitHub Actions / Psalm (8.2)UndefinedMethod
|
||
// @todo | ||
//->scalarNode('provider') | ||
// ->defaultValue('sons_of_php.feature_toggle.provider') | ||
//->end() // provider | ||
->arrayNode('features') | ||
->info('Features contains a list of features by "key" and the toggle the feature uses') | ||
->useAttributeAsKey('key') | ||
->arrayPrototype() | ||
->children() | ||
->scalarNode('toggle') | ||
->info('Can be "enabled", "disabled", or a service') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->defaultValue('sons_of_php.feature_toggle.toggle.enabled') | ||
->end() | ||
->end() | ||
->end() | ||
->end() // features | ||
->end(); | ||
} | ||
|
||
public function build(ContainerBuilder $container): void | ||
{ | ||
$container->registerForAutoconfiguration(AsFeature::class)->addTag('sons_of_php.feature_toggle.feature'); | ||
$container->addCompilerPass(new FeaturePass()); | ||
} | ||
|
||
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void | ||
{ | ||
$container->import('../config/services.yaml'); | ||
|
||
foreach ($config['features'] as $key => $value) { | ||
$value['toggle'] = match ($value['toggle']) { | ||
'enabled' => 'sons_of_php.feature_toggle.toggle.enabled', | ||
'disabled' => 'sons_of_php.feature_toggle.toggle.disabled', | ||
default => $value['toggle'], | ||
}; | ||
|
||
$feature = $container->services()->set('sons_of_php.feature_toggle.feature.' . $key, Feature::class); | ||
$feature->arg(0, $key); | ||
$feature->arg(1, new Reference($value['toggle'])); | ||
$feature->tag('sons_of_php.feature_toggle.feature'); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Bundle\FeatureToggleBundle\Twig\Extension; | ||
|
||
use SonsOfPHP\Bundle\FeatureToggleBundle\Twig\Runtime\FeatureToggleExtensionRuntime; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
final class FeatureToggleExtension extends AbstractExtension | ||
{ | ||
public function getFunctions(): array | ||
Check warning on line 13 in src/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Extension/FeatureToggleExtension.php Codecov / codecov/patchsrc/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Extension/FeatureToggleExtension.php#L13
|
||
{ | ||
return [ | ||
new TwigFunction('is_feature_enabled', [FeatureToggleExtensionRuntime::class, 'isEnabled']), | ||
]; | ||
Check warning on line 17 in src/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Extension/FeatureToggleExtension.php Codecov / codecov/patchsrc/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Extension/FeatureToggleExtension.php#L15-L17
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Bundle\FeatureToggleBundle\Twig\Runtime; | ||
|
||
use SonsOfPHP\Contract\FeatureToggle\FeatureToggleProviderInterface; | ||
use Twig\Extension\RuntimeExtensionInterface; | ||
|
||
final readonly class FeatureToggleExtensionRuntime implements RuntimeExtensionInterface | ||
{ | ||
public function __construct( | ||
Check warning on line 12 in src/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Runtime/FeatureToggleExtensionRuntime.php Codecov / codecov/patchsrc/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Runtime/FeatureToggleExtensionRuntime.php#L12
|
||
private FeatureToggleProviderInterface $provider, | ||
) {} | ||
Check warning on line 14 in src/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Runtime/FeatureToggleExtensionRuntime.php Codecov / codecov/patchsrc/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Runtime/FeatureToggleExtensionRuntime.php#L14
|
||
|
||
public function isEnabled(string $key): bool | ||
Check warning on line 16 in src/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Runtime/FeatureToggleExtensionRuntime.php Codecov / codecov/patchsrc/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Runtime/FeatureToggleExtensionRuntime.php#L16
|
||
{ | ||
return $this->provider->get($key)->isEnabled(); | ||
Check warning on line 18 in src/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Runtime/FeatureToggleExtensionRuntime.php Codecov / codecov/patchsrc/SonsOfPHP/Bundle/FeatureToggleBundle/src/Twig/Runtime/FeatureToggleExtensionRuntime.php#L18
|
||
} | ||
} |