Skip to content
Permalink
Browse files

[generate:plugin:migrate:dataparser] Add new command for generating d…

…ata parser plugin for migration. Fix #3773 (#3774)

* Adding command to generate data parser migrate plugin.

* Updating code styling in twig.

* Updating code styling in twig.

* Removing unwanted dependency from container.

* Adding option for plugin title,

* Adding dependency of extension manager.

* Changing the command name.
  • Loading branch information...
joshirohit100 authored and jmolivas committed Apr 22, 2018
1 parent f95cf2f commit 927a11b641b2df392a85ac5049a97cb0cf996bab
@@ -114,6 +114,11 @@ services:
arguments: [ '@console.plugin_migrate_process_generator', '@console.chain_queue', '@console.extension_manager', '@console.string_converter', '@console.validator']
tags:
- { name: drupal.command }
console.generate_plugin_migrate_data_parser:
class: Drupal\Console\Command\Generate\PluginMigrateDataParserCommand
arguments: [ '@console.plugin_migrate_data_parser_generator', '@console.chain_queue', '@console.extension_manager', '@console.string_converter', '@console.validator']
tags:
- { name: drupal.command }
console.generate_plugin_rest_resource:
class: Drupal\Console\Command\Generate\PluginRestResourceCommand
arguments: ['@console.extension_manager', '@console.plugin_rest_resource_generator','@console.string_converter', '@console.validator', '@console.chain_queue']
@@ -111,6 +111,11 @@ services:
arguments: ['@console.extension_manager']
tags:
- { name: drupal.generator }
console.plugin_migrate_data_parser_generator:
class: Drupal\Console\Generator\PluginMigrateDataParserGenerator
arguments: ['@console.extension_manager']
tags:
- { name: drupal.generator }
console.plugin_rest_resource_generator:
class: Drupal\Console\Generator\PluginRestResourceGenerator
arguments: ['@console.extension_manager']
@@ -0,0 +1,174 @@
<?php
/**
* @file
* Contains \Drupal\Console\Command\Generate\PluginMigrateDataParserCommand.
*/
namespace Drupal\Console\Command\Generate;
use Drupal\Console\Utils\Validator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\ContainerAwareCommand;
use Drupal\Console\Generator\PluginMigrateDataParserGenerator;
use Drupal\Console\Command\Shared\ModuleTrait;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Command\Shared\ConfirmationTrait;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Core\Utils\ChainQueue;
class PluginMigrateDataParserCommand extends ContainerAwareCommand
{
use ModuleTrait;
use ConfirmationTrait;
/**
* @var PluginMigrateProcessGenerator
*/
protected $generator;
/**
* @var ChainQueue
*/
protected $chainQueue;
/**
* @var Manager
*/
protected $extensionManager;
/**
* @var StringConverter
*/
protected $stringConverter;
/**
* @var Validator
*/
protected $validator;
/**
* PluginMigrateDataParserGenerator constructor.
*
* @param PluginMigrateDataParserGenerator $generator
* @param ChainQueue $chainQueue
* @param Manager $extensionManager
* @param StringConverter $stringConverter
* @param Validator $validator
*/
public function __construct(
PluginMigrateDataParserGenerator $generator,
ChainQueue $chainQueue,
Manager $extensionManager,
StringConverter $stringConverter,
Validator $validator
) {
$this->generator = $generator;
$this->chainQueue = $chainQueue;
$this->extensionManager = $extensionManager;
$this->stringConverter = $stringConverter;
$this->validator = $validator;
parent::__construct();
}
protected function configure()
{
$this
->setName('generate:plugin:migrate:dataparser')
->setDescription($this->trans('commands.generate.plugin.migrate.dataparser.description'))
->setHelp($this->trans('commands.generate.plugin.migrate.dataparser.help'))
->addOption(
'module',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.common.options.module')
)
->addOption(
'class',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.migrate.dataparser.options.class')
)
->addOption(
'plugin-id',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.migrate.dataparser.options.plugin-id')
)
->addOption(
'plugin-title',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.migrate.dataparser.options.plugin-title')
)->setAliases(['gpmdp']);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
if (!$this->confirmOperation()) {
return 1;
}
$module = $input->getOption('module');
$class_name = $this->validator->validateClassName($input->getOption('class'));
$plugin_id = $input->getOption('plugin-id');
$plugin_title = $input->getOption('plugin-title');
$this->generator->generate([
'module' => $module,
'class_name' => $class_name,
'plugin_id' => $plugin_id,
'plugin_title' => $plugin_title,
]);
$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
// 'module-name' option.
$module = $this->getModuleOption();
// 'class-name' option
$class = $input->getOption('class');
if (!$class) {
$class = $this->getIo()->ask(
$this->trans('commands.generate.plugin.migrate.dataparser.questions.class'),
ucfirst($this->stringConverter->underscoreToCamelCase($module)),
function ($class) {
return $this->validator->validateClassName($class);
}
);
$input->setOption('class', $class);
}
// 'plugin-id' option.
$pluginId = $input->getOption('plugin-id');
if (!$pluginId) {
$pluginId = $this->getIo()->ask(
$this->trans('commands.generate.plugin.migrate.dataparser.questions.plugin-id'),
$this->stringConverter->camelCaseToUnderscore($class)
);
$input->setOption('plugin-id', $pluginId);
}
// 'plugin-title' option.
$pluginTitle = $input->getOption('plugin-title');
if (!$pluginTitle) {
$pluginTitle = $this->getIo()->ask(
$this->trans('commands.generate.plugin.migrate.dataparser.questions.plugin-title'),
$this->stringConverter->camelCaseToUnderscore($class)
);
$input->setOption('plugin-title', $pluginTitle);
}
}
}
@@ -0,0 +1,45 @@
<?php
/**
* @file
* Contains \Drupal\Console\Generator\PluginMigrateDataParserGenerator.
*/
namespace Drupal\Console\Generator;
use Drupal\Console\Core\Generator\Generator;
use Drupal\Console\Extension\Manager;
class PluginMigrateDataParserGenerator extends Generator
{
/**
* @var Manager
*/
protected $extensionManager;
/**
* PluginMigrateDataParserGenerator constructor.
*
* @param Manager $extensionManager
*/
public function __construct(
Manager $extensionManager
) {
$this->extensionManager = $extensionManager;
}
/**
* {@inheritdoc}
*/
public function generate(array $parameters)
{
$module = $parameters['module'];
$class_name = $parameters['class_name'];
$this->renderFile(
'module/src/Plugin/migrate_plus/data_parser/data_parser.php.twig',
$this->extensionManager->getPluginPath($module, 'migrate_plus') . '/data_parser/' . $class_name . '.php',
$parameters
);
}
}
@@ -0,0 +1,39 @@
{% extends "base/class.php.twig" %}

{% block file_path %}
\Drupal\{{module}}\Plugin\migrate_plus\data_parser\{{class_name}}.
{% endblock %}

{% block namespace_class %}
namespace Drupal\{{module}}\Plugin\migrate_plus\data_parser;
{% endblock %}

{% block use_class %}
use Drupal\migrate_plus\DataParserPluginBase;
{% endblock %}

{% block class_declaration %}
/**
* Provides a '{{class_name}}' data parser plugin.
*
* @DataParser(
* id = "{{plugin_id}}"
* title = @Translation("{{plugin_title}}")
* )
*/
class {{class_name}} extends DataParserPluginBase {% endblock %}
{% block class_methods %}
/**
* {@inheritdoc}
*/
protected function openSourceUrl($url) {
// Plugin logic goes here.
}

/**
* {@inheritdoc}
*/
protected function fetchNextRow() {
// Plugin logic goes here.
}
{% endblock %}

0 comments on commit 927a11b

Please sign in to comment.
You can’t perform that action at this time.