Skip to content

Commit

Permalink
Add command to list available features
Browse files Browse the repository at this point in the history
console progressive:features

Available:
  dark-theme
  light-theme
  homepage-v2

console progressive:features homepage-v2

Name:
  homepage-v2

Config:
  unanimous:
    roles: ROLE-DEV
    env: dev, preprod
  • Loading branch information
antfroger committed Jan 18, 2021
1 parent edf1324 commit 9e78c16
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 4,876 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor/
.php_cs.cache
composer.lock
.phpunit.xml
.phpunit.result.cache
.php_cs.cache
105 changes: 105 additions & 0 deletions Command/ListFeaturesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Af\ProgressiveBundle\Command;

use Progressive\Progressive;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ListFeaturesCommand extends Command
{
private $progressive;
protected static $defaultName = 'progressive:features';

public function __construct(Progressive $progressive)
{
parent::__construct();

$this->progressive = $progressive;
}

protected function configure()
{
$this
->setDescription('List the available features.')
->setHelp(
<<<'EOF'
The <info>%command.name%</info> command lists all the features defined in Progressive:
<info>php %command.full_name%</info>
You can also display the feature's configuration:
<info>php %command.full_name% feature-name</info>
EOF
)
->addArgument('feature', InputArgument::OPTIONAL, "The feature's name")
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$feature = $input->getArgument('feature');

return $feature ?
$this->describeFeature($output, $feature) :
$this->describeAll($output);
}

private function describeAll(OutputInterface $output)
{
$output->writeln('<comment>Available features:</comment>');

foreach ($this->progressive->all() as $feature => $config) {
$output->writeln(sprintf(' <info>%s</info>', $feature));
}

return Command::SUCCESS;
}

private function describeFeature(OutputInterface $output, string $feature)
{
$config = $this->progressive->all();

if (!array_key_exists($feature, $config)) {
return Command::FAILURE;
}

$config = $config[$feature];
// Short syntax
if (is_bool($config)) {
$config = ['enabled' => $config];
}

$output->writeln('<comment>Name:</comment>');
$output->writeln(sprintf(' %s%s', $feature, PHP_EOL));

$output->writeln('<comment>Config:</comment>');

return $this->describeConfig($output, $config);
}

private function describeConfig(OutputInterface $output, array $config, int $level = 1)
{
foreach ($config as $key => $values) {
$output->write(sprintf('%s<info>%s:</info>', str_repeat(' ', $level * 2), $key));

if (is_array($values)) {
if (!is_int(key($values))) {
$output->writeln('');
$this->describeConfig($output, $values, ++$level);
--$level;
continue;
}

$values = implode(', ', $values);
}

$output->writeln(sprintf(' %s', $values));
}

return Command::SUCCESS;
}
}
4 changes: 3 additions & 1 deletion DependencyInjection/AfProgressiveExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function load(array $configs, ContainerBuilder $container)
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.xml');
$loader->load('progressive.xml');
$loader->load('twig.xml');
$loader->load('console.xml');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,30 @@ Progressive comes with two built-in rules:
[`unanimous`](https://github.com/antfroger/progressive#unanimous-) is a strategy (a combinaison of several rules). It enables the feature if all the conditions are met / if all the rules are true.
* `partial: []`
[`patial`](https://github.com/antfroger/progressive#partial-) is also a strategy. It enables the feature if only one of the conditions is met / if at least one of the rules is true.

## Commands

### `progressive:features`

The command `progressive:features` lists all the features configured in Progressive:

```bash
$ php bin/console progressive:features
Available features:
dark-theme
homepage-v2
customer-service-chat
```

If you specify the name of the feature, its configuration will be displayed:

```bash
$ php bin/console progressive:features dark-theme
Name:
dark-theme
Config:
unanimous:
env: dev, preprod
roles: ROLE_DEV
```
13 changes: 13 additions & 0 deletions Resources/config/console.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="Af\ProgressiveBundle\Command\ListFeaturesCommand">
<argument type="service" id="Progressive\Progressive"/>
<tag name="console.command"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,5 @@
<argument type="service" id="security.helper"/>
<tag name="af_progressive.rule" />
</service>

<service id="Af\ProgressiveBundle\Twig\ProgressiveExtension">
<argument type="service" id="Progressive\Progressive"/>
<tag name="twig.extension"/>
</service>
</services>
</container>
13 changes: 13 additions & 0 deletions Resources/config/twig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="Af\ProgressiveBundle\Twig\ProgressiveExtension">
<argument type="service" id="Progressive\Progressive"/>
<tag name="twig.extension"/>
</service>
</services>
</container>
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"require": {
"antfroger/progressive": "^1.0",
"symfony/config": "^5.2",
"symfony/console": "^5.2",
"symfony/dependency-injection": "^5.2",
"symfony/http-kernel": "^5.2",
"symfony/security-core": "^5.2",
Expand Down
Loading

0 comments on commit 9e78c16

Please sign in to comment.