Theme.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Theme configuration files handler
  8. */
  9. namespace Magento\Framework\Config;
  10. /**
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Theme
  15. {
  16. /**
  17. * Is used for separation path of themes
  18. */
  19. const THEME_PATH_SEPARATOR = '/';
  20. /**
  21. * Data extracted from the configuration file
  22. *
  23. * @var array
  24. */
  25. protected $_data;
  26. /**
  27. * @var \Magento\Framework\Config\Dom\UrnResolver
  28. */
  29. protected $urnResolver;
  30. /**
  31. * Constructor
  32. *
  33. * @param \Magento\Framework\Config\Dom\UrnResolver $urnResolver
  34. * @param string $configContent
  35. */
  36. public function __construct(
  37. \Magento\Framework\Config\Dom\UrnResolver $urnResolver,
  38. $configContent = null
  39. ) {
  40. $this->urnResolver = $urnResolver;
  41. $this->_data = $this->_extractData($configContent);
  42. }
  43. /**
  44. * Get absolute path to theme.xsd
  45. *
  46. * @return string
  47. */
  48. public function getSchemaFile()
  49. {
  50. return $this->urnResolver->getRealPath('urn:magento:framework:Config/etc/theme.xsd');
  51. }
  52. /**
  53. * Extract configuration data from theme.xml
  54. *
  55. * @param string $configContent
  56. * @return array
  57. * @SuppressWarnings(PHPMD.NPathComplexity)
  58. */
  59. protected function _extractData($configContent)
  60. {
  61. $data = [
  62. 'title' => null,
  63. 'media' => null,
  64. 'parent' => null,
  65. ];
  66. if (!empty($configContent)) {
  67. $dom = new \DOMDocument();
  68. $dom->loadXML($configContent);
  69. // todo: validation of the document
  70. /** @var $themeNode \DOMElement */
  71. $themeNode = $dom->getElementsByTagName('theme')->item(0);
  72. $themeTitleNode = $themeNode->getElementsByTagName('title')->item(0);
  73. $data['title'] = $themeTitleNode ? $themeTitleNode->nodeValue : null;
  74. /** @var $mediaNode \DOMElement */
  75. $mediaNode = $themeNode->getElementsByTagName('media')->item(0);
  76. $previewImage = $mediaNode ? $mediaNode->getElementsByTagName('preview_image')->item(0)->nodeValue : '';
  77. $data['media']['preview_image'] = $previewImage;
  78. $themeParentNode = $themeNode->getElementsByTagName('parent')->item(0);
  79. $data['parent'] = $themeParentNode ? $themeParentNode->nodeValue : null;
  80. }
  81. return $data;
  82. }
  83. /**
  84. * Get title for specified theme and package code
  85. *
  86. * @return string
  87. */
  88. public function getThemeTitle()
  89. {
  90. return $this->_data['title'];
  91. }
  92. /**
  93. * Get theme media data
  94. *
  95. * @return array
  96. */
  97. public function getMedia()
  98. {
  99. return $this->_data['media'];
  100. }
  101. /**
  102. * Retrieve a parent theme code
  103. *
  104. * @return array|null
  105. */
  106. public function getParentTheme()
  107. {
  108. $parentTheme = $this->_data['parent'];
  109. if (!$parentTheme) {
  110. return null;
  111. }
  112. return explode(self::THEME_PATH_SEPARATOR, $parentTheme);
  113. }
  114. }