resolving deprecated private service injection on commands classes

This commit is contained in:
Mathieu Jaumotte 2021-01-26 13:47:35 +01:00
parent f162ed3911
commit b45191bd04
8 changed files with 168 additions and 34 deletions

View File

@ -2,7 +2,7 @@
namespace Chill\MainBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@ -21,7 +21,7 @@ use Chill\MainBundle\Entity\PermissionsGroup;
use Symfony\Component\Console\Question\ChoiceQuestion;
use League\Csv\Writer;
class ChillImportUsersCommand extends ContainerAwareCommand
class ChillImportUsersCommand extends Command
{
/**

View File

@ -2,7 +2,7 @@
namespace Chill\MainBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@ -16,7 +16,12 @@ use Chill\MainBundle\Security\PasswordRecover\RecoverPasswordHelper;
use Chill\MainBundle\Security\PasswordRecover\PasswordRecoverEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ChillUserSendRenewPasswordCodeCommand extends ContainerAwareCommand
/**
* Class ChillUserSendRenewPasswordCodeCommand
*
* @package Chill\MainBundle\Command
*/
class ChillUserSendRenewPasswordCodeCommand extends Command
{
/**
*
@ -66,8 +71,8 @@ class ChillUserSendRenewPasswordCodeCommand extends ContainerAwareCommand
LoggerInterface $logger,
EntityManagerInterface $em,
RecoverPasswordHelper $recoverPasswordHelper,
EventDispatcherInterface $eventDispatcher)
{
EventDispatcherInterface $eventDispatcher
) {
$this->logger = $logger;
$this->em = $em;
$this->recoverPasswordHelper = $recoverPasswordHelper;

View File

@ -19,7 +19,8 @@
namespace Chill\MainBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@ -28,8 +29,16 @@ use Symfony\Component\Console\Output\OutputInterface;
/*
* Load or update the languages entities command
*/
class LoadAndUpdateLanguagesCommand extends ContainerAwareCommand
class LoadAndUpdateLanguagesCommand extends Command
{
/**
* @var EntityManager
*/
private $entityManager;
private $availableLanguages;
// The regional version of language are language with _ in the code
// This array contains regional code to not exclude
private $regionalVersionToInclude = ["ro_MD"];
@ -40,7 +49,21 @@ class LoadAndUpdateLanguagesCommand extends ContainerAwareCommand
const INCLUDE_REGIONAL_VERSION = 'include_regional';
const INCLUDE_ANCIENT = 'include_ancient';
/**
* LoadCountriesCommand constructor.
*
* @param EntityManager $entityManager
* @param $availableLanguages
*/
public function __construct(EntityManager $entityManager, $availableLanguages)
{
$this->entityManager=$entityManager;
$this->availableLanguages=$availableLanguages;
parent::__construct();
}
/*
* (non-PHPdoc)
* @see \Symfony\Component\Console\Command\Command::configure()
@ -72,8 +95,8 @@ class LoadAndUpdateLanguagesCommand extends ContainerAwareCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$chillAvailableLanguages = $this->getContainer()->getParameter('chill_main.available_languages');
$em = $this->entityManager;
$chillAvailableLanguages = $this->availableLanguages;
$languageBundle = Intl::getLanguageBundle();
$languages = array();

View File

@ -1,7 +1,8 @@
<?php
namespace Chill\MainBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@ -11,9 +12,29 @@ use Symfony\Component\Console\Output\OutputInterface;
* @author Julien Fastré <julien.fastre@champs-libres.coop
*
*/
class LoadCountriesCommand extends ContainerAwareCommand
class LoadCountriesCommand extends Command
{
/**
* @var EntityManager
*/
private $entityManager;
private $availableLanguages;
/**
* LoadCountriesCommand constructor.
*
* @param EntityManager $entityManager
* @param $availableLanguages
*/
public function __construct(EntityManager $entityManager, $availableLanguages)
{
$this->entityManager=$entityManager;
$this->availableLanguages=$availableLanguages;
parent::__construct();
}
/*
* (non-PHPdoc)
* @see \Symfony\Component\Console\Command\Command::configure()
@ -31,8 +52,8 @@ class LoadCountriesCommand extends ContainerAwareCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$countries = static::prepareCountryList($this->getContainer()->getParameter('chill_main.available_languages'));
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$countries = static::prepareCountryList($this->availableLanguages);
$em = $this->entityManager;
foreach($countries as $country) {
$countryStored = $em->getRepository('ChillMainBundle:Country')

View File

@ -19,21 +19,48 @@
namespace Chill\MainBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Filesystem\Filesystem;
use Chill\MainBundle\Entity\PostalCode;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
*
* Class LoadPostalCodesCommand
*
* @package Chill\MainBundle\Command
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class LoadPostalCodesCommand extends ContainerAwareCommand
class LoadPostalCodesCommand extends Command
{
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var ValidatorInterface
*/
private $validator;
/**
* LoadPostalCodesCommand constructor.
*
* @param EntityManager $entityManager
* @param ValidatorInterface $validator
*/
public function __construct(EntityManager $entityManager, ValidatorInterface $validator)
{
$this->entityManager = $entityManager;
$this->validator = $validator;
parent::__construct();
}
protected function configure()
{
$this->setName('chill:main:postal-code:populate')
@ -105,7 +132,7 @@ class LoadPostalCodesCommand extends ContainerAwareCommand
$line ++;
}
$this->getContainer()->get('doctrine.orm.entity_manager')->flush();
$this->entityManager->flush();
$output->writeln('<info>'.$num.' were added !</info>');
}
@ -134,7 +161,7 @@ class LoadPostalCodesCommand extends ContainerAwareCommand
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln('handling row: '. $row[0].' | '. $row[1].' | '. $row[2]);
}
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$em = $this->entityManager;
$country = $em
->getRepository('ChillMainBundle:Country')
->findOneBy(array('countryCode' => $row[2]));
@ -160,7 +187,7 @@ class LoadPostalCodesCommand extends ContainerAwareCommand
->setCountry($country)
;
$errors = $this->getContainer()->get('validator')->validate($postalCode);
$errors = $this->validator->validate($postalCode);
if ($errors->count() == 0) {
$em->persist($postalCode);

View File

@ -20,19 +20,41 @@
namespace Chill\MainBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Chill\MainBundle\Entity\User;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
use Symfony\Component\Security\Core\Security;
/**
* Description of SetPasswordCommand
* Class SetPasswordCommand
*
* @package Chill\MainBundle\Command
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class SetPasswordCommand extends ContainerAwareCommand
class SetPasswordCommand extends Command
{
/**
* @var EntityManager
*/
private $entityManager;
/**
* SetPasswordCommand constructor.
*
* @param EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
parent::__construct();
}
public function configure()
{
$this->setName('chill:user:set_password')
@ -64,17 +86,21 @@ class SetPasswordCommand extends ContainerAwareCommand
public function _getUser($username)
{
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
return $em->getRepository('ChillMainBundle:User')
->findOneBy(array('username' => $username));
return $this->entityManager
->getRepository('ChillMainBundle:User')
->findOneBy(array('username' => $username));
}
public function _setPassword(User $user, $password)
{
$encoder = $this->getContainer()->get('security.encoder_factory')
->getEncoder($user);
$user->setPassword($encoder->encodePassword($password, $user->getSalt()));
$this->getContainer()->get('doctrine.orm.entity_manager')
->flush($user);
$defaultEncoder = new MessageDigestPasswordEncoder('sha512', true, 5000);
$encoders = [
User::class => $defaultEncoder
];
$encoderFactory = new EncoderFactory($encoders);
$user->setPassword(
$encoderFactory->getEncoder($user)->encodePassword($password, $user->getSalt())
);
$this->entityManager->flush($user);
}
}

View File

@ -176,6 +176,7 @@ class PermissionsGroupController extends Controller
foreach($roleScopes as $roleScope) {
/* @var $roleScope RoleScope */
$title = $roleProvider->getRoleTitle($roleScope->getRole());
var_dump($title);
$roleScopesSorted[$title][] = $roleScope;
}
ksort($roleScopesSorted);

View File

@ -5,11 +5,42 @@ services:
$logger: '@Psr\Log\LoggerInterface'
$passwordEncoder: '@Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface'
$validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
tags:
- { name: console.command }
Chill\MainBundle\Command\ChillUserSendRenewPasswordCodeCommand:
autowire: true
arguments:
$logger: '@Psr\Log\LoggerInterface'
$em: '@Doctrine\ORM\EntityManagerInterface'
$recoverPasswordHelper: '@Chill\MainBundle\Security\PasswordRecover\RecoverPasswordHelper'
$eventDispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface'
tags:
- { name: console.command }
Chill\MainBundle\Command\LoadAndUpdateLanguagesCommand:
arguments:
$entityManager: '@doctrine.orm.entity_manager'
$availableLanguages: '%chill_main.available_languages%'
tags:
- { name: console.command }
Chill\MainBundle\Command\LoadCountriesCommand:
arguments:
$entityManager: '@doctrine.orm.entity_manager'
$availableLanguages: '%chill_main.available_languages%'
tags:
- { name: console.command }
Chill\MainBundle\Command\LoadPostalCodesCommand:
arguments:
$entityManager: '@doctrine.orm.entity_manager'
$validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
tags:
- { name: console.command }
Chill\MainBundle\Command\SetPasswordCommand:
arguments:
$entityManager: '@doctrine.orm.entity_manager'
tags:
- { name: console.command }