Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PhpStorm.php
Go to the documentation of this file.
1 <?php
8 
15 
19 class PhpStorm implements FormatInterface
20 {
24  private $currentDirRead;
25 
29  private $fileWriteFactory;
30 
34  private $domDocumentFactory;
35 
41  public function __construct(
42  ReadFactory $readFactory,
43  WriteFactory $fileWriteFactory,
44  DomDocumentFactory $domDocumentFactory = null
45  ) {
46  $this->currentDirRead = $readFactory->create(getcwd());
47  $this->fileWriteFactory = $fileWriteFactory;
48  $this->domDocumentFactory = $domDocumentFactory ?: ObjectManager::getInstance()->get(DomDocumentFactory::class);
49  }
50 
58  public function generateCatalog(array $dictionary, $configFilePath)
59  {
60  $componentNode = null;
61  $projectNode = null;
62 
63  try {
64  $file = $this->fileWriteFactory->create(
65  $configFilePath,
66  \Magento\Framework\Filesystem\DriverPool::FILE,
67  'r'
68  );
69  $dom = $this->domDocumentFactory->create();
70  $fileContent = $file->readAll();
71  if (!empty($fileContent)) {
72  $dom->loadXML($fileContent);
73  } else {
74  $this->initEmptyFile($dom);
75  }
76  $xpath = new \DOMXPath($dom);
77  $nodeList = $xpath->query('/project');
78  $projectNode = $nodeList->item(0);
79  $file->close();
80  } catch (FileSystemException $f) {
81  //create file if does not exists
82  $dom = $this->domDocumentFactory->create();
83  $projectNode = $this->initEmptyFile($dom);
84  }
85 
86  $xpath = new \DOMXPath($dom);
87  $nodeList = $xpath->query("/project/component[@name='ProjectResources']");
88  $componentNode = $nodeList->item(0);
89  if ($componentNode == null) {
90  $componentNode = $dom->createElement('component');
91  $componentNode->setAttribute('name', 'ProjectResources');
92  $projectNode->appendChild($componentNode);
93  }
94 
95  foreach ($dictionary as $urn => $xsdPath) {
96  $node = $dom->createElement('resource');
97  $node->setAttribute('url', $urn);
98  $node->setAttribute('location', $xsdPath);
99  $componentNode->appendChild($node);
100  }
101  $dom->formatOutput = true;
102  $file = $this->fileWriteFactory->create(
103  $configFilePath,
104  \Magento\Framework\Filesystem\DriverPool::FILE,
105  'w'
106  );
107  $file->write($dom->saveXML());
108  $file->close();
109  }
110 
117  private function initEmptyFile(\DOMDocument $dom)
118  {
119  $projectNode = $dom->createElement('project');
120 
121  //PhpStorm 9 version for component is "4"
122  $projectNode->setAttribute('version', '4');
123  $dom->appendChild($projectNode);
124  $rootComponentNode = $dom->createElement('component');
125 
126  //PhpStorm 9 version for ProjectRootManager is "2"
127  $rootComponentNode->setAttribute('version', '2');
128  $rootComponentNode->setAttribute('name', 'ProjectRootManager');
129  $projectNode->appendChild($rootComponentNode);
130  return $projectNode;
131  }
132 }
generateCatalog(array $dictionary, $configFilePath)
Definition: PhpStorm.php:58
create($path, $driverCode=DriverPool::FILE)
Definition: ReadFactory.php:36
__construct(ReadFactory $readFactory, WriteFactory $fileWriteFactory, DomDocumentFactory $domDocumentFactory=null)
Definition: PhpStorm.php:41