diff --git a/app/code/Magento/Developer/Console/Command/XmlConverterCommand.php b/app/code/Magento/Developer/Console/Command/XmlConverterCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..14eeadcabbea6d495867b777c5bd90fbc3b5b9a2 --- /dev/null +++ b/app/code/Magento/Developer/Console/Command/XmlConverterCommand.php @@ -0,0 +1,127 @@ +<?php +/** + * Copyright © 2015 Magento. 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; + +/** + * 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; + + /** + * Inject dependencies + * + * @param Formatter $formatter + */ + public function __construct( + Formatter $formatter, + \DOMDocument $dom, + \XSLTProcessor $xsltProcessor + ) { + $this->formatter = $formatter; + $this->domXml = clone $dom; + $this->domXsl = clone $dom; + $this->xsltProcessor = $xsltProcessor; + + 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 styli sheet that going to be applied to XML file' + ), + new InputOption( + self::OVERWRITE_OPTION, + '-o', + InputOption::VALUE_NONE, + 'Overwrite file XML file' + ), + + ]); + + parent::configure(); + } + + /** + * {@inheritdoc} + * @throws \InvalidArgumentException + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + try { + $xmlFile = $input->getArgument(self::XML_FILE_ARGUMENT); + $this->domXml->preserveWhiteSpace = true; + $this->domXml->load($xmlFile); + + $this->domXsl->preserveWhiteSpace = true; + $this->domXsl->load($input->getArgument(self::PROCESSOR_ARGUMENT)); + + $this->xsltProcessor->registerPHPFunctions(); + $this->xsltProcessor->importStylesheet($this->domXsl); + $transformedDoc = $this->xsltProcessor->transformToXml($this->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 { + echo $result; + } + + return; + } catch (\Zend_Console_Getopt_Exception $e) { + $errorMessage = $e->getUsageMessage(); + $output->writeln("<error>$errorMessage</error>"); + return; + } catch (\Exception $exception) { + $errorMessage = $exception->getMessage(); + $output->writeln("<error>$errorMessage</error>"); + return; + } + } +} diff --git a/dev/tools/Magento/Tools/Layout/Formatter.php b/app/code/Magento/Developer/Model/Tools/Formatter.php similarity index 97% rename from dev/tools/Magento/Tools/Layout/Formatter.php rename to app/code/Magento/Developer/Model/Tools/Formatter.php index a016e32f203b53fff238855607b41ba652a93fff..94e3624401c68254eda4c3c5ee373285e075cfe2 100644 --- a/dev/tools/Magento/Tools/Layout/Formatter.php +++ b/app/code/Magento/Developer/Model/Tools/Formatter.php @@ -3,7 +3,8 @@ * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ -namespace Magento\Tools\Layout; + +namespace Magento\Developer\Model\Tools; class Formatter { diff --git a/app/code/Magento/Developer/etc/di.xml b/app/code/Magento/Developer/etc/di.xml index 94be2446cc1404c4c37cc2c1a740773ba7ba84f1..850b8c2f609f9a895374f3e402f7a3a8de9f00bc 100644 --- a/app/code/Magento/Developer/etc/di.xml +++ b/app/code/Magento/Developer/etc/di.xml @@ -31,6 +31,7 @@ <argument name="commands" xsi:type="array"> <item name="dev_tests_run" xsi:type="object">Magento\Developer\Console\Command\DevTestsRunCommand</item> <item name="dev_css_deploy" xsi:type="object">Magento\Developer\Console\Command\CssDeployCommand</item> + <item name="xml_converter" xsi:type="object">Magento\Developer\Console\Command\XmlConverterCommand</item> </argument> </arguments> </type>