vendor/symfony/framework-bundle/Command/SecretsRemoveCommand.php line 38

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Bundle\FrameworkBundle\Secrets\AbstractVault;
  12. use Symfony\Component\Console\Attribute\AsCommand;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Completion\CompletionInput;
  15. use Symfony\Component\Console\Completion\CompletionSuggestions;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Input\InputOption;
  19. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. use Symfony\Component\Console\Style\SymfonyStyle;
  22. /**
  23.  * @author Jérémy Derussé <jeremy@derusse.com>
  24.  * @author Nicolas Grekas <p@tchwork.com>
  25.  *
  26.  * @internal
  27.  */
  28. #[AsCommand(name'secrets:remove'description'Remove a secret from the vault')]
  29. final class SecretsRemoveCommand extends Command
  30. {
  31.     private AbstractVault $vault;
  32.     private ?AbstractVault $localVault;
  33.     public function __construct(AbstractVault $vaultAbstractVault $localVault null)
  34.     {
  35.         $this->vault $vault;
  36.         $this->localVault $localVault;
  37.         parent::__construct();
  38.     }
  39.     protected function configure()
  40.     {
  41.         $this
  42.             ->addArgument('name'InputArgument::REQUIRED'The name of the secret')
  43.             ->addOption('local''l'InputOption::VALUE_NONE'Update the local vault.')
  44.             ->setHelp(<<<'EOF'
  45. The <info>%command.name%</info> command removes a secret from the vault.
  46.     <info>%command.full_name% <name></info>
  47. EOF
  48.             )
  49.         ;
  50.     }
  51.     protected function execute(InputInterface $inputOutputInterface $output): int
  52.     {
  53.         $io = new SymfonyStyle($input$output instanceof ConsoleOutputInterface $output->getErrorOutput() : $output);
  54.         $vault $input->getOption('local') ? $this->localVault $this->vault;
  55.         if (null === $vault) {
  56.             $io->success('The local vault is disabled.');
  57.             return 1;
  58.         }
  59.         if ($vault->remove($name $input->getArgument('name'))) {
  60.             $io->success($vault->getLastMessage() ?? 'Secret was removed from the vault.');
  61.         } else {
  62.             $io->comment($vault->getLastMessage() ?? 'Secret was not found in the vault.');
  63.         }
  64.         if ($this->vault === $vault && null !== $this->localVault->reveal($name)) {
  65.             $io->comment('Note that this secret is overridden in the local vault.');
  66.         }
  67.         return 0;
  68.     }
  69.     public function complete(CompletionInput $inputCompletionSuggestions $suggestions): void
  70.     {
  71.         if (!$input->mustSuggestArgumentValuesFor('name')) {
  72.             return;
  73.         }
  74.         $vaultKeys array_keys($this->vault->list(false));
  75.         if ($input->getOption('local')) {
  76.             if (null === $this->localVault) {
  77.                 return;
  78.             }
  79.             $vaultKeys array_intersect($vaultKeysarray_keys($this->localVault->list(false)));
  80.         }
  81.         $suggestions->suggestValues($vaultKeys);
  82.     }
  83. }