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

View File

@ -2,6 +2,7 @@
namespace Chill\CustomFieldsBundle\Controller;
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@ -24,7 +25,22 @@ use Chill\CustomFieldsBundle\Form\Type\CustomFieldType as FormTypeCustomField;
*/
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.
*
@ -243,7 +259,7 @@ class CustomFieldsGroupController extends Controller
{
$fieldChoices = array();
foreach ($this->get('chill.custom_field.provider')->getAllFields()
foreach ($this->customfieldProvider->getAllFields()
as $key => $customType) {
$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->load('services.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
//$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']