Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions
XmlConverterCommand Class Reference
Inheritance diagram for XmlConverterCommand:

Public Member Functions

 __construct (Formatter $formatter, DomDocumentFactory $domFactory, XsltProcessorFactory $xsltProcessorFactory)
 

Data Fields

const XML_FILE_ARGUMENT = 'xml-file'
 
const PROCESSOR_ARGUMENT = 'processor'
 
const OVERWRITE_OPTION = 'overwrite'
 

Protected Member Functions

 configure ()
 
 execute (InputInterface $input, OutputInterface $output)
 

Detailed Description

Class XmlConverterCommand Converts XML file using XSL style sheets

Definition at line 22 of file XmlConverterCommand.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Formatter  $formatter,
DomDocumentFactory  $domFactory,
XsltProcessorFactory  $xsltProcessorFactory 
)

Inject dependencies

Parameters
Formatter$formatter
DomDocumentFactory$domFactory
XsltProcessorFactory$xsltProcessorFactory

Definition at line 61 of file XmlConverterCommand.php.

65  {
66  $this->formatter = $formatter;
67  $this->domFactory = $domFactory;
68  $this->xsltProcessorFactory = $xsltProcessorFactory;
69 
70  parent::__construct();
71  }

Member Function Documentation

◆ configure()

configure ( )
protected

{}

Definition at line 76 of file XmlConverterCommand.php.

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  }

◆ execute()

execute ( InputInterface  $input,
OutputInterface  $output 
)
protected

{}

Definition at line 106 of file XmlConverterCommand.php.

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  }

Field Documentation

◆ OVERWRITE_OPTION

const OVERWRITE_OPTION = 'overwrite'

Overwrite option constant

Definition at line 37 of file XmlConverterCommand.php.

◆ PROCESSOR_ARGUMENT

const PROCESSOR_ARGUMENT = 'processor'

Processor argument constant

Definition at line 32 of file XmlConverterCommand.php.

◆ XML_FILE_ARGUMENT

const XML_FILE_ARGUMENT = 'xml-file'

XML file argument name constant

Definition at line 27 of file XmlConverterCommand.php.


The documentation for this class was generated from the following file: