795e005adacc6081c05557752dccaefabbe40d3a
[yaffs-website] / vendor / drupal / console / src / Command / User / PasswordHashCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\User\PasswordHashCommand.
6  */
7
8 namespace Drupal\Console\Command\User;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Core\Command\Command;
14 use Drupal\Core\Password\PasswordInterface;
15
16 class PasswordHashCommand extends Command
17 {
18     /**
19      * @var PasswordInterface
20      */
21     protected $password;
22
23     /**
24      * PasswordHashCommand constructor.
25      *
26      * @param PasswordInterface $password
27      */
28     public function __construct(PasswordInterface $password)
29     {
30         $this->password = $password;
31         parent::__construct();
32     }
33
34     /**
35      * {@inheritdoc}
36      */
37     protected function configure()
38     {
39         $this
40             ->setName('user:password:hash')
41             ->setDescription($this->trans('commands.user.password.hash.description'))
42             ->setHelp($this->trans('commands.user.password.hash.help'))
43             ->addArgument(
44                 'password',
45                 InputArgument::IS_ARRAY,
46                 $this->trans('commands.user.password.hash.options.password')
47             )
48             ->setAliases(['uph']);
49     }
50
51     /**
52      * {@inheritdoc}
53      */
54     protected function interact(InputInterface $input, OutputInterface $output)
55     {
56         $password = $input->getArgument('password');
57         if (!$password) {
58             $password = $this->getIo()->ask(
59                 $this->trans('commands.user.password.hash.questions.password')
60             );
61
62             $input->setArgument('password', [$password]);
63         }
64     }
65
66     /**
67      * {@inheritdoc}
68      */
69     protected function execute(InputInterface $input, OutputInterface $output)
70     {
71         $passwords = $input->getArgument('password');
72
73         $tableHeader = [
74             $this->trans('commands.user.password.hash.messages.password'),
75             $this->trans('commands.user.password.hash.messages.hash'),
76         ];
77
78         $tableRows = [];
79         foreach ($passwords as $password) {
80             $tableRows[] = [
81                 $password,
82                 $this->password->hash($password),
83             ];
84         }
85
86         $this->getIo()->table($tableHeader, $tableRows, 'compact');
87     }
88 }