Simplify loading of Symfony commands.

This commit is contained in:
Pol Dellaiera 2021-06-22 15:31:49 +02:00
parent ca9ae3874c
commit 5ddc0e7a53
5 changed files with 246 additions and 336 deletions

View File

@ -1,25 +1,7 @@
<?php
/*
* Copyright (C) 2016-2019 Champs-Libres <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\PersonBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
@ -28,39 +10,28 @@ use Doctrine\ORM\EntityManagerInterface;
use Chill\PersonBundle\Entity\Person;
use Symfony\Component\Console\Exception\RuntimeException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
class ChillPersonMoveCommand extends ContainerAwareCommand
final class ChillPersonMoveCommand extends Command
{
/**
*
* @var PersonMove
*/
protected $mover;
/**
*
* @var EntityManagerInterface
*/
protected $em;
/**
*
* @var LoggerInterface
*/
protected $chillLogger;
private PersonMove $mover;
private EntityManagerInterface $em;
private LoggerInterface $chillLogger;
public function __construct(
PersonMove $mover,
PersonMove $mover,
EntityManagerInterface $em,
LoggerInterface $chillLogger
) {
parent::__construct('chill:person:move');
$this->mover = $mover;
$this->em = $em;
$this->chillLogger = $chillLogger;
}
protected function configure()
{
$this
@ -73,14 +44,14 @@ class ChillPersonMoveCommand extends ContainerAwareCommand
->addOption('delete-entity', null, InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, "entity to delete", [])
;
}
protected function interact(InputInterface $input, OutputInterface $output)
{
if (FALSE === $input->hasOption('dump-sql') && FALSE === $input->hasOption('force')) {
$msg = "You must use \"--dump-sql\" or \"--force\"";
throw new RuntimeException($msg);
}
foreach (["from", "to"] as $name) {
if (empty($input->getOption($name))) {
throw new RuntimeException("You must set a \"$name\" option");
@ -90,7 +61,7 @@ class ChillPersonMoveCommand extends ContainerAwareCommand
throw new RuntimeException("The id in \"$name\" field does not contains "
. "only digits: $id");
}
}
}
}
protected function execute(InputInterface $input, OutputInterface $output)
@ -99,16 +70,16 @@ class ChillPersonMoveCommand extends ContainerAwareCommand
$from = $repository->find($input->getOption('from'));
$to = $repository->find($input->getOption('to'));
$deleteEntities = $input->getOption('delete-entity');
if ($from === NULL) {
throw new RuntimeException(sprintf("Person \"from\" with id %d not found", $input->getOption('from')));
}
if ($to === NULL) {
throw new RuntimeException(sprintf("Person \"to\" with id %d not found", $input->getOption('to')));
}
$sqls = $this->mover->getSQL($from, $to, $deleteEntities);
if ($input->getOption('dump-sql')) {
foreach($sqls as $sql) {
$output->writeln($sql);
@ -125,25 +96,25 @@ class ChillPersonMoveCommand extends ContainerAwareCommand
$connection->executeQuery($sql);
}
$connection->commit();
$this->chillLogger->notice("Move a person from command line succeeded", $ctxt);
}
}
protected function buildLoggingContext(Person $from, Person $to, $deleteEntities, $sqls)
{
$ctxt = [
'from' => $from->getId(),
'to' => $to->getId()
];
foreach ($deleteEntities as $key => $de) {
$ctxt['delete_entity_'.$key] = $de;
}
foreach ($sqls as $key => $sql) {
$ctxt['sql_'.$key] = $sql;
}
return $ctxt;
}

View File

@ -69,7 +69,6 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
$loader->load('services/search.yaml');
$loader->load('services/menu.yaml');
$loader->load('services/privacyEvent.yaml');
$loader->load('services/command.yaml');
$loader->load('services/actions.yaml');
$loader->load('services/form.yaml');
$loader->load('services/alt_names.yaml');

View File

@ -12,6 +12,13 @@ services:
tags:
- { name: 'serializer.normalizer', priority: 64 }
Chill\PersonBundle\Command\:
resource: '../Command/'
autowire: true
autoconfigure: true
tags:
- { name: console.command }
chill.person.form.type.select2maritalstatus:
class: Chill\PersonBundle\Form\Type\Select2MaritalStatusType
arguments:

View File

@ -1,19 +0,0 @@
services:
Chill\PersonBundle\Command\ChillPersonMoveCommand:
arguments:
$em: '@Doctrine\ORM\EntityManagerInterface'
$mover: '@Chill\PersonBundle\Actions\Remove\PersonMove'
$chillLogger: '@chill.main.logger'
tags:
- { name: console.command }
Chill\PersonBundle\Command\ImportPeopleFromCSVCommand:
arguments:
$logger: '@logger'
$helper: '@Chill\MainBundle\Templating\TranslatableStringHelper'
$em: '@Doctrine\ORM\EntityManagerInterface'
$customFieldProvider: '@Chill\CustomFieldsBundle\Service\CustomFieldProvider'
$eventDispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface'
$formFactory: '@form.factory'
tags:
- { name: console.command }