1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-08-31 18:31:02 +00:00

Remove ContainerAwareCommand from commands

And use DI to retrieve services in commands (except for `RedisWorkerCommand` where the container is injected, hard to find a better way, at least for now).
This commit is contained in:
Jeremy Benoist 2022-12-15 20:57:02 +01:00
parent 9c16dd7bd1
commit 5832482a10
No known key found for this signature in database
GPG key ID: 7168D5DD29F38552
14 changed files with 290 additions and 169 deletions

View file

@ -2,9 +2,9 @@
namespace Wallabag\ImportBundle\Command;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@ -20,9 +20,39 @@ use Wallabag\ImportBundle\Import\ReadabilityImport;
use Wallabag\ImportBundle\Import\WallabagV1Import;
use Wallabag\ImportBundle\Import\WallabagV2Import;
use Wallabag\UserBundle\Entity\User;
use Wallabag\UserBundle\Repository\UserRepository;
class ImportCommand extends ContainerAwareCommand
class ImportCommand extends Command
{
private EntityManagerInterface $entityManager;
private TokenStorageInterface $tokenStorage;
private UserRepository $userRepository;
private WallabagV2Import $wallabagV2Import;
private FirefoxImport $firefoxImport;
private ChromeImport $chromeImport;
private ReadabilityImport $readabilityImport;
private InstapaperImport $instapaperImport;
private PinboardImport $pinboardImport;
private DeliciousImport $deliciousImport;
private WallabagV1Import $wallabagV1Import;
public function __construct(EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage, UserRepository $userRepository, WallabagV2Import $wallabagV2Import, FirefoxImport $firefoxImport, ChromeImport $chromeImport, ReadabilityImport $readabilityImport, InstapaperImport $instapaperImport, PinboardImport $pinboardImport, DeliciousImport $deliciousImport, WallabagV1Import $wallabagV1Import)
{
$this->entityManager = $entityManager;
$this->tokenStorage = $tokenStorage;
$this->userRepository = $userRepository;
$this->wallabagV2Import = $wallabagV2Import;
$this->firefoxImport = $firefoxImport;
$this->chromeImport = $chromeImport;
$this->readabilityImport = $readabilityImport;
$this->instapaperImport = $instapaperImport;
$this->pinboardImport = $pinboardImport;
$this->deliciousImport = $deliciousImport;
$this->wallabagV1Import = $wallabagV1Import;
parent::__construct();
}
protected function configure()
{
$this
@ -45,14 +75,13 @@ class ImportCommand extends ContainerAwareCommand
throw new Exception(sprintf('File "%s" not found', $input->getArgument('filepath')));
}
$em = $this->getContainer()->get(ManagerRegistry::class)->getManager();
// Turning off doctrine default logs queries for saving memory
$em->getConnection()->getConfiguration()->setSQLLogger(null);
$this->entityManager->getConnection()->getConfiguration()->setSQLLogger(null);
if ($input->getOption('useUserId')) {
$entityUser = $em->getRepository(User::class)->findOneById($input->getArgument('username'));
$entityUser = $this->userRepository->findOneById($input->getArgument('username'));
} else {
$entityUser = $em->getRepository(User::class)->findOneByUsername($input->getArgument('username'));
$entityUser = $this->userRepository->findOneByUsername($input->getArgument('username'));
}
if (!\is_object($entityUser)) {
@ -66,33 +95,33 @@ class ImportCommand extends ContainerAwareCommand
'main',
$entityUser->getRoles());
$this->getContainer()->get(TokenStorageInterface::class)->setToken($token);
$user = $this->getContainer()->get(TokenStorageInterface::class)->getToken()->getUser();
$this->tokenStorage->setToken($token);
$user = $this->tokenStorage->getToken()->getUser();
switch ($input->getOption('importer')) {
case 'v2':
$import = $this->getContainer()->get(WallabagV2Import::class);
$import = $this->wallabagV2Import;
break;
case 'firefox':
$import = $this->getContainer()->get(FirefoxImport::class);
$import = $this->firefoxImport;
break;
case 'chrome':
$import = $this->getContainer()->get(ChromeImport::class);
$import = $this->chromeImport;
break;
case 'readability':
$import = $this->getContainer()->get(ReadabilityImport::class);
$import = $this->readabilityImport;
break;
case 'instapaper':
$import = $this->getContainer()->get(InstapaperImport::class);
$import = $this->instapaperImport;
break;
case 'pinboard':
$import = $this->getContainer()->get(PinboardImport::class);
$import = $this->pinboardImport;
break;
case 'delicious':
$import = $this->getContainer()->get(DeliciousImport::class);
$import = $this->deliciousImport;
break;
default:
$import = $this->getContainer()->get(WallabagV1Import::class);
$import = $this->wallabagV1Import;
}
$import->setMarkAsRead($input->getOption('markAsRead'));
@ -109,7 +138,7 @@ class ImportCommand extends ContainerAwareCommand
$output->writeln('<comment>' . $summary['skipped'] . ' already saved</comment>');
}
$em->clear();
$this->entityManager->clear();
$output->writeln('End : ' . (new \DateTime())->format('d-m-Y G:i:s') . ' ---');