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

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\Command\Command;
  13. use Symfony\Component\Console\Completion\CompletionInput;
  14. use Symfony\Component\Console\Completion\CompletionSuggestions;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Symfony\Component\Console\Style\SymfonyStyle;
  21. /**
  22.  * @author Jérémy Derussé <jeremy@derusse.com>
  23.  * @author Nicolas Grekas <p@tchwork.com>
  24.  *
  25.  * @internal
  26.  */
  27. final class SecretsRemoveCommand extends Command
  28. {
  29.     protected static $defaultName 'secrets:remove';
  30.     protected static $defaultDescription 'Remove a secret from the vault';
  31.     private $vault;
  32.     private $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.             ->setDescription(self::$defaultDescription)
  43.             ->addArgument('name'InputArgument::REQUIRED'The name of the secret')
  44.             ->addOption('local''l'InputOption::VALUE_NONE'Update the local vault.')
  45.             ->setHelp(<<<'EOF'
  46. The <info>%command.name%</info> command removes a secret from the vault.
  47.     <info>%command.full_name% <name></info>
  48. EOF
  49.             )
  50.         ;
  51.     }
  52.     protected function execute(InputInterface $inputOutputInterface $output): int
  53.     {
  54.         $io = new SymfonyStyle($input$output instanceof ConsoleOutputInterface $output->getErrorOutput() : $output);
  55.         $vault $input->getOption('local') ? $this->localVault $this->vault;
  56.         if (null === $vault) {
  57.             $io->success('The local vault is disabled.');
  58.             return 1;
  59.         }
  60.         if ($vault->remove($name $input->getArgument('name'))) {
  61.             $io->success($vault->getLastMessage() ?? 'Secret was removed from the vault.');
  62.         } else {
  63.             $io->comment($vault->getLastMessage() ?? 'Secret was not found in the vault.');
  64.         }
  65.         if ($this->vault === $vault && null !== $this->localVault->reveal($name)) {
  66.             $io->comment('Note that this secret is overridden in the local vault.');
  67.         }
  68.         return 0;
  69.     }
  70.     public function complete(CompletionInput $inputCompletionSuggestions $suggestions): void
  71.     {
  72.         if (!$input->mustSuggestArgumentValuesFor('name')) {
  73.             return;
  74.         }
  75.         $vaultKeys array_keys($this->vault->list(false));
  76.         if ($input->getOption('local')) {
  77.             if (null === $this->localVault) {
  78.                 return;
  79.             }
  80.             $vaultKeys array_intersect($vaultKeysarray_keys($this->localVault->list(false)));
  81.         }
  82.         $suggestions->suggestValues($vaultKeys);
  83.     }
  84. }