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

Remove unused features, and simplify configuration structure #2

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
79 changes: 29 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,65 +36,44 @@ composer require yokai/safe-command-bundle

return [
// ...
Yokai\SafeCommandBundle\YokaiSafeCommandBundle::class => ['all' => true],
Yokai\SafeCommandBundle\YokaiSafeCommandBundle::class => ['prod' => true],
];
```

> [!NOTE]
> The bundle is enabled only for `prod` here, but you are free to do whatever you want.

### Configuration

The bundle comes with some commands disabled by default (from Symfony's standards).

That "standard" command list can be overridden:
```
# config/packages/yokai_safe_command.yaml
when@prod:
yokai_safe_command:
standard: []
```

> [!NOTE]
> "standard" disabled commands are viewable via the command:
> ```
> bin/console config:dump-reference yokai_safe_command
> ```

And you can also add your own commands to the list:
```
# config/packages/yokai_safe_command.yaml
yokai_safe_command:
enabled: true
commands:
enabled: true
config:
enabled: true
commands:
- 'config:dump-reference'
doctrine:
enabled: true
commands:
- 'doctrine:database:drop'
- 'doctrine:mapping:convert'
- 'doctrine:mapping:import'
- 'doctrine:schema:drop'
- 'doctrine:schema:validate'
debug:
enabled: true
commands:
- 'debug:config'
- 'debug:container'
- 'debug:event-dispatcher'
- 'debug:router'
- 'debug:swiftmailer'
- 'debug:translation'
- 'debug:twig'
lint:
enabled: true
commands:
- 'lint:twig'
- 'lint:yaml'
server:
enabled: true
commands:
- 'server:run'
- 'server:start'
- 'server:status'
- 'server:stop'
translation:
enabled: true
commands:
- 'translation:update'
misc:
enabled: true
commands: { }
environments:
enabled: false
environments:
- prod
when@prod:
yokai_safe_command:
custom:
- 'vendor:my:dev-command'
- 'app:my:dev-command'
```

> [!NOTE]
> `standard` and `custom` configs are merged together to create the final list of disabled commands.


## License

Expand Down
6 changes: 0 additions & 6 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
<tag name="kernel.event_subscriber"/>
</service>

<service id="yokai_safe_command.event_listener.prevent_command_from_being_used_with_disabled_environment_listener"
class="Yokai\SafeCommandBundle\EventListener\PreventCommandFromBeingUsedWithDisabledEnvironmentListener">
<argument>%yokai_safe_command.disabled_commands%</argument>
<tag name="kernel.event_subscriber"/>
</service>

</services>

</container>
51 changes: 37 additions & 14 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,46 @@ public function getConfigTreeBuilder(): TreeBuilder
$root = $tree->getRootNode();

$root
->canBeDisabled()
->addDefaultsIfNotSet()
->children()
->arrayNode('commands')
->canBeDisabled()
->addDefaultsIfNotSet()
->children()
->append($this->createCommandsNode('config'))
->append($this->createCommandsNode('doctrine'))
->append($this->createCommandsNode('debug'))
->append($this->createCommandsNode('lint'))
->append($this->createCommandsNode('server'))
->append($this->createCommandsNode('translation'))
->append($this->createCommandsNode('misc'))
->end()
->arrayNode('standard')
->defaultValue([
'config:dump-reference',
'doctrine:database:drop',
'doctrine:mapping:convert',
'doctrine:mapping:import',
'doctrine:schema:drop',
'doctrine:schema:validate',
'debug:autowiring',
'debug:config',
'debug:container',
'debug:dotenv',
'debug:event-dispatcher',
'debug:firewall',
'debug:form',
'debug:router',
'debug:serializer',
'debug:translation',
'debug:twig',
'debug:validator',
'lint:container',
'lint:twig',
'lint:xliff',
'lint:yaml',
'server:dump',
'server:log',
'translation:extract',
'translation:pull',
'translation:push',
])
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->arrayNode('custom')
->defaultValue([])
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->append($this->createEnvironmentsNode())
->end()
;

Expand Down
57 changes: 4 additions & 53 deletions src/DependencyInjection/YokaiSafeCommandExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,13 @@ public function load(array $configs, ContainerBuilder $container): void
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

if (!$config['enabled']) {
return;
}

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader->load('services.xml');

$this->defineDisabledCommands($config['commands'], $container);
$this->defineAllowedEnvironments($config['environments'], $container);
}

private function defineDisabledCommands(array $config, ContainerBuilder $container): void
{
if (!$config['enabled']) {
$container->removeDefinition(
'yokai_safe_command.event_listener.prevent_disabled_command_from_being_used_listener'
);

return;
}

// collect commands over config sections
$commands = [];
foreach ($config as $value) {
if (!\is_array($value)) {
continue;
}
if (!isset($value['enabled']) || !$value['enabled']) {
continue;
}
if (!isset($value['commands'])) {
continue;
}

$commands = array_merge($commands, $value['commands']);
}

// format commands
$commands = array_unique($commands);
sort($commands);

// set disabled commands parameter
$commands = array_unique([
...$config['standard'],
...$config['custom'],
]);
$container->setParameter('yokai_safe_command.disabled_commands', $commands);
}

private function defineAllowedEnvironments(array $config, ContainerBuilder $container): void
{
if (!$config['enabled']) {
$container->removeDefinition(
'yokai_safe_command.event_listener.prevent_command_from_being_used_with_disabled_environment_listener'
);

return;
}

// set allowed environments parameter
$container->setParameter('yokai_safe_command.allowed_environments', $config['environments']);
}
}

This file was deleted.

16 changes: 16 additions & 0 deletions tests/CommandDisabledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Yokai\SafeCommandBundle\Tests;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\NullOutput;

final class CommandDisabledTest extends CommandTestCase
{
Expand All @@ -19,6 +21,8 @@ public function disabled_command_should_not_appear_in_list_command_output(): voi

$out = $output->fetch();

self::assertMatchesRegularExpression('/cache:clear/', $out, 'Some commands are still viewable.');

foreach (self::commands() as [$command]) {
self::assertDoesNotMatchRegularExpression(
'/' . $command . '/',
Expand Down Expand Up @@ -51,6 +55,18 @@ public function disabled_command_should_not_run(string $command): void
);
}

/**
* @test
*/
public function enabled_command_should_run(): void
{
$application = self::createApplication();

$exit = $application->run(new StringInput('cache:clear'), new NullOutput());

self::assertSame(Command::SUCCESS, $exit, 'Some commands are still runnable');
}

public static function commands(): \Generator
{
yield ['debug:container'];
Expand Down
34 changes: 0 additions & 34 deletions tests/EnvironmentAllowedTest.php

This file was deleted.

Loading
Loading