mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-13 03:09:51 +00:00
Simplify loading of Symfony commands.
This commit is contained in:
parent
ca9ae3874c
commit
5ddc0e7a53
@ -1,25 +1,7 @@
|
|||||||
<?php
|
<?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;
|
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\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
@ -28,39 +10,28 @@ use Doctrine\ORM\EntityManagerInterface;
|
|||||||
use Chill\PersonBundle\Entity\Person;
|
use Chill\PersonBundle\Entity\Person;
|
||||||
use Symfony\Component\Console\Exception\RuntimeException;
|
use Symfony\Component\Console\Exception\RuntimeException;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
|
||||||
class ChillPersonMoveCommand extends ContainerAwareCommand
|
final class ChillPersonMoveCommand extends Command
|
||||||
{
|
{
|
||||||
/**
|
private PersonMove $mover;
|
||||||
*
|
|
||||||
* @var PersonMove
|
private EntityManagerInterface $em;
|
||||||
*/
|
|
||||||
protected $mover;
|
private LoggerInterface $chillLogger;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @var EntityManagerInterface
|
|
||||||
*/
|
|
||||||
protected $em;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @var LoggerInterface
|
|
||||||
*/
|
|
||||||
protected $chillLogger;
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
PersonMove $mover,
|
PersonMove $mover,
|
||||||
EntityManagerInterface $em,
|
EntityManagerInterface $em,
|
||||||
LoggerInterface $chillLogger
|
LoggerInterface $chillLogger
|
||||||
) {
|
) {
|
||||||
parent::__construct('chill:person:move');
|
parent::__construct('chill:person:move');
|
||||||
|
|
||||||
$this->mover = $mover;
|
$this->mover = $mover;
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->chillLogger = $chillLogger;
|
$this->chillLogger = $chillLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure()
|
protected function configure()
|
||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
@ -73,14 +44,14 @@ class ChillPersonMoveCommand extends ContainerAwareCommand
|
|||||||
->addOption('delete-entity', null, InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, "entity to delete", [])
|
->addOption('delete-entity', null, InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, "entity to delete", [])
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function interact(InputInterface $input, OutputInterface $output)
|
protected function interact(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
if (FALSE === $input->hasOption('dump-sql') && FALSE === $input->hasOption('force')) {
|
if (FALSE === $input->hasOption('dump-sql') && FALSE === $input->hasOption('force')) {
|
||||||
$msg = "You must use \"--dump-sql\" or \"--force\"";
|
$msg = "You must use \"--dump-sql\" or \"--force\"";
|
||||||
throw new RuntimeException($msg);
|
throw new RuntimeException($msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (["from", "to"] as $name) {
|
foreach (["from", "to"] as $name) {
|
||||||
if (empty($input->getOption($name))) {
|
if (empty($input->getOption($name))) {
|
||||||
throw new RuntimeException("You must set a \"$name\" option");
|
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 "
|
throw new RuntimeException("The id in \"$name\" field does not contains "
|
||||||
. "only digits: $id");
|
. "only digits: $id");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
@ -99,16 +70,16 @@ class ChillPersonMoveCommand extends ContainerAwareCommand
|
|||||||
$from = $repository->find($input->getOption('from'));
|
$from = $repository->find($input->getOption('from'));
|
||||||
$to = $repository->find($input->getOption('to'));
|
$to = $repository->find($input->getOption('to'));
|
||||||
$deleteEntities = $input->getOption('delete-entity');
|
$deleteEntities = $input->getOption('delete-entity');
|
||||||
|
|
||||||
if ($from === NULL) {
|
if ($from === NULL) {
|
||||||
throw new RuntimeException(sprintf("Person \"from\" with id %d not found", $input->getOption('from')));
|
throw new RuntimeException(sprintf("Person \"from\" with id %d not found", $input->getOption('from')));
|
||||||
}
|
}
|
||||||
if ($to === NULL) {
|
if ($to === NULL) {
|
||||||
throw new RuntimeException(sprintf("Person \"to\" with id %d not found", $input->getOption('to')));
|
throw new RuntimeException(sprintf("Person \"to\" with id %d not found", $input->getOption('to')));
|
||||||
}
|
}
|
||||||
|
|
||||||
$sqls = $this->mover->getSQL($from, $to, $deleteEntities);
|
$sqls = $this->mover->getSQL($from, $to, $deleteEntities);
|
||||||
|
|
||||||
if ($input->getOption('dump-sql')) {
|
if ($input->getOption('dump-sql')) {
|
||||||
foreach($sqls as $sql) {
|
foreach($sqls as $sql) {
|
||||||
$output->writeln($sql);
|
$output->writeln($sql);
|
||||||
@ -125,25 +96,25 @@ class ChillPersonMoveCommand extends ContainerAwareCommand
|
|||||||
$connection->executeQuery($sql);
|
$connection->executeQuery($sql);
|
||||||
}
|
}
|
||||||
$connection->commit();
|
$connection->commit();
|
||||||
|
|
||||||
$this->chillLogger->notice("Move a person from command line succeeded", $ctxt);
|
$this->chillLogger->notice("Move a person from command line succeeded", $ctxt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function buildLoggingContext(Person $from, Person $to, $deleteEntities, $sqls)
|
protected function buildLoggingContext(Person $from, Person $to, $deleteEntities, $sqls)
|
||||||
{
|
{
|
||||||
$ctxt = [
|
$ctxt = [
|
||||||
'from' => $from->getId(),
|
'from' => $from->getId(),
|
||||||
'to' => $to->getId()
|
'to' => $to->getId()
|
||||||
];
|
];
|
||||||
|
|
||||||
foreach ($deleteEntities as $key => $de) {
|
foreach ($deleteEntities as $key => $de) {
|
||||||
$ctxt['delete_entity_'.$key] = $de;
|
$ctxt['delete_entity_'.$key] = $de;
|
||||||
}
|
}
|
||||||
foreach ($sqls as $key => $sql) {
|
foreach ($sqls as $key => $sql) {
|
||||||
$ctxt['sql_'.$key] = $sql;
|
$ctxt['sql_'.$key] = $sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $ctxt;
|
return $ctxt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -69,7 +69,6 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
|
|||||||
$loader->load('services/search.yaml');
|
$loader->load('services/search.yaml');
|
||||||
$loader->load('services/menu.yaml');
|
$loader->load('services/menu.yaml');
|
||||||
$loader->load('services/privacyEvent.yaml');
|
$loader->load('services/privacyEvent.yaml');
|
||||||
$loader->load('services/command.yaml');
|
|
||||||
$loader->load('services/actions.yaml');
|
$loader->load('services/actions.yaml');
|
||||||
$loader->load('services/form.yaml');
|
$loader->load('services/form.yaml');
|
||||||
$loader->load('services/alt_names.yaml');
|
$loader->load('services/alt_names.yaml');
|
||||||
|
@ -12,6 +12,13 @@ services:
|
|||||||
tags:
|
tags:
|
||||||
- { name: 'serializer.normalizer', priority: 64 }
|
- { name: 'serializer.normalizer', priority: 64 }
|
||||||
|
|
||||||
|
Chill\PersonBundle\Command\:
|
||||||
|
resource: '../Command/'
|
||||||
|
autowire: true
|
||||||
|
autoconfigure: true
|
||||||
|
tags:
|
||||||
|
- { name: console.command }
|
||||||
|
|
||||||
chill.person.form.type.select2maritalstatus:
|
chill.person.form.type.select2maritalstatus:
|
||||||
class: Chill\PersonBundle\Form\Type\Select2MaritalStatusType
|
class: Chill\PersonBundle\Form\Type\Select2MaritalStatusType
|
||||||
arguments:
|
arguments:
|
||||||
|
@ -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 }
|
|
Loading…
x
Reference in New Issue
Block a user