formatter = $formatter;
$this->domFactory = $domFactory;
$this->xsltProcessorFactory = $xsltProcessorFactory;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('dev:xml:convert')
->setDescription('Converts XML file using XSL style sheets')
->setDefinition([
new InputArgument(
self::XML_FILE_ARGUMENT,
InputArgument::REQUIRED,
'Path to XML file that going to be transformed'
),
new InputArgument(
self::PROCESSOR_ARGUMENT,
InputArgument::REQUIRED,
'Path to XSL style sheet that going to be applied to XML file'
),
new InputOption(
self::OVERWRITE_OPTION,
'-o',
InputOption::VALUE_NONE,
'Overwrite XML file'
),
]);
parent::configure();
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$domXml = $this->domFactory->create();
$domXsl = $this->domFactory->create();
$xsltProcessor = $this->xsltProcessorFactory->create();
$xmlFile = $input->getArgument(self::XML_FILE_ARGUMENT);
$domXml->preserveWhiteSpace = true;
$domXml->load($xmlFile);
$domXsl->preserveWhiteSpace = true;
$domXsl->load($input->getArgument(self::PROCESSOR_ARGUMENT));
$xsltProcessor->registerPHPFunctions();
$xsltProcessor->importStylesheet($domXsl);
$transformedDoc = $xsltProcessor->transformToXml($domXml);
$result = $this->formatter->format($transformedDoc);
if ($input->getOption(self::OVERWRITE_OPTION)) {
file_put_contents($input->getArgument(self::XML_FILE_ARGUMENT), $result);
$output->writeln("You saved converted XML into $xmlFile");
} else {
$output->write($result);
}
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
} catch (\Exception $exception) {
$errorMessage = $exception->getMessage();
$output->writeln("$errorMessage");
// we must have an exit code higher than zero to indicate something was wrong
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
}
}