123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Developer\Console\Command;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Magento\Developer\Model\Tools\Formatter;
- use Magento\Framework\DomDocument\DomDocumentFactory;
- use Magento\Framework\XsltProcessor\XsltProcessorFactory;
- /**
- * Class XmlConverterCommand
- * Converts XML file using XSL style sheets
- */
- class XmlConverterCommand extends Command
- {
- /**
- * XML file argument name constant
- */
- const XML_FILE_ARGUMENT = 'xml-file';
- /**
- * Processor argument constant
- */
- const PROCESSOR_ARGUMENT = 'processor';
- /**
- * Overwrite option constant
- */
- const OVERWRITE_OPTION = 'overwrite';
- /**
- * @var Formatter
- */
- private $formatter;
- /**
- * @var DomDocumentFactory
- */
- private $domFactory;
- /**
- * @var XsltProcessorFactory
- */
- private $xsltProcessorFactory;
- /**
- * Inject dependencies
- *
- * @param Formatter $formatter
- * @param DomDocumentFactory $domFactory
- * @param XsltProcessorFactory $xsltProcessorFactory
- */
- public function __construct(
- Formatter $formatter,
- DomDocumentFactory $domFactory,
- XsltProcessorFactory $xsltProcessorFactory
- ) {
- $this->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("<info>You saved converted XML into $xmlFile</info>");
- } else {
- $output->write($result);
- }
- return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
- } catch (\Exception $exception) {
- $errorMessage = $exception->getMessage();
- $output->writeln("<error>$errorMessage</error>");
- // we must have an exit code higher than zero to indicate something was wrong
- return \Magento\Framework\Console\Cli::RETURN_FAILURE;
- }
- }
- }
|