Skip to content

Commit fede586

Browse files
author
Eugene Tulika
committed
MAGETWO-44440: Register URN resolver to resolve internal dependencies
- Fixed CR comments
1 parent 61df06a commit fede586

File tree

6 files changed

+47
-51
lines changed

6 files changed

+47
-51
lines changed

app/code/Magento/Developer/Console/Command/XmlCatalogGenerateCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ protected function configure()
114114
/**
115115
* Get an array of URNs
116116
*
117+
* @param OutputInterface $output
117118
* @return array
118119
*/
119-
private function getUrnDictionary()
120+
private function getUrnDictionary(OutputInterface $output)
120121
{
121122
$files = $this->filesUtility->getXmlCatalogFiles('*.xml');
122123
$files = array_merge($files, $this->filesUtility->getXmlCatalogFiles('*.xsd'));
@@ -139,6 +140,9 @@ private function getUrnDictionary()
139140
$paths[$urn] = $this->urnResolver->getRealPath($urn);
140141
} catch (\Exception $e) {
141142
// don't add unsupported element to array
143+
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
144+
$output->writeln($e->getMessage());
145+
}
142146
}
143147
}
144148
return $paths;
@@ -154,7 +158,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
154158
$ideFilePath = $input->getArgument(self::IDE_FILE_PATH_ARGUMENT);
155159
$absolutePath = $this->currentDirRead->getAbsolutePath($ideFilePath);
156160

157-
$urnDictionary = $this->getUrnDictionary();
161+
$urnDictionary = $this->getUrnDictionary($output);
158162
if ($formatter = $this->getFormatters($ideName)) {
159163
$formatter->generateCatalog($urnDictionary, $absolutePath);
160164
} else {

app/code/Magento/Developer/Model/XmlCatalog/Format/FormatInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ interface FormatInterface
1515
* Generate Catalog of URNs
1616
*
1717
* @param string[] $dictionary
18-
* @param string $path
18+
* @param string $configFile absolute path to the file to write the catalog
1919
* @return void
2020
*/
21-
public function generateCatalog(array $dictionary, $path);
21+
public function generateCatalog(array $dictionary, $configFile);
2222
}

app/code/Magento/Developer/Model/XmlCatalog/Format/PhpStorm.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\Framework\Filesystem\Directory\WriteInterface;
1313

1414
/**
15-
* Class PhpStormNine generates URN catalog for PhpStorm 9
15+
* Class PhpStorm generates URN catalog for PhpStorm 9
1616
*/
1717
class PhpStorm implements FormatInterface
1818
{
@@ -42,17 +42,17 @@ public function __construct(
4242
* Generate Catalog of URNs for the PhpStorm 9
4343
*
4444
* @param string[] $dictionary
45-
* @param string $path
45+
* @param string $configFile absolute path to the PhpStorm misc.xml
4646
* @return void
4747
*/
48-
public function generateCatalog(array $dictionary, $path)
48+
public function generateCatalog(array $dictionary, $configFile)
4949
{
5050
$componentNode = null;
5151
$projectNode = null;
52-
$path = $this->currentDirRead->getRelativePath($path);
53-
if ($this->currentDirRead->isExist($path) && $this->currentDirRead->isFile($path)) {
52+
$configFile = $this->currentDirRead->getRelativePath($configFile);
53+
if ($this->currentDirRead->isExist($configFile) && $this->currentDirRead->isFile($configFile)) {
5454
$dom = new \DOMDocument();
55-
$dom->load($path);
55+
$dom->load($configFile);
5656
$xpath = new \DOMXPath($dom);
5757
$nodeList = $xpath->query('/project');
5858
$projectNode = $nodeList->item(0);
@@ -87,6 +87,6 @@ public function generateCatalog(array $dictionary, $path)
8787
$componentNode->appendChild($node);
8888
}
8989
$dom->formatOutput = true;
90-
$this->currentDirWrite->writeFile($path, $dom->saveXML());
90+
$this->currentDirWrite->writeFile($configFile, $dom->saveXML());
9191
}
9292
}

app/code/Magento/Developer/Test/Unit/Console/Command/XmlCatalogGenerateCommandTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class XmlCatalogGenerateCommandTest extends \PHPUnit_Framework_TestCase
1616
*/
1717
private $command;
1818

19-
public function setUp()
19+
public function testExecuteBadType()
2020
{
2121
$fixtureXmlFile = __DIR__ . '/_files/test.xml';
2222

@@ -75,10 +75,7 @@ public function setUp()
7575
$readDirFactory,
7676
$formats
7777
);
78-
}
7978

80-
public function testExecuteBadType()
81-
{
8279
$commandTester = new CommandTester($this->command);
8380
$commandTester->execute([XmlCatalogGenerateCommand::IDE_FILE_PATH_ARGUMENT => 'test']);
8481
$this->assertEquals('', $commandTester->getDisplay());

lib/internal/Magento/Framework/Config/Dom/UrnResolver.php

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Magento\Framework\Component\ComponentRegistrar;
1313
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Exception\NotFoundException;
1415
use Magento\Framework\Phrase;
1516

1617
class UrnResolver
@@ -20,43 +21,37 @@ class UrnResolver
2021
*
2122
* @param string $schema
2223
* @return string
23-
* @throws \UnexpectedValueException
24+
* @throws NotFoundException
2425
*/
2526
public function getRealPath($schema)
2627
{
28+
if (strpos($schema, 'urn:') !== 0) {
29+
return $schema;
30+
}
31+
2732
$componentRegistrar = new ComponentRegistrar();
28-
if (substr($schema, 0, 4) == 'urn:') {
29-
// resolve schema location
30-
$urnParts = explode(':', $schema);
31-
if ($urnParts[2] == 'module') {
32-
// urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd
33-
// 0 : urn, 1: magento, 2: module, 3: Magento_Catalog, 4: etc/catalog_attributes.xsd
34-
// moduleName -> Magento_Catalog
35-
$schemaPath = $componentRegistrar->getPath(
36-
ComponentRegistrar::MODULE,
37-
$urnParts[3]
38-
) . '/' . $urnParts[4];
39-
} else if (strpos($urnParts[2], 'framework') === 0) {
40-
// urn:magento:framework:Module/etc/module.xsd
41-
// 0: urn, 1: magento, 2: framework, 3: Module/etc/module.xsd
42-
// libaryName -> magento/framework
43-
$libraryName = $urnParts[1] . '/' . $urnParts[2];
44-
$schemaPath = $componentRegistrar->getPath(
45-
ComponentRegistrar::LIBRARY,
46-
$libraryName
47-
) . '/' . $urnParts[3];
48-
} else {
49-
throw new \UnexpectedValueException("Unsupported format of schema location: " . $schema);
50-
}
51-
if (!empty($schemaPath) && file_exists($schemaPath)) {
52-
$schema = $schemaPath;
53-
} else {
54-
throw new \UnexpectedValueException(
55-
"Could not locate schema: '" . $schema . "' at '" . $schemaPath . "'"
56-
);
57-
}
33+
$matches = [];
34+
$modulePattern = '/urn:(?<vendor>([a-zA-Z]*)):module:(?<module>([A-Za-z\_]*)):(?<path>(.+))/';
35+
$frameworkPattern = '/urn:(?<vendor>([a-zA-Z]*)):(?<framework>(framework[A-Za-z\-]*)):(?<path>(.+))/';
36+
if (preg_match($modulePattern, $schema, $matches)) {
37+
//urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd
38+
$package = $componentRegistrar
39+
->getPath(ComponentRegistrar::MODULE, $matches['module']);
40+
} else if (preg_match($frameworkPattern, $schema, $matches)) {
41+
//urn:magento:framework:Module/etc/module.xsd
42+
//urn:magento:framework-amqp:Module/etc/module.xsd
43+
$package = $componentRegistrar
44+
->getPath(ComponentRegistrar::LIBRARY, $matches['vendor'] . '/' . $matches['framework']);
45+
} else {
46+
throw new NotFoundException(__("Unsupported format of schema location: '%1'", $schema));
47+
}
48+
$schemaPath = $package . '/' . $matches['path'];
49+
if (empty($package) || !file_exists($schemaPath)) {
50+
throw new NotFoundException(
51+
__("Could not locate schema: '%1' at '%2'", $schema, $schemaPath)
52+
);
5853
}
59-
return $schema;
54+
return $schemaPath;
6055
}
6156

6257
/**

lib/internal/Magento/Framework/Config/Test/Unit/Dom/UrnResolverTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Framework\Config\Test\Unit\Dom;
77

8-
use \Magento\Framework\Config\Dom\UrnResolver;
8+
use Magento\Framework\Config\Dom\UrnResolver;
99
use Magento\Framework\Component\ComponentRegistrar;
1010

1111
class UrnResolverTest extends \PHPUnit_Framework_TestCase
@@ -53,8 +53,8 @@ public function testGetRealPathWithModuleUrn()
5353
}
5454

5555
/**
56-
* @expectedException \UnexpectedValueException
57-
* @expectedExceptionMessage Unsupported format of schema location: urn:magento:test:test:etc/test_test.xsd
56+
* @expectedException \Magento\Framework\Exception\NotFoundException
57+
* @expectedExceptionMessage Unsupported format of schema location: 'urn:magento:test:test:etc/test_test.xsd'
5858
*/
5959
public function testGetRealPathWrongSection()
6060
{
@@ -63,7 +63,7 @@ public function testGetRealPathWrongSection()
6363
}
6464

6565
/**
66-
* @expectedException \UnexpectedValueException
66+
* @expectedException \Magento\Framework\Exception\NotFoundException
6767
* @expectedExceptionMessage Could not locate schema: 'urn:magento:module:Magento_Test:test.xsd' at '/test.xsd'
6868
*/
6969
public function testGetRealPathWrongModule()

0 commit comments

Comments
 (0)