Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
XmlCatalogGenerateCommand.php
Go to the documentation of this file.
1 <?php
8 
10 use Symfony\Component\Console\Command\Command;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Output\OutputInterface;
15 
24 class XmlCatalogGenerateCommand extends Command
25 {
29  const IDE_OPTION = 'ide';
30 
34  const IDE_FILE_PATH_ARGUMENT = 'path';
35 
39  private $filesUtility;
40 
44  private $urnResolver;
45 
49  private $readFactory;
50 
56  private $formats;
57 
64  public function __construct(
65  \Magento\Framework\App\Utility\Files $filesUtility,
66  \Magento\Framework\Config\Dom\UrnResolver $urnResolver,
67  \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory,
68  array $formats = []
69  ) {
70  $this->filesUtility = $filesUtility;
71  $this->urnResolver = $urnResolver;
72  $this->formats = $formats;
73  $this->readFactory = $readFactory;
74  parent::__construct();
75  }
76 
80  protected function configure()
81  {
82  $this->setName('dev:urn-catalog:generate')
83  ->setDescription('Generates the catalog of URNs to *.xsd mappings for the IDE to highlight xml.')
84  ->setDefinition([
85  new InputOption(
86  self::IDE_OPTION,
87  null,
88  InputOption::VALUE_REQUIRED,
89  'Format in which catalog will be generated. Supported: ['.
90  implode(', ', $this->getSupportedFormats()) . ']',
91  'phpstorm'
92  ),
93  new InputArgument(
94  self::IDE_FILE_PATH_ARGUMENT,
95  InputArgument::REQUIRED,
96  'Path to file to output the catalog. For PhpStorm use .idea/misc.xml'
97  )
98  ]);
99 
100  parent::configure();
101  }
102 
109  private function getUrnDictionary(OutputInterface $output)
110  {
111  $files = $this->filesUtility->getXmlCatalogFiles('*.xml');
112  $files = array_merge($files, $this->filesUtility->getXmlCatalogFiles('*.xsd'));
113 
114  $urns = [];
115  foreach ($files as $file) {
116  $fileDir = dirname($file[0]);
117  $fileName = basename($file[0]);
118  $read = $this->readFactory->create($fileDir);
119  $content = $read->readFile($fileName);
120  $matches = [];
121  preg_match_all('/schemaLocation="(urn\:magento\:[^"]*)"/i', $content, $matches);
122  if (isset($matches[1])) {
123  $urns = array_merge($urns, $matches[1]);
124  }
125  }
126  $urns = array_unique($urns);
127  $paths = [];
128  foreach ($urns as $urn) {
129  try {
130  $paths[$urn] = $this->urnResolver->getRealPath($urn);
131  } catch (\Exception $e) {
132  // don't add unsupported element to array
133  if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
134  $output->writeln($e->getMessage());
135  }
136  }
137  }
138  return $paths;
139  }
140 
145  protected function execute(InputInterface $input, OutputInterface $output)
146  {
147  $ideName = $input->getOption(self::IDE_OPTION);
148  $ideFilePath = $input->getArgument(self::IDE_FILE_PATH_ARGUMENT);
149 
150  $urnDictionary = $this->getUrnDictionary($output);
151  if ($formatter = $this->getFormatters($ideName)) {
152  $formatter->generateCatalog($urnDictionary, $ideFilePath);
153  } else {
154  throw new InputException(__("Format for IDE '%1' is not supported", $ideName));
155  }
156  }
157 
164  private function getFormatters($format)
165  {
166  $format = strtolower($format);
167  if (!isset($this->formats[$format])) {
168  return false;
169  }
170  return $this->formats[$format];
171  }
172 
178  public function getSupportedFormats()
179  {
180  return array_keys($this->formats);
181  }
182 }
execute(InputInterface $input, OutputInterface $output)
__()
Definition: __.php:13
$fileName
Definition: translate.phtml:15
$format
Definition: list.phtml:12
__construct(\Magento\Framework\App\Utility\Files $filesUtility, \Magento\Framework\Config\Dom\UrnResolver $urnResolver, \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory, array $formats=[])
$paths
Definition: _bootstrap.php:83
foreach($appDirs as $dir) $files