Skip to content

Commit

Permalink
Add support for paths outside Magento root
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Nov 18, 2024
1 parent 3fbcf05 commit 2a95d52
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions Observer/RegisterModuleForHyvaConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Yireo\HyvaThemeAutoRegistration\Observer;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
Expand All @@ -18,12 +19,13 @@ class RegisterModuleForHyvaConfig implements ObserverInterface
*/
public function __construct(
private ComponentRegistrar $componentRegistrar,
private DirectoryList $directoryList,
private ModuleList $moduleList,
private HyvaFiles $hyvaFiles,
private array $moduleNames = [],
private array $modulePrefixes = [],
)
{}
) {
}

/**
* @param Observer $event
Expand All @@ -36,10 +38,8 @@ public function execute(Observer $observer)
$config = $event->getData('config');
$extensions = $config->hasData('extensions') ? $config->getData('extensions') : [];


foreach ($this->moduleNames as $moduleName) {
$path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
$extensions[] = ['src' => str_replace(BP, '', $path)];
$this->addModule($moduleName, $extensions);
}

foreach ($this->moduleList->getAll() as $moduleData) {
Expand All @@ -52,8 +52,7 @@ public function execute(Observer $observer)
continue;
}

$path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
$extensions[] = ['src' => str_replace(BP, '', $path)];
$this->addModule($moduleName, $extensions);
}

$config->setData('extensions', $extensions);
Expand All @@ -69,4 +68,36 @@ private function allowModuleName(string $moduleName): bool

return false;
}

private function getModulePath(string $moduleName): string
{
$path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
if (false === strstr($this->directoryList->getRoot(), $path)) {
return $path;
}

return trim(str_replace(BP, '', $path), '/');
}

private function isAlreadyDefined(string $path, array $extensions = []): bool
{
foreach ($extensions as $extension) {
if ($extension['src'] === $path) {
return true;
}
}

return false;
}

private function addModule(string $moduleName, array &$extensions = [])
{

$modulePath = $this->getModulePath($moduleName);
if ($this->isAlreadyDefined($modulePath, $extensions)) {
return;
}

$extensions[] = ['src' => $modulePath];
}
}

0 comments on commit 2a95d52

Please sign in to comment.