Linux SRV-16 6.18.44-paas #1 SMP PREEMPT_DYNAMIC Thu Aug 13 10:08:12 UTC 2026 x86_64
/
srv
/
data
/
trash
/
vhost_www.bachtale.com_1719306535
/
htdocs
/
src
/
Command
/
/srv/data/trash/vhost_www.bachtale.com_1719306535/htdocs/src/Command/CreateUserCommand.php
<?php namespace App\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Command\Command; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; use Doctrine\ORM\EntityManagerInterface; use App\Entity\User; use Symfony\Component\Validator\Validator\ValidatorInterface; class CreateUserCommand extends Command { private $em; private $passwordEncoder; private $validator; public function __construct(UserPasswordEncoderInterface $passwordEncoder, EntityManagerInterface $em, ValidatorInterface $validator) { $this->em = $em; $this->passwordEncoder = $passwordEncoder; $this->validator = $validator; parent::__construct(); } protected function configure() { $this ->setName('make:user') ->setDescription('Create a user.') ->setDefinition(array( new InputArgument('username', InputArgument::REQUIRED, 'The username'), new InputArgument('email', InputArgument::REQUIRED, 'The email'), new InputArgument('password', InputArgument::REQUIRED, 'The password'), )) ; } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $user = new User(); $username = $input->getArgument('username'); $email = $input->getArgument('email'); $password = $input->getArgument('password'); $password = $this->passwordEncoder->encodePassword($user, $password); $user->setUsername($username); $user->setEmail($email); $user->setPassword($password); $errors = $this->validator->validate($user); if (count($errors) > 0) { $errorsString = (string) $errors; throw new \Exception($errorsString); } $this->em->persist($user); $this->em->flush(); $output->writeln(sprintf('Created user %s', $username)); } /** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $questions = array(); if (!$input->getArgument('username')) { $question = new Question('Please choose a username:'); $questions['username'] = $question; } if (!$input->getArgument('email')) { $question = new Question('Please choose an email:'); $questions['email'] = $question; } if (!$input->getArgument('password')) { $question = new Question('Please choose a password:'); $question->setHidden(true); $question->setHiddenFallback(false); $questions['password'] = $question; } foreach ($questions as $name => $question) { $answer = $this->getHelper('question')->ask($input, $output, $question); $input->setArgument($name, $answer); } } }