-
-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
1 parent
f95cf2f
commit 927a11b
Showing
5 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
src/Command/Generate/PluginMigrateDataParserCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
templates/module/src/Plugin/migrate_plus/data_parser/data_parser.php.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %} |