XmlConverterCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Console\Command;
  7. use Symfony\Component\Console\Command\Command;
  8. use Symfony\Component\Console\Input\InputArgument;
  9. use Symfony\Component\Console\Input\InputOption;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Magento\Developer\Model\Tools\Formatter;
  13. use Magento\Framework\DomDocument\DomDocumentFactory;
  14. use Magento\Framework\XsltProcessor\XsltProcessorFactory;
  15. /**
  16. * Class XmlConverterCommand
  17. * Converts XML file using XSL style sheets
  18. */
  19. class XmlConverterCommand extends Command
  20. {
  21. /**
  22. * XML file argument name constant
  23. */
  24. const XML_FILE_ARGUMENT = 'xml-file';
  25. /**
  26. * Processor argument constant
  27. */
  28. const PROCESSOR_ARGUMENT = 'processor';
  29. /**
  30. * Overwrite option constant
  31. */
  32. const OVERWRITE_OPTION = 'overwrite';
  33. /**
  34. * @var Formatter
  35. */
  36. private $formatter;
  37. /**
  38. * @var DomDocumentFactory
  39. */
  40. private $domFactory;
  41. /**
  42. * @var XsltProcessorFactory
  43. */
  44. private $xsltProcessorFactory;
  45. /**
  46. * Inject dependencies
  47. *
  48. * @param Formatter $formatter
  49. * @param DomDocumentFactory $domFactory
  50. * @param XsltProcessorFactory $xsltProcessorFactory
  51. */
  52. public function __construct(
  53. Formatter $formatter,
  54. DomDocumentFactory $domFactory,
  55. XsltProcessorFactory $xsltProcessorFactory
  56. ) {
  57. $this->formatter = $formatter;
  58. $this->domFactory = $domFactory;
  59. $this->xsltProcessorFactory = $xsltProcessorFactory;
  60. parent::__construct();
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function configure()
  66. {
  67. $this->setName('dev:xml:convert')
  68. ->setDescription('Converts XML file using XSL style sheets')
  69. ->setDefinition([
  70. new InputArgument(
  71. self::XML_FILE_ARGUMENT,
  72. InputArgument::REQUIRED,
  73. 'Path to XML file that going to be transformed'
  74. ),
  75. new InputArgument(
  76. self::PROCESSOR_ARGUMENT,
  77. InputArgument::REQUIRED,
  78. 'Path to XSL style sheet that going to be applied to XML file'
  79. ),
  80. new InputOption(
  81. self::OVERWRITE_OPTION,
  82. '-o',
  83. InputOption::VALUE_NONE,
  84. 'Overwrite XML file'
  85. ),
  86. ]);
  87. parent::configure();
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. protected function execute(InputInterface $input, OutputInterface $output)
  93. {
  94. try {
  95. $domXml = $this->domFactory->create();
  96. $domXsl = $this->domFactory->create();
  97. $xsltProcessor = $this->xsltProcessorFactory->create();
  98. $xmlFile = $input->getArgument(self::XML_FILE_ARGUMENT);
  99. $domXml->preserveWhiteSpace = true;
  100. $domXml->load($xmlFile);
  101. $domXsl->preserveWhiteSpace = true;
  102. $domXsl->load($input->getArgument(self::PROCESSOR_ARGUMENT));
  103. $xsltProcessor->registerPHPFunctions();
  104. $xsltProcessor->importStylesheet($domXsl);
  105. $transformedDoc = $xsltProcessor->transformToXml($domXml);
  106. $result = $this->formatter->format($transformedDoc);
  107. if ($input->getOption(self::OVERWRITE_OPTION)) {
  108. file_put_contents($input->getArgument(self::XML_FILE_ARGUMENT), $result);
  109. $output->writeln("<info>You saved converted XML into $xmlFile</info>");
  110. } else {
  111. $output->write($result);
  112. }
  113. return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
  114. } catch (\Exception $exception) {
  115. $errorMessage = $exception->getMessage();
  116. $output->writeln("<error>$errorMessage</error>");
  117. // we must have an exit code higher than zero to indicate something was wrong
  118. return \Magento\Framework\Console\Cli::RETURN_FAILURE;
  119. }
  120. }
  121. }