mirror of
https://github.com/wallabag/wallabag.git
synced 2025-10-05 19: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:
parent
9c16dd7bd1
commit
5832482a10
14 changed files with 290 additions and 169 deletions
|
@ -5,11 +5,10 @@ namespace Wallabag\CoreBundle\Command;
|
|||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\Exception\DriverException;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use FOS\UserBundle\Event\UserEvent;
|
||||
use FOS\UserBundle\FOSUserEvents;
|
||||
use FOS\UserBundle\Model\UserManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
@ -22,28 +21,37 @@ use Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule;
|
|||
use Wallabag\CoreBundle\Entity\InternalSetting;
|
||||
use Wallabag\UserBundle\Entity\User;
|
||||
|
||||
class InstallCommand extends ContainerAwareCommand
|
||||
class InstallCommand extends Command
|
||||
{
|
||||
/**
|
||||
* @var InputInterface
|
||||
*/
|
||||
private $defaultInput;
|
||||
|
||||
/**
|
||||
* @var SymfonyStyle
|
||||
*/
|
||||
private $io;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $functionExists = [
|
||||
private InputInterface $defaultInput;
|
||||
private SymfonyStyle $io;
|
||||
private array $functionExists = [
|
||||
'curl_exec',
|
||||
'curl_multi_init',
|
||||
];
|
||||
|
||||
private bool $runOtherCommands = true;
|
||||
|
||||
private EntityManagerInterface $entityManager;
|
||||
private EventDispatcherInterface $dispatcher;
|
||||
private UserManagerInterface $userManager;
|
||||
private string $databaseDriver;
|
||||
private string $databaseName;
|
||||
private array $defaultSettings;
|
||||
private array $defaultIgnoreOriginInstanceRules;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager, EventDispatcherInterface $dispatcher, UserManagerInterface $userManager, string $databaseDriver, string $databaseName, array $defaultSettings, array $defaultIgnoreOriginInstanceRules)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->userManager = $userManager;
|
||||
$this->databaseDriver = $databaseDriver;
|
||||
$this->databaseName = $databaseName;
|
||||
$this->defaultSettings = $defaultSettings;
|
||||
$this->defaultIgnoreOriginInstanceRules = $defaultIgnoreOriginInstanceRules;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function disableRunOtherCommands(): void
|
||||
{
|
||||
$this->runOtherCommands = false;
|
||||
|
@ -88,8 +96,6 @@ class InstallCommand extends ContainerAwareCommand
|
|||
{
|
||||
$this->io->section('Step 1 of 4: Checking system requirements.');
|
||||
|
||||
$doctrineManager = $this->getContainer()->get(ManagerRegistry::class)->getManager();
|
||||
|
||||
$rows = [];
|
||||
|
||||
// testing if database driver exists
|
||||
|
@ -98,26 +104,26 @@ class InstallCommand extends ContainerAwareCommand
|
|||
$status = '<info>OK!</info>';
|
||||
$help = '';
|
||||
|
||||
if (!\extension_loaded($this->getContainer()->getParameter('database_driver'))) {
|
||||
if (!\extension_loaded($this->databaseDriver)) {
|
||||
$fulfilled = false;
|
||||
$status = '<error>ERROR!</error>';
|
||||
$help = 'Database driver "' . $this->getContainer()->getParameter('database_driver') . '" is not installed.';
|
||||
$help = 'Database driver "' . $this->databaseDriver . '" is not installed.';
|
||||
}
|
||||
|
||||
$rows[] = [sprintf($label, $this->getContainer()->getParameter('database_driver')), $status, $help];
|
||||
$rows[] = [sprintf($label, $this->databaseDriver), $status, $help];
|
||||
|
||||
// testing if connection to the database can be etablished
|
||||
$label = '<comment>Database connection</comment>';
|
||||
$status = '<info>OK!</info>';
|
||||
$help = '';
|
||||
|
||||
$conn = $this->getContainer()->get(ManagerRegistry::class)->getManager()->getConnection();
|
||||
$conn = $this->entityManager->getConnection();
|
||||
|
||||
try {
|
||||
$conn->connect();
|
||||
} catch (\Exception $e) {
|
||||
if (false === strpos($e->getMessage(), 'Unknown database')
|
||||
&& false === strpos($e->getMessage(), 'database "' . $this->getContainer()->getParameter('database_name') . '" does not exist')) {
|
||||
&& false === strpos($e->getMessage(), 'database "' . $this->databaseName . '" does not exist')) {
|
||||
$fulfilled = false;
|
||||
$status = '<error>ERROR!</error>';
|
||||
$help = 'Can\'t connect to the database: ' . $e->getMessage();
|
||||
|
@ -146,7 +152,7 @@ class InstallCommand extends ContainerAwareCommand
|
|||
// testing if PostgreSQL > 9.1
|
||||
if ($conn->isConnected() && 'postgresql' === $conn->getDatabasePlatform()->getName()) {
|
||||
// return version should be like "PostgreSQL 9.5.4 on x86_64-apple-darwin15.6.0, compiled by Apple LLVM version 8.0.0 (clang-800.0.38), 64-bit"
|
||||
$version = $doctrineManager->getConnection()->query('SELECT version();')->fetchColumn();
|
||||
$version = $conn->query('SELECT version();')->fetchColumn();
|
||||
|
||||
preg_match('/PostgreSQL ([0-9\.]+)/i', $version, $matches);
|
||||
|
||||
|
@ -260,10 +266,7 @@ class InstallCommand extends ContainerAwareCommand
|
|||
return $this;
|
||||
}
|
||||
|
||||
$em = $this->getContainer()->get(EntityManagerInterface::class);
|
||||
|
||||
$userManager = $this->getContainer()->get(UserManagerInterface::class);
|
||||
$user = $userManager->createUser();
|
||||
$user = $this->userManager->createUser();
|
||||
\assert($user instanceof User);
|
||||
|
||||
$user->setUsername($this->io->ask('Username', 'wallabag'));
|
||||
|
@ -277,11 +280,10 @@ class InstallCommand extends ContainerAwareCommand
|
|||
$user->setEnabled(true);
|
||||
$user->addRole('ROLE_SUPER_ADMIN');
|
||||
|
||||
$em->persist($user);
|
||||
$this->entityManager->persist($user);
|
||||
|
||||
// dispatch a created event so the associated config will be created
|
||||
$event = new UserEvent($user);
|
||||
$this->getContainer()->get(EventDispatcherInterface::class)->dispatch($event, FOSUserEvents::USER_CREATED);
|
||||
$this->dispatcher->dispatch(new UserEvent($user), FOSUserEvents::USER_CREATED);
|
||||
|
||||
$this->io->text('<info>Administration successfully setup.</info>');
|
||||
|
||||
|
@ -291,27 +293,28 @@ class InstallCommand extends ContainerAwareCommand
|
|||
private function setupConfig()
|
||||
{
|
||||
$this->io->section('Step 4 of 4: Config setup.');
|
||||
$em = $this->getContainer()->get(EntityManagerInterface::class);
|
||||
|
||||
// cleanup before insert new stuff
|
||||
$em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\InternalSetting')->execute();
|
||||
$em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule')->execute();
|
||||
$this->entityManager->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\InternalSetting')->execute();
|
||||
$this->entityManager->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule')->execute();
|
||||
|
||||
foreach ($this->getContainer()->getParameter('wallabag_core.default_internal_settings') as $setting) {
|
||||
foreach ($this->defaultSettings as $setting) {
|
||||
$newSetting = new InternalSetting();
|
||||
$newSetting->setName($setting['name']);
|
||||
$newSetting->setValue($setting['value']);
|
||||
$newSetting->setSection($setting['section']);
|
||||
$em->persist($newSetting);
|
||||
|
||||
$this->entityManager->persist($newSetting);
|
||||
}
|
||||
|
||||
foreach ($this->getContainer()->getParameter('wallabag_core.default_ignore_origin_instance_rules') as $ignore_origin_instance_rule) {
|
||||
foreach ($this->defaultIgnoreOriginInstanceRules as $ignore_origin_instance_rule) {
|
||||
$newIgnoreOriginInstanceRule = new IgnoreOriginInstanceRule();
|
||||
$newIgnoreOriginInstanceRule->setRule($ignore_origin_instance_rule['rule']);
|
||||
$em->persist($newIgnoreOriginInstanceRule);
|
||||
|
||||
$this->entityManager->persist($newIgnoreOriginInstanceRule);
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->io->text('<info>Config successfully setup.</info>');
|
||||
|
||||
|
@ -350,7 +353,7 @@ class InstallCommand extends ContainerAwareCommand
|
|||
|
||||
// PDO does not always close the connection after Doctrine commands.
|
||||
// See https://github.com/symfony/symfony/issues/11750.
|
||||
$this->getContainer()->get(ManagerRegistry::class)->getManager()->getConnection()->close();
|
||||
$this->entityManager->getConnection()->close();
|
||||
|
||||
if (0 !== $exitCode) {
|
||||
$this->getApplication()->setAutoExit(true);
|
||||
|
@ -368,7 +371,7 @@ class InstallCommand extends ContainerAwareCommand
|
|||
*/
|
||||
private function isDatabasePresent()
|
||||
{
|
||||
$connection = $this->getContainer()->get(ManagerRegistry::class)->getManager()->getConnection();
|
||||
$connection = $this->entityManager->getConnection();
|
||||
$databaseName = $connection->getDatabase();
|
||||
|
||||
try {
|
||||
|
@ -389,7 +392,7 @@ class InstallCommand extends ContainerAwareCommand
|
|||
|
||||
// custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
|
||||
if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
|
||||
$params = $this->getContainer()->get(Connection::class)->getParams();
|
||||
$params = $connection->getParams();
|
||||
|
||||
if (isset($params['path']) && file_exists($params['path'])) {
|
||||
return true;
|
||||
|
@ -415,7 +418,7 @@ class InstallCommand extends ContainerAwareCommand
|
|||
*/
|
||||
private function isSchemaPresent()
|
||||
{
|
||||
$schemaManager = $this->getContainer()->get(ManagerRegistry::class)->getManager()->getConnection()->getSchemaManager();
|
||||
$schemaManager = $this->entityManager->getConnection()->getSchemaManager();
|
||||
|
||||
return \count($schemaManager->listTableNames()) > 0 ? true : false;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue