resolving deprecated private service injection on commands classes

This commit is contained in:
Mathieu Jaumotte 2021-01-26 11:56:07 +01:00
parent e053bf999e
commit 9f54665836
5 changed files with 96 additions and 20 deletions

View File

@ -20,14 +20,16 @@
namespace Chill\CustomFieldsBundle\Command; namespace Chill\CustomFieldsBundle\Command;
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
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;
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup; use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Question\Question;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Yaml\Parser; use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Exception\ParseException;
use Chill\CustomFieldsBundle\Entity\CustomField; use Chill\CustomFieldsBundle\Entity\CustomField;
@ -39,11 +41,53 @@ use Chill\CustomFieldsBundle\Entity\CustomField;
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Marc Ducobu <marc.ducobu@champs-libres.coop> * @author Marc Ducobu <marc.ducobu@champs-libres.coop>
*/ */
class CreateFieldsOnGroupCommand extends ContainerAwareCommand class CreateFieldsOnGroupCommand extends Command
{ {
const ARG_PATH = 'path'; const ARG_PATH = 'path';
const ARG_DELETE = 'delete'; const ARG_DELETE = 'delete';
/**
* @var CustomFieldProvider
*/
private $customFieldProvider;
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var ValidatorInterface
*/
private $validator;
private $availableLanguages;
private $customizablesEntities;
/**
* CreateFieldsOnGroupCommand constructor.
*
* @param CustomFieldProvider $customFieldProvider
* @param EntityManager $entityManager
* @param ValidatorInterface $validator
* @param $availableLanguages
* @param $customizablesEntities
*/
public function __construct(
CustomFieldProvider $customFieldProvider,
EntityManager $entityManager,
ValidatorInterface $validator,
$availableLanguages,
$customizablesEntities
) {
$this->customFieldProvider = $customFieldProvider;
$this->entityManager = $entityManager;
$this->validator = $validator;
$this->availableLanguages = $availableLanguages;
$this->customizablesEntities = $customizablesEntities;
parent::__construct();
}
protected function configure() protected function configure()
{ {
$this->setName('chill:custom_fields:populate_group') $this->setName('chill:custom_fields:populate_group')
@ -61,8 +105,7 @@ class CreateFieldsOnGroupCommand extends ContainerAwareCommand
*/ */
protected function deleteFieldsForCFGroup($customFieldsGroup) protected function deleteFieldsForCFGroup($customFieldsGroup)
{ {
$em = $this->getContainer() $em = $this->entityManager;
->get('doctrine.orm.default_entity_manager');
foreach ($customFieldsGroup->getCustomFields() as $field) { foreach ($customFieldsGroup->getCustomFields() as $field) {
$em->remove($field); $em->remove($field);
@ -78,8 +121,7 @@ class CreateFieldsOnGroupCommand extends ContainerAwareCommand
{ {
$helper = $this->getHelperSet()->get('question'); $helper = $this->getHelperSet()->get('question');
$em = $this->getContainer() $em = $this->entityManager;
->get('doctrine.orm.default_entity_manager');
$customFieldsGroups = $em $customFieldsGroups = $em
->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup') ->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')
@ -94,7 +136,7 @@ class CreateFieldsOnGroupCommand extends ContainerAwareCommand
$table $table
->setHeaders(array_merge( ->setHeaders(array_merge(
['id', 'entity'], ['id', 'entity'],
$this->getContainer()->getParameter('chill_main.available_languages') $this->availableLanguages
)) ))
->setRows($this->_prepareRows($customFieldsGroups)) ->setRows($this->_prepareRows($customFieldsGroups))
->render() ->render()
@ -129,12 +171,10 @@ class CreateFieldsOnGroupCommand extends ContainerAwareCommand
private function _prepareRows ($customFieldsGroups) private function _prepareRows ($customFieldsGroups)
{ {
$rows = array(); $rows = array();
$languages = $this->getContainer() $languages = $this->availableLanguages;
->getParameter('chill_main.available_languages');
//gather entitites and create an array to access them easily //gather entitites and create an array to access them easily
$customizableEntities = array(); $customizableEntities = array();
foreach ($this->getContainer() foreach ($this->customizablesEntities as $entry) {
->getParameter('chill_custom_fields.customizables_entities') as $entry) {
$customizableEntities[$entry['class']] = $entry['name']; $customizableEntities[$entry['class']] = $entry['name'];
} }
@ -179,15 +219,14 @@ class CreateFieldsOnGroupCommand extends ContainerAwareCommand
private function _addFields(CustomFieldsGroup $group, $values, OutputInterface $output) private function _addFields(CustomFieldsGroup $group, $values, OutputInterface $output)
{ {
$cfProvider = $this->getContainer()->get('chill.custom_field.provider');
$em = $this->getContainer()->get('doctrine.orm.default_entity_manager'); $em = $this->entityManager;
$languages = $this->getContainer() $languages = $this->availableLanguages;
->getParameter('chill_main.available_languages');
foreach($values['fields'] as $slug => $field) { foreach($values['fields'] as $slug => $field) {
//check the cf type exists //check the cf type exists
$cfType = $cfProvider->getCustomFieldByType($field['type']); $cfType = $this->customFieldProvider->getCustomFieldByType($field['type']);
if ($cfType === NULL) { if ($cfType === NULL) {
throw new \RunTimeException('the type '.$field['type'].' ' throw new \RunTimeException('the type '.$field['type'].' '
. 'does not exists'); . 'does not exists');
@ -210,7 +249,7 @@ class CreateFieldsOnGroupCommand extends ContainerAwareCommand
'Not available in this language'; 'Not available in this language';
} }
if ($this->getContainer()->get('validator')->validate($cf)) { if ($this->validator->validate($cf)) {
$em->persist($cf); $em->persist($cf);
$output->writeln("<info>Adding Custom Field of type " $output->writeln("<info>Adding Custom Field of type "
.$cf->getType()."\t with slug ".$cf->getSlug(). .$cf->getType()."\t with slug ".$cf->getSlug().

View File

@ -2,6 +2,7 @@
namespace Chill\CustomFieldsBundle\Controller; namespace Chill\CustomFieldsBundle\Controller;
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@ -24,7 +25,22 @@ use Chill\CustomFieldsBundle\Form\Type\CustomFieldType as FormTypeCustomField;
*/ */
class CustomFieldsGroupController extends Controller class CustomFieldsGroupController extends Controller
{ {
/**
* @var CustomFieldProvider
*/
private $customfieldProvider;
/**
* CustomFieldsGroupController constructor.
*
* @param CustomFieldProvider $customFieldProvider
*/
public function __construct(CustomFieldProvider $customFieldProvider)
{
$this->customfieldProvider = $customFieldProvider;
}
/** /**
* Lists all CustomFieldsGroup entities. * Lists all CustomFieldsGroup entities.
* *
@ -243,7 +259,7 @@ class CustomFieldsGroupController extends Controller
{ {
$fieldChoices = array(); $fieldChoices = array();
foreach ($this->get('chill.custom_field.provider')->getAllFields() foreach ($this->customfieldProvider->getAllFields()
as $key => $customType) { as $key => $customType) {
$fieldChoices[$key] = $customType->getName(); $fieldChoices[$key] = $customType->getName();
} }

View File

@ -26,6 +26,8 @@ class ChillCustomFieldsExtension extends Extension implements PrependExtensionIn
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../config')); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../config'));
$loader->load('services.yaml'); $loader->load('services.yaml');
$loader->load('services/fixtures.yaml'); $loader->load('services/fixtures.yaml');
$loader->load('services/controller.yaml');
$loader->load('services/command.yaml');
//add at least a blank array at 'customizable_entities' options //add at least a blank array at 'customizable_entities' options
//$customizable_entities = (isset($config['customizables_entities']) //$customizable_entities = (isset($config['customizables_entities'])

View File

@ -0,0 +1,10 @@
services:
Chill\CustomFieldsBundle\Command\CreateFieldsOnGroupCommand:
arguments:
$customFieldProvider: '@chill.custom_field.provider'
$entityManager: '@doctrine.orm.default_entity_manager'
$validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
$availableLanguages: '%chill_main.available_languages%'
$customizablesEntities: '%chill_custom_fields.customizables_entities%'
tags:
- { name: console.command }

View File

@ -0,0 +1,9 @@
services:
Chill\CustomFieldsBundle\Controller\:
resource: '../../Controller'
tags: ['controller.service_arguments']
Chill\CustomFieldsBundle\Controller\CustomFieldsGroupController:
arguments:
$customFieldProvider: '@chill.custom_field.provider'
tags: ['controller.service_arguments']