Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
XmlConverterCommand.php
Go to the documentation of this file.
1 <?php
8 
9 use Symfony\Component\Console\Command\Command;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
17 
22 class XmlConverterCommand extends Command
23 {
27  const XML_FILE_ARGUMENT = 'xml-file';
28 
32  const PROCESSOR_ARGUMENT = 'processor';
33 
37  const OVERWRITE_OPTION = 'overwrite';
38 
42  private $formatter;
43 
47  private $domFactory;
48 
52  private $xsltProcessorFactory;
53 
61  public function __construct(
62  Formatter $formatter,
63  DomDocumentFactory $domFactory,
64  XsltProcessorFactory $xsltProcessorFactory
65  ) {
66  $this->formatter = $formatter;
67  $this->domFactory = $domFactory;
68  $this->xsltProcessorFactory = $xsltProcessorFactory;
69 
70  parent::__construct();
71  }
72 
76  protected function configure()
77  {
78  $this->setName('dev:xml:convert')
79  ->setDescription('Converts XML file using XSL style sheets')
80  ->setDefinition([
81  new InputArgument(
82  self::XML_FILE_ARGUMENT,
83  InputArgument::REQUIRED,
84  'Path to XML file that going to be transformed'
85  ),
86  new InputArgument(
87  self::PROCESSOR_ARGUMENT,
88  InputArgument::REQUIRED,
89  'Path to XSL style sheet that going to be applied to XML file'
90  ),
91  new InputOption(
92  self::OVERWRITE_OPTION,
93  '-o',
94  InputOption::VALUE_NONE,
95  'Overwrite XML file'
96  ),
97 
98  ]);
99 
100  parent::configure();
101  }
102 
106  protected function execute(InputInterface $input, OutputInterface $output)
107  {
108  try {
109  $domXml = $this->domFactory->create();
110  $domXsl = $this->domFactory->create();
111  $xsltProcessor = $this->xsltProcessorFactory->create();
112 
113  $xmlFile = $input->getArgument(self::XML_FILE_ARGUMENT);
114  $domXml->preserveWhiteSpace = true;
115  $domXml->load($xmlFile);
116 
117  $domXsl->preserveWhiteSpace = true;
118  $domXsl->load($input->getArgument(self::PROCESSOR_ARGUMENT));
119 
120  $xsltProcessor->registerPHPFunctions();
121  $xsltProcessor->importStylesheet($domXsl);
122  $transformedDoc = $xsltProcessor->transformToXml($domXml);
123  $result = $this->formatter->format($transformedDoc);
124 
125  if ($input->getOption(self::OVERWRITE_OPTION)) {
126  file_put_contents($input->getArgument(self::XML_FILE_ARGUMENT), $result);
127  $output->writeln("<info>You saved converted XML into $xmlFile</info>");
128  } else {
129  $output->write($result);
130  }
131 
132  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
133  } catch (\Exception $exception) {
134  $errorMessage = $exception->getMessage();
135  $output->writeln("<error>$errorMessage</error>");
136  // we must have an exit code higher than zero to indicate something was wrong
137  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
138  }
139  }
140 }
__construct(Formatter $formatter, DomDocumentFactory $domFactory, XsltProcessorFactory $xsltProcessorFactory)
execute(InputInterface $input, OutputInterface $output)