diff --git a/app/code/Magento/Developer/Model/XmlCatalog/Format/PhpStorm.php b/app/code/Magento/Developer/Model/XmlCatalog/Format/PhpStorm.php index 30684b7177c99..1ced906cce763 100644 --- a/app/code/Magento/Developer/Model/XmlCatalog/Format/PhpStorm.php +++ b/app/code/Magento/Developer/Model/XmlCatalog/Format/PhpStorm.php @@ -6,11 +6,12 @@ namespace Magento\Developer\Model\XmlCatalog\Format; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\DomDocument\DomDocumentFactory; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Filesystem\Directory\ReadFactory; use Magento\Framework\Filesystem\Directory\ReadInterface; -use Magento\Framework\Filesystem\Directory\WriteFactory; -use Magento\Framework\Filesystem\Directory\WriteInterface; +use Magento\Framework\Filesystem\File\WriteFactory; /** * Class PhpStorm generates URN catalog for PhpStorm 9 @@ -23,20 +24,28 @@ class PhpStorm implements FormatInterface private $currentDirRead; /** - * @var \Magento\Framework\Filesystem\File\WriteFactory + * @var WriteFactory */ private $fileWriteFactory; + /** + * @var DomDocumentFactory + */ + private $domDocumentFactory; + /** * @param ReadFactory $readFactory - * @param \Magento\Framework\Filesystem\File\WriteFactory $fileWriteFactory + * @param WriteFactory $fileWriteFactory + * @param DomDocumentFactory $domDocumentFactory */ public function __construct( ReadFactory $readFactory, - \Magento\Framework\Filesystem\File\WriteFactory $fileWriteFactory + WriteFactory $fileWriteFactory, + DomDocumentFactory $domDocumentFactory = null ) { $this->currentDirRead = $readFactory->create(getcwd()); $this->fileWriteFactory = $fileWriteFactory; + $this->domDocumentFactory = $domDocumentFactory ?: ObjectManager::getInstance()->get(DomDocumentFactory::class); } /** @@ -57,26 +66,21 @@ public function generateCatalog(array $dictionary, $configFilePath) \Magento\Framework\Filesystem\DriverPool::FILE, 'r' ); - $dom = new \DOMDocument(); - $dom->loadXML($file->readAll()); + $dom = $this->domDocumentFactory->create(); + $fileContent = $file->readAll(); + if (!empty($fileContent)) { + $dom->loadXML($fileContent); + } else { + $this->initEmptyFile($dom); + } $xpath = new \DOMXPath($dom); $nodeList = $xpath->query('/project'); $projectNode = $nodeList->item(0); $file->close(); } catch (FileSystemException $f) { //create file if does not exists - $dom = new \DOMDocument(); - $projectNode = $dom->createElement('project'); - - //PhpStorm 9 version for component is "4" - $projectNode->setAttribute('version', '4'); - $dom->appendChild($projectNode); - $rootComponentNode = $dom->createElement('component'); - - //PhpStorm 9 version for ProjectRootManager is "2" - $rootComponentNode->setAttribute('version', '2'); - $rootComponentNode->setAttribute('name', 'ProjectRootManager'); - $projectNode->appendChild($rootComponentNode); + $dom = $this->domDocumentFactory->create(); + $projectNode = $this->initEmptyFile($dom); } $xpath = new \DOMXPath($dom); @@ -103,4 +107,26 @@ public function generateCatalog(array $dictionary, $configFilePath) $file->write($dom->saveXML()); $file->close(); } + + /** + * Setup basic empty dom elements + * + * @param \DOMDocument $dom + * @return \DOMElement + */ + private function initEmptyFile(\DOMDocument $dom) + { + $projectNode = $dom->createElement('project'); + + //PhpStorm 9 version for component is "4" + $projectNode->setAttribute('version', '4'); + $dom->appendChild($projectNode); + $rootComponentNode = $dom->createElement('component'); + + //PhpStorm 9 version for ProjectRootManager is "2" + $rootComponentNode->setAttribute('version', '2'); + $rootComponentNode->setAttribute('name', 'ProjectRootManager'); + $projectNode->appendChild($rootComponentNode); + return $projectNode; + } } diff --git a/lib/internal/Magento/Framework/DomDocument/DomDocumentFactory.php b/lib/internal/Magento/Framework/DomDocument/DomDocumentFactory.php index 677e4d654e52c..d2e16700bc904 100644 --- a/lib/internal/Magento/Framework/DomDocument/DomDocumentFactory.php +++ b/lib/internal/Magento/Framework/DomDocument/DomDocumentFactory.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Framework\DomDocument; /** diff --git a/lib/internal/Magento/Framework/Test/Unit/DomDocument/DomDocumentFactoryTest.php b/lib/internal/Magento/Framework/Test/Unit/DomDocument/DomDocumentFactoryTest.php new file mode 100644 index 0000000000000..e3ea74339e043 --- /dev/null +++ b/lib/internal/Magento/Framework/Test/Unit/DomDocument/DomDocumentFactoryTest.php @@ -0,0 +1,21 @@ +assertInstanceOf( + \DOMDocument::class, + $domDocumentFactory->create() + ); + } +}