UrnResolver.php 3.1 KB

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