Skip to content

Commit d1d1254

Browse files
committed
feat: enhance VendorFileMapper with area emulation for compatibility
1 parent e567ce9 commit d1d1254

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

src/Service/VendorFileMapper.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88
use Magento\Framework\Component\ComponentRegistrarInterface;
99
use Magento\Framework\Filesystem\DirectoryList;
1010
use RuntimeException;
11+
use Magento\Framework\App\Area;
1112

1213
class VendorFileMapper
1314
{
1415
/**
1516
* @param ComponentRegistrarInterface $componentRegistrar
1617
* @param DirectoryList $directoryList
1718
* @param \Magento\Framework\ObjectManagerInterface $objectManager
19+
* @param \Magento\Framework\App\State $appState
1820
*/
1921
public function __construct(
2022
private readonly ComponentRegistrarInterface $componentRegistrar,
2123
private readonly DirectoryList $directoryList,
22-
private readonly \Magento\Framework\ObjectManagerInterface $objectManager
24+
private readonly \Magento\Framework\ObjectManagerInterface $objectManager,
25+
private readonly \Magento\Framework\App\State $appState
2326
) {
2427
}
2528

@@ -61,15 +64,20 @@ public function mapToThemePath(string $sourcePath, string $themePath, ?string $t
6164
// Priority 1A: Check if this is a Hyva Compatibility Module
6265
// If so, map to its registered "original_module"
6366
$originalModule = $this->getOriginalModuleFromCompatRegistry($moduleName);
67+
6468
if ($originalModule) {
6569
// Start with clean path (e.g. templates/Original_Module/foo.phtml or templates/foo.phtml)
6670
$targetPath = ltrim($cleanPath, '/');
6771

6872
// If path contains the Original Module name as a subdirectory (Hyva convention), strip it
6973
// Example: templates/Mollie_Payment/foo.phtml -> templates/foo.phtml
7074
// This prevents Theme/Mollie_Payment/templates/Mollie_Payment/foo.phtml
75+
// Note: Check both strict and case-insensitive to be safe
7176
if (str_contains($targetPath, '/' . $originalModule . '/')) {
7277
$targetPath = str_replace('/' . $originalModule . '/', '/', $targetPath);
78+
} elseif (stripos($targetPath, '/' . $originalModule . '/') !== false) {
79+
// Case-insensitive replacement if strict failed
80+
$targetPath = str_ireplace('/' . $originalModule . '/', '/', $targetPath);
7381
}
7482

7583
return rtrim($themePath, '/') . '/' . $originalModule . '/' . $targetPath;
@@ -203,24 +211,39 @@ private function isAreaCompatible(string $sourceArea, string $targetArea): bool
203211
private function getOriginalModuleFromCompatRegistry(string $compatModuleName): ?string
204212
{
205213
// Check if Hyva Compat Registry class exists (soft dependency)
206-
// Note: class_exists takes a fully qualified class name string.
207214
if (!class_exists('\Hyva\CompatModuleFallback\Model\CompatModuleRegistry')) {
208215
return null;
209216
}
210217

211218
try {
212-
/** @var \Hyva\CompatModuleFallback\Model\CompatModuleRegistry $registry */
213-
$registry = $this->objectManager->get('\Hyva\CompatModuleFallback\Model\CompatModuleRegistry');
219+
// Emulate frontend area to load proper DI configuration for CompatModuleRegistry
220+
// as CLI commands run in global scope where frontend/di.xml is ignored.
221+
$registry = $this->appState->emulateAreaCode(
222+
Area::AREA_FRONTEND,
223+
function () {
224+
// Use create() to ensure we get a fresh instance with the emulated configuration.
225+
// get() might return a cached instance from global scope (empty).
226+
return $this->objectManager->create('\Hyva\CompatModuleFallback\Model\CompatModuleRegistry');
227+
}
228+
);
214229

230+
/** @var \Hyva\CompatModuleFallback\Model\CompatModuleRegistry $registry */
215231
// Iterate through original modules to find if current module is a registered compat module
216232
foreach ($registry->getOrigModules() as $originalModule) {
217233
// Get compat modules for this original module
218234
$compatModules = $registry->getCompatModulesFor($originalModule);
219235

220-
// If our module is in the list, return the original module
236+
// Check exact match first
221237
if (in_array($compatModuleName, $compatModules, true)) {
222238
return $originalModule;
223239
}
240+
241+
// Fallback: Case-insensitive check (some modules handle naming inconsistently)
242+
foreach ($compatModules as $compatModule) {
243+
if (strnatcasecmp($compatModuleName, $compatModule) === 0) {
244+
return $originalModule;
245+
}
246+
}
224247
}
225248
} catch (\Throwable $e) {
226249
// If anything fails (e.g. DI config issues), fallback to standard mapping

0 commit comments

Comments
 (0)