Skip to content

Commit

Permalink
MAGETWO-42196: Magento components loaded from vendor directory - XSD …
Browse files Browse the repository at this point in the history
…filepath resolution

- Implemented URN resolver
  • Loading branch information
Maksym Savich committed Sep 8, 2015
1 parent 6f1ad53 commit 6379732
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/internal/Magento/Framework/Config/Dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class Dom
*/
protected $_rootNamespace;

/**
Magento\Framework\Config\Dom\UrnResolver
*/
private static $urnResolver;

/**
* Build DOM with initial XML contents and specifying identifier attributes for merging
*
Expand Down Expand Up @@ -260,6 +265,11 @@ public static function validateDomDocument(
$schemaFileName,
$errorFormat = self::ERROR_FORMAT_DEFAULT
) {
echo "schema: " . $schemaFileName . "\n";
if (!self::$urnResolver) {
self::$urnResolver = new Dom\UrnResolver();
}
$schemaFileName = self::$urnResolver->getRealPath($schemaFileName);
libxml_use_internal_errors(true);
try {
$result = $dom->schemaValidate($schemaFileName);
Expand Down Expand Up @@ -299,7 +309,19 @@ private static function _renderErrorMessage(\LibXMLError $errorInfo, $format)
$result = str_replace($placeholder, $value, $result);
}
if (strpos($result, '%') !== false) {
throw new \InvalidArgumentException("Error format '{$format}' contains unsupported placeholders.");
if (preg_match_all('/%.+%/', $format, $matches)) {
$unsupported = [];
foreach ($matches[0] as $placeholder) {
if (strpos($result, $placeholder)) {
$unsupported[] = $placeholder;
}
}
if (!empty($unsupported)) {
throw new \InvalidArgumentException(
"Error format '{$format}' contains unsupported placeholders: " . join(', ', $unsupported)
);
}
}
}
return $result;
}
Expand Down
65 changes: 65 additions & 0 deletions lib/internal/Magento/Framework/Config/Dom/UrnResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

/**
* Resolve URN path to a real schema path
*/
namespace Magento\Framework\Config\Dom;

class UrnResolver
{
/**
* @var \Magento\Framework\Component\ModuleRegistrar
*/
private static $moduleRegistrar;

/**
* @var \Magento\Framework\Component\LibraryRegistrar
*/
private static $libraryRegistrar;

/**
* Get real file path by it's URN reference
*
* @param $schemaFileName
* @return string
*/
public static function getRealPath($schemaFileName)
{
if (substr($schemaFileName, 0, 4) == 'urn:') {
if (!self::$moduleRegistrar) {
self::$moduleRegistrar = new \Magento\Framework\Component\ModuleRegistrar();
}
if (!self::$libraryRegistrar) {
self::$libraryRegistrar = new \Magento\Framework\Component\LibraryRegistrar();
}
// resolve schema location
// urn:magento:module:catalog:etc/catalog_attributes.xsd
// 0 : urn, 1: magento, 2: module, 3: catalog, 4: etc/catalog_attributes.xsd
// BP/app/code/Magento/Catalog/etc/catalog_attributes.xsd
// BP/vendor/magento/module-catalog/etc/catalog_attributes.xsd
$urnParts = explode(':', $schemaFileName);
if ($urnParts[2] == 'module') {
$appModulePath = str_replace(' ', '', ucwords(str_replace('-', ' ', $urnParts[3])));
$moduleName = ucfirst($urnParts[1]) . '_' . $appModulePath;
$appSchemaPath = self::$moduleRegistrar->getPath($moduleName) . '/' . $urnParts[4];
} else if ($urnParts[2] == 'library') {
// urn:magento:library:framework:Module/etc/module.xsd
// 0: urn, 1: magento, 2:library, 3: framework, 4: Module/etc/module.xsd
$moduleName = $urnParts[1] . '/' . $urnParts[3];
$appSchemaPath = self::$libraryRegistrar->getPath($moduleName) . '/' . $urnParts[4];
} else {
throw new \UnexpectedValueException("Unsupported format of schema location: " . $schemaFileName);
}
if (!empty($appSchemaPath) && file_exists($appSchemaPath)) {
$schemaFileName = $appSchemaPath;
} else {
throw new \UnexpectedValueException("Could not locate schema: " . $schemaFileName);
}
}
return $schemaFileName;
}
}

0 comments on commit 6379732

Please sign in to comment.