first commit symfony 6
This commit is contained in:
102
src/Command/CleanCommand.php
Normal file
102
src/Command/CleanCommand.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?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\Finder\Finder;
|
||||
use App\Entity\Tallyday as Tallyday;
|
||||
|
||||
|
||||
class CleanCommand extends Command
|
||||
{
|
||||
private $container;
|
||||
private $em;
|
||||
private $output;
|
||||
private $filesystem;
|
||||
private $rootlog;
|
||||
|
||||
public function __construct(ContainerInterface $container,EntityManagerInterface $em)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->container = $container;
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('app:Clean')
|
||||
->setDescription('Nettoyage des données obsolètes')
|
||||
->setHelp('Nettoyage des données obsolètes')
|
||||
->addArgument('cronid', InputArgument::OPTIONAL, 'ID Cron Job')
|
||||
->addArgument('lastchance', InputArgument::OPTIONAL, 'Lastchance to run the cron')
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$this->output = $output;
|
||||
$this->filesystem = new Filesystem();
|
||||
$this->rootlog = $this->container->get('kernel')->getLogDir()."/";
|
||||
$alias = $this->container->getParameter('appAlias');
|
||||
|
||||
$this->writelnred('');
|
||||
$this->writelnred('== app:Clean');
|
||||
$this->writelnred('==========================================================================================================');
|
||||
|
||||
$now=new \DateTime('now');
|
||||
|
||||
$directory=$this->container->get('kernel')->getProjectDir()."/public/uploads/avatar";
|
||||
$this->writeln($directory);
|
||||
|
||||
$files=[];
|
||||
$fs = new Filesystem();
|
||||
|
||||
if($fs->exists($directory)) {
|
||||
$finder = new Finder();
|
||||
$finder->in($directory)->files();
|
||||
|
||||
foreach (iterator_to_array($finder) as $file) {
|
||||
$name = $file->getRelativePathname();
|
||||
if($name!="admin.jpg"&&$name!="noavatar.png"&&$name!="system.jpg") {
|
||||
$entity=$this->em->getRepository("App\Entity\User")->findBy(["avatar"=>$name]);
|
||||
if(!$entity) {
|
||||
$this->writeln($name);
|
||||
$url=$directory."/".$name;
|
||||
if($fs->exists($url)) {
|
||||
$fs->remove($url);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$fs = new Filesystem();
|
||||
$users=$this->em->getRepository("App\Entity\User")->findAll();
|
||||
foreach($users as $user) {
|
||||
if(!$fs->exists($directory."/".$user->getAvatar())) {
|
||||
$this->writeln($user->getUsername());
|
||||
$user->setAvatar("noavatar.png");
|
||||
$this->em->persist($user);
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
|
||||
$this->writeln('');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user