ThemePackageInfo.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Theme;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. use Magento\Framework\Filesystem\Directory\ReadFactory;
  9. /**
  10. * Maps package name to full theme path, and vice versa
  11. */
  12. class ThemePackageInfo
  13. {
  14. /**
  15. * @var ComponentRegistrar
  16. */
  17. private $componentRegistrar;
  18. /**
  19. * @var ReadFactory
  20. */
  21. private $readDirFactory;
  22. /**
  23. * @var array
  24. */
  25. private $packageNameToFullPathMap = [];
  26. /**
  27. * @var \Magento\Framework\Serialize\Serializer\Json
  28. */
  29. private $serializer;
  30. /**
  31. * Initialize dependencies.
  32. *
  33. * @param ComponentRegistrar $componentRegistrar
  34. * @param ReadFactory $readDirFactory
  35. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  36. */
  37. public function __construct(
  38. ComponentRegistrar $componentRegistrar,
  39. ReadFactory $readDirFactory,
  40. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  41. ) {
  42. $this->componentRegistrar = $componentRegistrar;
  43. $this->readDirFactory = $readDirFactory;
  44. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  45. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  46. }
  47. /**
  48. * Get package name of a theme by its full theme path
  49. *
  50. * @param string $themePath
  51. * @return string
  52. */
  53. public function getPackageName($themePath)
  54. {
  55. $themePath = $this->componentRegistrar->getPath(ComponentRegistrar::THEME, $themePath);
  56. $themeDir = $this->readDirFactory->create($themePath);
  57. if ($themeDir->isExist('composer.json')) {
  58. $rawData = [];
  59. $themeFile = $themeDir->readFile('composer.json');
  60. if ($themeFile) {
  61. $rawData = $this->serializer->unserialize($themeFile);
  62. }
  63. return isset($rawData['name']) ? $rawData['name'] : '';
  64. }
  65. return '';
  66. }
  67. /**
  68. * Get full theme path by its package name
  69. *
  70. * @param string $packageName
  71. * @return string
  72. */
  73. public function getFullThemePath($packageName)
  74. {
  75. if (empty($this->packageNameToFullPathMap)) {
  76. $this->initializeMap();
  77. }
  78. return isset($this->packageNameToFullPathMap[$packageName])
  79. ? $this->packageNameToFullPathMap[$packageName] : '';
  80. }
  81. /**
  82. * Initialize package name to full theme path map
  83. *
  84. * @return void
  85. */
  86. private function initializeMap()
  87. {
  88. $themePaths = $this->componentRegistrar->getPaths(ComponentRegistrar::THEME);
  89. /** @var \Magento\Theme\Model\Theme $theme */
  90. foreach ($themePaths as $fullThemePath => $themeDir) {
  91. $themeDirRead = $this->readDirFactory->create($themeDir);
  92. if ($themeDirRead->isExist('composer.json')) {
  93. $rawData = $this->serializer->unserialize($themeDirRead->readFile('composer.json'));
  94. if (isset($rawData['name'])) {
  95. $this->packageNameToFullPathMap[$rawData['name']] = $fullThemePath;
  96. }
  97. }
  98. }
  99. }
  100. }