vendor/symfony/framework-bundle/Command/SecretsListCommand.php line 37

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\Helper\Dumper;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Symfony\Component\Console\Style\SymfonyStyle;
  20. /**
  21.  * @author Tobias Schultze <http://tobion.de>
  22.  * @author Jérémy Derussé <jeremy@derusse.com>
  23.  * @author Nicolas Grekas <p@tchwork.com>
  24.  *
  25.  * @internal
  26.  */
  27. #[AsCommand(name'secrets:list'description'List all secrets')]
  28. final class SecretsListCommand extends Command
  29. {
  30.     private AbstractVault $vault;
  31.     private ?AbstractVault $localVault;
  32.     public function __construct(AbstractVault $vaultAbstractVault $localVault null)
  33.     {
  34.         $this->vault $vault;
  35.         $this->localVault $localVault;
  36.         parent::__construct();
  37.     }
  38.     protected function configure()
  39.     {
  40.         $this
  41.             ->addOption('reveal''r'InputOption::VALUE_NONE'Display decrypted values alongside names')
  42.             ->setHelp(<<<'EOF'
  43. The <info>%command.name%</info> command list all stored secrets.
  44.     <info>%command.full_name%</info>
  45. When the option <info>--reveal</info> is provided, the decrypted secrets are also displayed.
  46.     <info>%command.full_name% --reveal</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.         $io->comment('Use <info>"%env(<name>)%"</info> to reference a secret in a config file.');
  55.         if (!$reveal $input->getOption('reveal')) {
  56.             $io->comment(sprintf('To reveal the secrets run <info>php %s %s --reveal</info>'$_SERVER['PHP_SELF'], $this->getName()));
  57.         }
  58.         $secrets $this->vault->list($reveal);
  59.         $localSecrets $this->localVault?->list($reveal);
  60.         $rows = [];
  61.         $dump = new Dumper($output);
  62.         $dump = static function (?string $v) use ($dump) {
  63.             return null === $v '******' $dump($v);
  64.         };
  65.         foreach ($secrets as $name => $value) {
  66.             $rows[$name] = [$name$dump($value)];
  67.         }
  68.         if (null !== $message $this->vault->getLastMessage()) {
  69.             $io->comment($message);
  70.         }
  71.         foreach ($localSecrets ?? [] as $name => $value) {
  72.             if (isset($rows[$name])) {
  73.                 $rows[$name][] = $dump($value);
  74.             }
  75.         }
  76.         if (null !== $this->localVault && null !== $message $this->localVault->getLastMessage()) {
  77.             $io->comment($message);
  78.         }
  79.         (new SymfonyStyle($input$output))
  80.             ->table(['Secret''Value'] + (null !== $localSecrets ? [=> 'Local Value'] : []), $rows);
  81.         $io->comment("Local values override secret values.\nUse <info>secrets:set --local</info> to define them.");
  82.         return 0;
  83.     }
  84. }