1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Resolve URN path to a real schema path
*/
namespace Magento\Framework\Config\Dom;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Phrase;
/**
* @api
* @since 100.0.2
*/
class UrnResolver
{
/**
* Get real file path by it's URN reference
*
* @param string $schema
* @return string
* @throws NotFoundException
*/
public function getRealPath($schema)
{
if (strpos($schema, 'urn:') !== 0) {
return $schema;
}
$componentRegistrar = new ComponentRegistrar();
$matches = [];
$modulePattern = '/urn:(?<vendor>([a-zA-Z]*)):module:(?<module>([A-Za-z0-9\_]*)):(?<path>(.+))/';
$frameworkPattern = '/urn:(?<vendor>([a-zA-Z]*)):(?<framework>(framework[A-Za-z\-]*)):(?<path>(.+))/';
$setupPattern = '/urn:(?<vendor>([a-zA-Z]*)):(?<setup>(setup[A-Za-z\-]*)):(?<path>(.+))/';
if (preg_match($modulePattern, $schema, $matches)) {
//urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd
$package = $componentRegistrar
->getPath(ComponentRegistrar::MODULE, $matches['module']);
} elseif (preg_match($frameworkPattern, $schema, $matches)) {
//urn:magento:framework:Module/etc/module.xsd
//urn:magento:framework-amqp:Module/etc/module.xsd
$package = $componentRegistrar
->getPath(ComponentRegistrar::LIBRARY, $matches['vendor'] . '/' . $matches['framework']);
} elseif (preg_match($setupPattern, $schema, $matches)) {
//urn:magento:setup:
$package = $componentRegistrar
->getPath(ComponentRegistrar::SETUP, $matches['vendor'] . '/' . $matches['setup']);
} else {
throw new NotFoundException(new Phrase("Unsupported format of schema location: '%1'", [$schema]));
}
$schemaPath = $package . '/' . $matches['path'];
if (empty($package) || !file_exists($schemaPath)) {
throw new NotFoundException(
new Phrase("Could not locate schema: '%1' at '%2'", [$schema, $schemaPath])
);
}
return $schemaPath;
}
/**
* Callback registered for libxml to resolve URN to the file path
*
* @param string $public
* @param string $system
* @param array $context
* @return resource
* @throws LocalizedException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function registerEntityLoader($public, $system, $context)
{
if (strpos($system, 'urn:') === 0) {
$filePath = $this->getRealPath($system);
} else {
if (file_exists($system)) {
$filePath = $system;
} else {
throw new LocalizedException(new Phrase("File '%system' cannot be found", ['system' => $system]));
}
}
return fopen($filePath, "r");
}
}