Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ThemePackageInfo.php
Go to the documentation of this file.
1 <?php
7 
10 
15 {
19  private $componentRegistrar;
20 
24  private $readDirFactory;
25 
29  private $packageNameToFullPathMap = [];
30 
34  private $serializer;
35 
43  public function __construct(
44  ComponentRegistrar $componentRegistrar,
45  ReadFactory $readDirFactory,
46  \Magento\Framework\Serialize\Serializer\Json $serializer = null
47  ) {
48  $this->componentRegistrar = $componentRegistrar;
49  $this->readDirFactory = $readDirFactory;
50  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
51  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
52  }
53 
60  public function getPackageName($themePath)
61  {
62  $themePath = $this->componentRegistrar->getPath(ComponentRegistrar::THEME, $themePath);
63  $themeDir = $this->readDirFactory->create($themePath);
64  if ($themeDir->isExist('composer.json')) {
65  $rawData = [];
66  $themeFile = $themeDir->readFile('composer.json');
67  if ($themeFile) {
68  $rawData = $this->serializer->unserialize($themeFile);
69  }
70  return isset($rawData['name']) ? $rawData['name'] : '';
71  }
72  return '';
73  }
74 
81  public function getFullThemePath($packageName)
82  {
83  if (empty($this->packageNameToFullPathMap)) {
84  $this->initializeMap();
85  }
86  return isset($this->packageNameToFullPathMap[$packageName])
87  ? $this->packageNameToFullPathMap[$packageName] : '';
88  }
89 
95  private function initializeMap()
96  {
97  $themePaths = $this->componentRegistrar->getPaths(ComponentRegistrar::THEME);
99  foreach ($themePaths as $fullThemePath => $themeDir) {
100  $themeDirRead = $this->readDirFactory->create($themeDir);
101  if ($themeDirRead->isExist('composer.json')) {
102  $rawData = $this->serializer->unserialize($themeDirRead->readFile('composer.json'));
103  if (isset($rawData['name'])) {
104  $this->packageNameToFullPathMap[$rawData['name']] = $fullThemePath;
105  }
106  }
107  }
108  }
109 }
__construct(ComponentRegistrar $componentRegistrar, ReadFactory $readDirFactory, \Magento\Framework\Serialize\Serializer\Json $serializer=null)
$componentRegistrar
Definition: bootstrap.php:23