WIP: Symfony 6 project remake #2

Draft
skobkin wants to merge 103 commits from symfony6_remake into master
Showing only changes of commit 4cb8e715bf - Show all commits

View file

@ -1,12 +1,17 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\Command; namespace App\Command;
use Doctrine\ORM\{EntityManager, EntityManagerInterface}; use App\Entity\User;
use src\PointToolsBundle\Entity\User; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\{InputInterface, InputArgument, InputOption}; 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\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
/** /**
@ -18,23 +23,17 @@ use Symfony\Component\Filesystem\Filesystem;
* WHERE u.id <> (-1) * WHERE u.id <> (-1)
* ) TO '/tmp/point_users.csv' WITH HEADER DELIMITER '|' CSV; * ) TO '/tmp/point_users.csv' WITH HEADER DELIMITER '|' CSV;
*/ */
#[AsCommand(name: 'app:import:users', description: 'Import users from CSV file')]
class ImportUsersCommand extends Command class ImportUsersCommand extends Command
{ {
/** @var EntityManager */ public function __construct(private readonly EntityManagerInterface $em)
private $em;
public function __construct(EntityManagerInterface $em)
{ {
$this->em = $em;
parent::__construct(); parent::__construct();
} }
protected function configure() protected function configure(): void
{ {
$this $this
->setName('point:import:users')
->setDescription('Import users from CSV file')
->addArgument( ->addArgument(
'file', 'file',
InputArgument::REQUIRED, InputArgument::REQUIRED,
@ -55,20 +54,24 @@ class ImportUsersCommand extends Command
; ;
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$io = new SymfonyStyle($input, $output);
$fs = new Filesystem(); $fs = new Filesystem();
$fileName = $input->getArgument('file'); $fileName = $input->getArgument('file');
if (!($fs->exists($fileName) && is_readable($fileName))) { if (!($fs->exists($fileName) && \is_readable($fileName))) {
$output->writeln('File does not exists or not readable.'); $io->error('File does not exists or not readable.');
return 1;
return Command::FAILURE;
} }
if (false === ($file = fopen($fileName, 'r'))) { if (false === ($file = fopen($fileName, 'rb'))) {
$output->writeln('fopen() error'); $io->error('fopen() error');
return 1;
return Command::FAILURE;
} }
if (!$input->getOption('no-skip-first')) { if (!$input->getOption('no-skip-first')) {
@ -79,7 +82,7 @@ class ImportUsersCommand extends Command
$count = 0; $count = 0;
while (false !== ($row = fgetcsv($file, 1000, '|'))) { while (false !== ($row = fgetcsv($file, 1000, '|'))) {
if (count($row) !== 4) { if (\count($row) !== 4) {
continue; continue;
} }
@ -93,15 +96,15 @@ class ImportUsersCommand extends Command
$this->em->detach($user); $this->em->detach($user);
} }
if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) { if (OutputInterface::VERBOSITY_VERBOSE === $io->getVerbosity()) {
$output->writeln('@' . $row[1] . ' added'); $io->info('@' . $row[1] . ' added');
} }
$count++; $count++;
} }
$output->writeln($count . ' users imported.'); $io->success($count . ' users imported.');
return 0; return Command::SUCCESS;
} }
} }