From 9f546658368691227f93eb01285819c26ad2c66f Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Tue, 26 Jan 2021 11:56:07 +0100 Subject: [PATCH] resolving deprecated private service injection on commands classes --- Command/CreateFieldsOnGroupCommand.php | 75 ++++++++++++++----- Controller/CustomFieldsGroupController.php | 20 ++++- .../ChillCustomFieldsExtension.php | 2 + config/services/command.yaml | 10 +++ config/services/controller.yaml | 9 +++ 5 files changed, 96 insertions(+), 20 deletions(-) create mode 100644 config/services/command.yaml create mode 100644 config/services/controller.yaml diff --git a/Command/CreateFieldsOnGroupCommand.php b/Command/CreateFieldsOnGroupCommand.php index 5b2232deb..b037975ab 100644 --- a/Command/CreateFieldsOnGroupCommand.php +++ b/Command/CreateFieldsOnGroupCommand.php @@ -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é * @author Marc Ducobu */ -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("Adding Custom Field of type " .$cf->getType()."\t with slug ".$cf->getSlug(). diff --git a/Controller/CustomFieldsGroupController.php b/Controller/CustomFieldsGroupController.php index 3b802d3b8..24efb0752 100644 --- a/Controller/CustomFieldsGroupController.php +++ b/Controller/CustomFieldsGroupController.php @@ -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(); } diff --git a/DependencyInjection/ChillCustomFieldsExtension.php b/DependencyInjection/ChillCustomFieldsExtension.php index 4d4a26db7..f6aabc0f1 100644 --- a/DependencyInjection/ChillCustomFieldsExtension.php +++ b/DependencyInjection/ChillCustomFieldsExtension.php @@ -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']) diff --git a/config/services/command.yaml b/config/services/command.yaml new file mode 100644 index 000000000..ec9b54d15 --- /dev/null +++ b/config/services/command.yaml @@ -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 } diff --git a/config/services/controller.yaml b/config/services/controller.yaml new file mode 100644 index 000000000..957128e36 --- /dev/null +++ b/config/services/controller.yaml @@ -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']