This commit is contained in:
2024-11-20 17:09:09 +01:00
parent d8882c93b9
commit ce7f5a1b4a
9 changed files with 275 additions and 57 deletions

View File

@ -2,6 +2,9 @@
namespace App\Command;
use App\Entity\Company;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
@ -9,41 +12,63 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
#[AsCommand(
name: 'app:init',
description: 'Add a short description for your command',
description: 'Initialisation of the app',
)]
class InitCommand extends Command
{
public function __construct()
{
parent::__construct();
}
private EntityManagerInterface $em;
private ParameterBagInterface $params;
private UserPasswordHasherInterface $passwordHasher;
protected function configure(): void
public function __construct(EntityManagerInterface $em,ParameterBagInterface $params,UserPasswordHasherInterface $passwordHasher)
{
$this
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
;
$this->em = $em;
$this->params = $params;
$this->passwordHasher = $passwordHasher;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$arg1 = $input->getArgument('arg1');
$io->title("APP:INIT");
$io->text("Initialisation of the app");
$io->text("");
// Création du compte admin
$io->text("> Création du compte admin");
$user = $this->em->getRepository("App\Entity\User")->findOneBy(["username"=>"admin"]);
if(!$user) {
$user=new User;
if ($arg1) {
$io->note(sprintf('You passed an argument: %s', $arg1));
$hashedPassword = $this->passwordHasher->hashPassword(
$user,
$this->params->get("appSecret")
);
$user->setUsername("admin");
$user->setPassword($hashedPassword);
$this->em->persist($user);
$this->em->flush();
}
if ($input->getOption('option1')) {
// ...
// Création d'un company par defaut
$io->text("> Création d'un company par defaut");
$nbcompanys = $this->em->getRepository("App\Entity\Company")->count([]);
if($nbcompanys==0) {
$company=new Company;
$company->setTitle($this->params->get("appName"));
$company->setLogo("logo.png");
$this->em->persist($company);
$this->em->flush();
}
$io->success('You have a new command! Now make it your own! Pass --help to see your options.');
return Command::SUCCESS;
}
}