fix service dependency injection for validatorInterface

This commit is contained in:
Mathieu Jaumotte 2021-03-30 11:13:36 +02:00
parent 6ea45e4d16
commit 3a50aea138
7 changed files with 81 additions and 22 deletions

View File

@ -14,6 +14,7 @@ use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\Role\RoleHierarchy; use Symfony\Component\Security\Core\Role\RoleHierarchy;
use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Form\Type\ComposedRoleScopeType; use Chill\MainBundle\Form\Type\ComposedRoleScopeType;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
/** /**
@ -43,6 +44,11 @@ class PermissionsGroupController extends AbstractController
*/ */
private $translator; private $translator;
/**
* @var ValidatorInterface
*/
private $validator;
/** /**
* PermissionsGroupController constructor. * PermissionsGroupController constructor.
* *
@ -50,18 +56,21 @@ class PermissionsGroupController extends AbstractController
* @param RoleProvider $roleProvider * @param RoleProvider $roleProvider
* @param RoleHierarchy $roleHierarchy * @param RoleHierarchy $roleHierarchy
* @param TranslatorInterface $translator * @param TranslatorInterface $translator
* @param ValidatorInterface $validator
*/ */
public function __construct( public function __construct(
TranslatableStringHelper $translatableStringHelper, TranslatableStringHelper $translatableStringHelper,
RoleProvider $roleProvider, RoleProvider $roleProvider,
RoleHierarchy $roleHierarchy, RoleHierarchy $roleHierarchy,
TranslatorInterface $translator TranslatorInterface $translator,
ValidatorInterface $validator
) )
{ {
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
$this->roleProvider = $roleProvider; $this->roleProvider = $roleProvider;
$this->roleHierarchy = $roleHierarchy; $this->roleHierarchy = $roleHierarchy;
$this->translator = $translator; $this->translator = $translator;
$this->validator = $validator;
} }
/** /**
@ -452,7 +461,7 @@ class PermissionsGroupController extends AbstractController
); );
$permissionsGroup->addRoleScope($roleScope); $permissionsGroup->addRoleScope($roleScope);
$violations = $this->get('validator')->validate($permissionsGroup); $violations = $this->validator->validate($permissionsGroup);
if ($violations->count() === 0) { if ($violations->count() === 0) {
$em->flush(); $em->flush();

View File

@ -11,6 +11,7 @@ use Chill\MainBundle\Form\UserType;
use Chill\MainBundle\Entity\GroupCenter; use Chill\MainBundle\Entity\GroupCenter;
use Chill\MainBundle\Form\Type\ComposedGroupCenterType; use Chill\MainBundle\Form\Type\ComposedGroupCenterType;
use Chill\MainBundle\Form\UserPasswordType; use Chill\MainBundle\Form\UserPasswordType;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/** /**
@ -28,14 +29,21 @@ class UserController extends AbstractController
*/ */
private $logger; private $logger;
/**
* @var ValidatorInterface
*/
private $validator;
/** /**
* UserController constructor. * UserController constructor.
* *
* @param LoggerInterface $logger * @param LoggerInterface $logger
* @param ValidatorInterface $validator
*/ */
public function __construct(LoggerInterface $logger) public function __construct(LoggerInterface $logger, ValidatorInterface $validator)
{ {
$this->logger = $logger; $this->logger = $logger;
$this->validator = $validator;
} }
/** /**
@ -258,7 +266,7 @@ class UserController extends AbstractController
$form[self::FORM_GROUP_CENTER_COMPOSED]->getData()); $form[self::FORM_GROUP_CENTER_COMPOSED]->getData());
$user->addGroupCenter($groupCenter); $user->addGroupCenter($groupCenter);
if ($this->get('validator')->validate($user)->count() === 0) { if ($this->validator->validate($user)->count() === 0) {
$em->flush(); $em->flush();
$this->addFlash('success', $this->get('translator')->trans('The ' $this->addFlash('success', $this->get('translator')->trans('The '
@ -267,7 +275,7 @@ class UserController extends AbstractController
return $this->redirect($this->generateUrl('admin_user_edit', return $this->redirect($this->generateUrl('admin_user_edit',
array('id' => $uid))); array('id' => $uid)));
} else { } else {
foreach($this->get('validator')->validate($user) as $error) foreach($this->validator->validate($user) as $error)
$this->addFlash('error', $error->getMessage()); $this->addFlash('error', $error->getMessage());
} }
} }
@ -380,7 +388,7 @@ class UserController extends AbstractController
// logging for prod // logging for prod
$this->logger->info('update password for an user', [ $this->logger->info('update password for an user', [
'by' => $this->getUser()->getUsername(), 'by' => $this->getUser()->getUsername(),
'user' => $user->getUsername() 'user' => $user->getUsername()
]); ]);

View File

@ -1,16 +1,16 @@
services: services:
Chill\MainBundle\Controller\: Chill\MainBundle\Controller\:
autowire: true autowire: true
resource: '../../Controller' resource: '../../Controller'
tags: ['controller.service_arguments'] tags: ['controller.service_arguments']
Chill\MainBundle\Controller\PasswordController: Chill\MainBundle\Controller\PasswordController:
autowire: true autowire: true
arguments: arguments:
$chillLogger: '@monolog.logger.chill' $chillLogger: '@monolog.logger.chill'
tags: ['controller.service_arguments'] tags: ['controller.service_arguments']
Chill\MainBundle\Controller\SearchController: Chill\MainBundle\Controller\SearchController:
arguments: arguments:
$searchProvider: '@chill_main.search_provider' $searchProvider: '@chill_main.search_provider'
@ -24,9 +24,11 @@ services:
$roleProvider: '@chill.main.role_provider' $roleProvider: '@chill.main.role_provider'
$roleHierarchy: '@security.role_hierarchy' $roleHierarchy: '@security.role_hierarchy'
$translator: '@Symfony\Contracts\Translation\TranslatorInterface' $translator: '@Symfony\Contracts\Translation\TranslatorInterface'
$validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
tags: ['controller.service_arguments'] tags: ['controller.service_arguments']
Chill\MainBundle\Controller\UserController: Chill\MainBundle\Controller\UserController:
arguments: arguments:
$logger: '@Psr\Log\LoggerInterface' $logger: '@Psr\Log\LoggerInterface'
tags: ['controller.service_arguments'] $validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
tags: ['controller.service_arguments']

View File

@ -32,6 +32,7 @@ use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/** /**
* Class AccompanyingPeriodController * Class AccompanyingPeriodController
@ -46,13 +47,20 @@ class AccompanyingPeriodController extends AbstractController
protected $eventDispatcher; protected $eventDispatcher;
/** /**
* ReportController constructor. * @var ValidatorInterface
*/
protected $validator;
/**
* AccompanyingPeriodController constructor.
* *
* @param EventDispatcherInterface $eventDispatcher * @param EventDispatcherInterface $eventDispatcher
* @param ValidatorInterface $validator
*/ */
public function __construct(EventDispatcherInterface $eventDispatcher) public function __construct(EventDispatcherInterface $eventDispatcher, ValidatorInterface $validator)
{ {
$this->eventDispatcher = $eventDispatcher; $this->eventDispatcher = $eventDispatcher;
$this->validator = $validator;
} }
/** /**
@ -285,12 +293,14 @@ class AccompanyingPeriodController extends AbstractController
* @param Person $person * @param Person $person
* @return \Symfony\Component\Validator\ConstraintViolationListInterface * @return \Symfony\Component\Validator\ConstraintViolationListInterface
*/ */
private function _validatePerson(Person $person) { private function _validatePerson(Person $person)
$errors = $this->get('validator')->validate($person, null, {
$errors = $this->validator->validate($person, null,
array('Default')); array('Default'));
$errors_accompanying_period = $this->get('validator')->validate($person, null,
array('accompanying_period_consistent'));
$errors_accompanying_period = $this->validator->validate($person, null,
array('accompanying_period_consistent'));
foreach($errors_accompanying_period as $error ) { foreach($errors_accompanying_period as $error ) {
$errors->add($error); $errors->add($error);
} }

View File

@ -29,6 +29,7 @@ use Chill\MainBundle\Form\Type\AddressType;
use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Entity\Address;
use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Criteria;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/** /**
* Class PersonAddressController * Class PersonAddressController
@ -40,7 +41,21 @@ use Symfony\Component\HttpFoundation\Request;
*/ */
class PersonAddressController extends AbstractController class PersonAddressController extends AbstractController
{ {
/**
* @var ValidatorInterface
*/
protected $validator;
/**
* PersonAddressController constructor.
*
* @param ValidatorInterface $validator
*/
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}
public function listAction($person_id) public function listAction($person_id)
{ {
$person = $this->getDoctrine()->getManager() $person = $this->getDoctrine()->getManager()
@ -302,9 +317,9 @@ class PersonAddressController extends AbstractController
*/ */
private function validatePerson(Person $person) private function validatePerson(Person $person)
{ {
$errors = $this->get('validator') $errors = $this->validator
->validate($person, null, array('Default')); ->validate($person, null, array('Default'));
$errors_addresses_consistent = $this->get('validator') $errors_addresses_consistent = $this->validator
->validate($person, null, array('addresses_consistent')); ->validate($person, null, array('addresses_consistent'));
foreach($errors_addresses_consistent as $error) { foreach($errors_addresses_consistent as $error) {

View File

@ -38,6 +38,7 @@ use Symfony\Component\Translation\TranslatorInterface;
use Chill\MainBundle\Search\SearchProvider; use Chill\MainBundle\Search\SearchProvider;
use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper; use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/** /**
* Class PersonController * Class PersonController
@ -80,6 +81,11 @@ class PersonController extends AbstractController
* @var \Psr\Log\LoggerInterface * @var \Psr\Log\LoggerInterface
*/ */
private $logger; private $logger;
/**
* @var ValidatorInterface
*/
private $validator;
public function __construct( public function __construct(
SimilarPersonMatcher $similarPersonMatcher, SimilarPersonMatcher $similarPersonMatcher,
@ -87,7 +93,8 @@ class PersonController extends AbstractController
EventDispatcherInterface $eventDispatcher, EventDispatcherInterface $eventDispatcher,
PersonRepository $personRepository, PersonRepository $personRepository,
ConfigPersonAltNamesHelper $configPersonAltNameHelper, ConfigPersonAltNamesHelper $configPersonAltNameHelper,
LoggerInterface $logger LoggerInterface $logger,
ValidatorInterface $validator
) { ) {
$this->similarPersonMatcher = $similarPersonMatcher; $this->similarPersonMatcher = $similarPersonMatcher;
$this->translator = $translator; $this->translator = $translator;
@ -95,6 +102,7 @@ class PersonController extends AbstractController
$this->configPersonAltNameHelper = $configPersonAltNameHelper; $this->configPersonAltNameHelper = $configPersonAltNameHelper;
$this->personRepository = $personRepository; $this->personRepository = $personRepository;
$this->logger = $logger; $this->logger = $logger;
$this->validator = $validator;
} }
public function getCFGroup() public function getCFGroup()
@ -267,14 +275,14 @@ class PersonController extends AbstractController
*/ */
private function _validatePersonAndAccompanyingPeriod(Person $person) private function _validatePersonAndAccompanyingPeriod(Person $person)
{ {
$errors = $this->get('validator') $errors = $this->validator
->validate($person, null, array('creation')); ->validate($person, null, array('creation'));
//validate accompanying periods //validate accompanying periods
$periods = $person->getAccompanyingPeriods(); $periods = $person->getAccompanyingPeriods();
foreach ($periods as $period) { foreach ($periods as $period) {
$period_errors = $this->get('validator') $period_errors = $this->validator
->validate($period); ->validate($period);
//group errors : //group errors :

View File

@ -7,6 +7,7 @@ services:
$personRepository: '@Chill\PersonBundle\Repository\PersonRepository' $personRepository: '@Chill\PersonBundle\Repository\PersonRepository'
$configPersonAltNameHelper: '@Chill\PersonBundle\Config\ConfigPersonAltNamesHelper' $configPersonAltNameHelper: '@Chill\PersonBundle\Config\ConfigPersonAltNamesHelper'
$logger: '@Psr\Log\LoggerInterface' $logger: '@Psr\Log\LoggerInterface'
$validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
tags: ['controller.service_arguments'] tags: ['controller.service_arguments']
Chill\PersonBundle\Controller\TimelinePersonController: Chill\PersonBundle\Controller\TimelinePersonController:
@ -19,6 +20,12 @@ services:
Chill\PersonBundle\Controller\AccompanyingPeriodController: Chill\PersonBundle\Controller\AccompanyingPeriodController:
arguments: arguments:
$eventDispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface' $eventDispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface'
$validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
tags: ['controller.service_arguments']
Chill\PersonBundle\Controller\PersonAddressController:
arguments:
$validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
tags: ['controller.service_arguments'] tags: ['controller.service_arguments']
Chill\PersonBundle\Controller\AdminController: ~ Chill\PersonBundle\Controller\AdminController: ~