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