Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[generate:plugin:migrate:dataparser] Add new command for generating data parser plugin for migration. Fix #3773 #3774

Merged
merged 7 commits into from Apr 22, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/services/generate.yml
Expand Up @@ -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']
Expand Down
5 changes: 5 additions & 0 deletions config/services/generator.yml
Expand Up @@ -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']
Expand Down
174 changes: 174 additions & 0 deletions src/Command/Generate/PluginMigrateDataParserCommand.php
@@ -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:data_parser')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshirohit100 Normally, underscore is not used in command name.
some examples generate:plugin:rulesaction, generate:plugin:rulesaction and etc.

Maybe it would be better to use something like this generate:plugin:migrate:data:parse. So, if we add also data fetcher we will have generate:plugin:migrate:data:fetcher. What do you think ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with both generate:plugin:migrate:dataparser or generate:plugin:migrate:data:parse
Lets see what other say

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like generate:plugin:migrate:dataparser better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I have updated the PR

->setDescription($this->trans('commands.generate.plugin.migrate.data_parser.description'))
->setHelp($this->trans('commands.generate.plugin.migrate.data_parser.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.data_parser.options.class')
)
->addOption(
'plugin-id',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.migrate.data_parser.options.plugin-id')
)
->addOption(
'plugin-title',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.plugin.migrate.data_parser.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.data_parser.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.data_parser.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.data_parser.questions.plugin-title'),
$this->stringConverter->camelCaseToUnderscore($class)
);
$input->setOption('plugin-title', $pluginTitle);
}
}
}
45 changes: 45 additions & 0 deletions src/Generator/PluginMigrateDataParserGenerator.php
@@ -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 %}