102 lines
3.2 KiB
PHP
102 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Command;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
|
use Doctrine\ORM\Id\AssignedGenerator;
|
|
|
|
use App\Entity\User;
|
|
|
|
class AppInitCommand extends Command
|
|
{
|
|
|
|
private $container;
|
|
private $em;
|
|
private $output;
|
|
private $filesystem;
|
|
private $rootlog;
|
|
private $appname;
|
|
private $appmailnoreply;
|
|
|
|
public function __construct(ContainerInterface $container,EntityManagerInterface $em)
|
|
{
|
|
parent::__construct();
|
|
$this->container = $container;
|
|
$this->em = $em;
|
|
}
|
|
|
|
|
|
protected function configure()
|
|
{
|
|
$this
|
|
->setName('app:AppInit')
|
|
->setDescription('Init Data for App')
|
|
->setHelp('This command Init Data App')
|
|
;
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
$this->output = $output;
|
|
$this->filesystem = new Filesystem();
|
|
$this->rootlog = $this->container->get('kernel')->getLogDir()."/";
|
|
$this->appname = $this->container->getParameter('appName');
|
|
$this->appmailnoreply = $this->container->getParameter('appMailnoreply');
|
|
|
|
$output->writeln('APP = Default Data');
|
|
|
|
// Création du compte admin si non existant
|
|
$this->insertUser("admin","admin");
|
|
|
|
$output->writeln('');
|
|
|
|
return 1;
|
|
}
|
|
|
|
protected function insertUser() {
|
|
$metadata = $this->em->getClassMetaData('App:User');
|
|
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
|
|
$metadata->setIdGenerator(new AssignedGenerator());
|
|
|
|
|
|
|
|
// Job Récupératoin la table de référence des articles
|
|
// Toute les 1mn
|
|
$entity = $this->em->getRepository('App:User')->findOneBy(["username"=>"admin"]);
|
|
if(!$entity) {
|
|
$this->writelnred('Création du compte admin par défaut avec password admin - Veuillez modifier votre password admin après connexion');
|
|
$entity = new User;
|
|
$entity->setId(0);
|
|
$entity->setUsername("admin");
|
|
$entity->setPassword("admin");
|
|
$entity->setFirstname($this->appname);
|
|
$entity->setLastname("Admin");
|
|
$entity->setEmail($this->appmailnoreply);
|
|
$entity->setRoles(["ROLE_ADMIN"]);
|
|
$entity->setAvatar("admin.jpg");
|
|
$this->em->persist($entity);
|
|
}
|
|
|
|
// On flush
|
|
$this->em->flush();
|
|
|
|
}
|
|
|
|
private function writelnred($string) {
|
|
$this->output->writeln('<fg=red>'.$string.'</>');
|
|
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
|
|
}
|
|
private function writeln($string) {
|
|
$this->output->writeln($string);
|
|
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
|
|
}
|
|
}
|