|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
3 | 12 | namespace Symfony\Bundle\FrameworkBundle\Command; |
4 | 13 |
|
5 | | -use Symfony\Bundle\FrameworkBundle\Secret\SecretStorageInterface; |
| 14 | +use Symfony\Bundle\FrameworkBundle\Exception\EncryptionKeyNotFoundException; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Secret\Storage\MutableSecretStorageInterface; |
6 | 16 | use Symfony\Component\Console\Command\Command; |
7 | | -use Symfony\Component\Console\Helper\QuestionHelper; |
8 | 17 | use Symfony\Component\Console\Input\InputArgument; |
9 | 18 | use Symfony\Component\Console\Input\InputInterface; |
10 | 19 | use Symfony\Component\Console\Output\OutputInterface; |
11 | | -use Symfony\Component\Console\Question\Question; |
| 20 | +use Symfony\Component\Console\Style\SymfonyStyle; |
12 | 21 |
|
| 22 | +/** |
| 23 | + * @author Tobias Schultze <http://tobion.de> |
| 24 | + * @author Jérémy Derussé <jeremy@derusse.com> |
| 25 | + */ |
13 | 26 | final class SecretsAddCommand extends Command |
14 | 27 | { |
15 | 28 | protected static $defaultName = 'secrets:add'; |
16 | 29 |
|
17 | | - /** |
18 | | - * @var SecretStorageInterface |
19 | | - */ |
20 | | - private $secretStorage; |
| 30 | + private $secretsStorage; |
21 | 31 |
|
22 | | - public function __construct(SecretStorageInterface $secretStorage) |
| 32 | + public function __construct(MutableSecretStorageInterface $secretsStorage) |
23 | 33 | { |
24 | | - $this->secretStorage = $secretStorage; |
| 34 | + $this->secretsStorage = $secretsStorage; |
25 | 35 |
|
26 | 36 | parent::__construct(); |
27 | 37 | } |
28 | 38 |
|
29 | 39 | protected function configure() |
30 | 40 | { |
31 | 41 | $this |
32 | | - ->setDescription('Adds a secret with the key.') |
33 | | - ->addArgument( |
34 | | - 'key', |
35 | | - InputArgument::REQUIRED |
36 | | - ) |
37 | | - ->addArgument( |
38 | | - 'secret', |
39 | | - InputArgument::REQUIRED |
| 42 | + ->setDefinition([ |
| 43 | + new InputArgument('name', InputArgument::REQUIRED, 'The name of the secret'), |
| 44 | + ]) |
| 45 | + ->setDescription('Adds a secret in the storage.') |
| 46 | + ->setHelp(<<<'EOF' |
| 47 | +The <info>%command.name%</info> command stores a secret. |
| 48 | +
|
| 49 | + %command.full_name% <name> |
| 50 | +EOF |
40 | 51 | ) |
41 | 52 | ; |
42 | 53 | } |
43 | 54 |
|
44 | 55 | protected function execute(InputInterface $input, OutputInterface $output) |
45 | 56 | { |
46 | | - $key = $input->getArgument('key'); |
47 | | - $secret = $input->getArgument('secret'); |
48 | | - |
49 | | - $this->secretStorage->putSecret($key, $secret); |
50 | | - } |
51 | | - |
52 | | - protected function interact(InputInterface $input, OutputInterface $output) |
53 | | - { |
54 | | - /** @var QuestionHelper $helper */ |
55 | | - $helper = $this->getHelper('question'); |
56 | | - |
57 | | - $question = new Question('Key of the secret: ', $input->getArgument('key')); |
| 57 | + $io = new SymfonyStyle($input, $output); |
58 | 58 |
|
59 | | - $key = $helper->ask($input, $output, $question); |
60 | | - $input->setArgument('key', $key); |
| 59 | + $name = $input->getArgument('name'); |
| 60 | + $secret = $io->askHidden('Value of the secret'); |
61 | 61 |
|
62 | | - $question = new Question('Plaintext secret value: ', $input->getArgument('secret')); |
63 | | - $question->setHidden(true); |
| 62 | + try { |
| 63 | + $this->secretsStorage->setSecret($name, $secret); |
| 64 | + } catch (EncryptionKeyNotFoundException $e) { |
| 65 | + throw new \LogicException(sprintf('No encryption keys found. You should call the "%s" command.', SecretsGenerateKeyCommand::getDefaultName())); |
| 66 | + } |
64 | 67 |
|
65 | | - $secret = $helper->ask($input, $output, $question); |
66 | | - $input->setArgument('secret', $secret); |
| 68 | + $io->success('Secret was successfully stored.'); |
67 | 69 | } |
68 | 70 | } |
0 commit comments