Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GenerateDevUrnCommand.php
Go to the documentation of this file.
1 <?php
2 // @codingStandardsIgnoreFile
7 declare(strict_types = 1);
8 
10 
12 use Symfony\Component\Console\Command\Command;
13 use Symfony\Component\Console\Input\InputArgument;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Output\OutputInterface;
16 use Symfony\Component\Console\Input\InputOption;
18 
19 class GenerateDevUrnCommand extends Command
20 {
26  protected function configure()
27  {
28  $this->setName('generate:urn-catalog')
29  ->setDescription('This command generates an URN catalog to enable PHPStorm to recognize and highlight URNs.')
30  ->addArgument('path', InputArgument::REQUIRED, 'path to PHPStorm misc.xml file (typically located in [ProjectRoot]/.idea/misc.xml)')
31  ->addOption(
32  "force",
33  'f',
34  InputOption::VALUE_NONE,
35  'forces creation of misc.xml file if not found in the path given.'
36  );
37  }
38 
47  protected function execute(InputInterface $input, OutputInterface $output)
48  {
49  $miscXmlFilePath = $input->getArgument('path') . DIRECTORY_SEPARATOR . "misc.xml";
50  $miscXmlFile = realpath($miscXmlFilePath);
51  $force = $input->getOption('force');
52 
53  if ($miscXmlFile === false) {
54  if ($force == true) {
55  // create file and refresh realpath
56  $xml = "<project version=\"4\"/>";
57  file_put_contents($miscXmlFilePath, $xml);
58  $miscXmlFile = realpath($miscXmlFilePath);
59  } else {
60  $exceptionMessage = "misc.xml not found in given path '{$miscXmlFilePath}'";
61  LoggingUtil::getInstance()->getLogger(GenerateDevUrnCommand::class)
62  ->error($exceptionMessage);
63  throw new TestFrameworkException($exceptionMessage);
64  }
65  }
66  $dom = new \DOMDocument('1.0');
67  $dom->preserveWhiteSpace = false;
68  $dom->formatOutput = true;
69  $dom->loadXML(file_get_contents($miscXmlFile));
70 
71  //Locate ProjectResources node, create one if none are found.
72  $nodeForWork = null;
73  foreach($dom->getElementsByTagName('component') as $child) {
74  if ($child->getAttribute('name') === 'ProjectResources') {
75  $nodeForWork = $child;
76  }
77  }
78  if ($nodeForWork === null) {
79  $project = $dom->getElementsByTagName('project')->item(0);
80  $nodeForWork = $dom->createElement('component');
81  $nodeForWork->setAttribute('name', 'ProjectResources');
82  $project->appendChild($nodeForWork);
83  }
84 
85  //Extract url=>location mappings that already exist, add MFTF URNs and reappend
86  $resources = [];
87  $resourceNodes = $nodeForWork->getElementsByTagName('resource');
88  $resourceCount = $resourceNodes->length;
89  for ($i = 0; $i < $resourceCount; $i++) {
90  $child = $resourceNodes[0];
91  $resources[$child->getAttribute('url')] = $child->getAttribute('location');
92  $child->parentNode->removeChild($child);
93  }
94 
95  $resources = array_merge($resources, $this->generateResourcesArray());
96 
97  foreach ($resources as $url => $location) {
98  $resourceNode = $dom->createElement('resource');
99  $resourceNode->setAttribute('url', $url);
100  $resourceNode->setAttribute('location', $location);
101  $nodeForWork->appendChild($resourceNode);
102  }
103 
104  //Save output
105  $dom->save($miscXmlFile);
106  $output->writeln("MFTF URN mapping successfully added to {$miscXmlFile}.");
107  }
108 
113  private function generateResourcesArray()
114  {
115  $resourcesArray = [
116  'urn:magento:mftf:DataGenerator/etc/dataOperation.xsd' =>
117  realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/DataGenerator/etc/dataOperation.xsd'),
118  'urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd' =>
119  realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/DataGenerator/etc/dataProfileSchema.xsd'),
120  'urn:magento:mftf:Page/etc/PageObject.xsd' =>
121  realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/Page/etc/PageObject.xsd'),
122  'urn:magento:mftf:Page/etc/SectionObject.xsd' =>
123  realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/Page/etc/SectionObject.xsd'),
124  'urn:magento:mftf:Test/etc/actionGroupSchema.xsd' =>
125  realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/Test/etc/actionGroupSchema.xsd'),
126  'urn:magento:mftf:Test/etc/testSchema.xsd' =>
127  realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/Test/etc/testSchema.xsd'),
128  'urn:magento:mftf:Suite/etc/suiteSchema.xsd' =>
129  realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/Suite/etc/suiteSchema.xsd')
130  ];
131  return $resourcesArray;
132  }
133 
134 }
execute(InputInterface $input, OutputInterface $output)
$i
Definition: gallery.phtml:31