Skip to content

Commit

Permalink
Add MAGENTO_MODULE_FOLDER
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Oct 11, 2024
1 parent fa0f257 commit 9130952
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 22 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,21 @@ return (new InstallConfig())
->get();
```

Instead of using a hard-coded value, you might also want to set an environment variable `MAGENTO_MODULE` - for instance, in the **Run** configuration in PHPStorm - which is then reused via the method `enableByMagentoModuleEnv`. This way, you can keep the same `install-config-mysql.php` file while reusing it for various **Run** configurations:
Instead of using a hard-coded value, you might also want to set an environment variable `MAGENTO_MODULE` - for instance, in the **Run** configuration in PHPStorm. This way, you can keep the same `install-config-mysql.php` file while reusing it for various **Run** configurations:

```php
$disableModules->disableAll()
->enableMagento()
->enableByMagentoModuleEnv();
MAGENTO_MODULE=Yireo_Example
```

Note that if your module has dependencies, they need to be added to the same environment as well:

```php
MAGENTO_MODULE=Yireo_Example,Yireo_Foobar
```

If you have a lot of requirements, you can also use the `MAGENTO_MODULE_FOLDER` variable instead, which parses your own `etc/module.xml` and adds all declared modules to the whitelist:
```php
MAGENTO_MODULE_FOLDER=app/code/Yireo/Example
```

Another example, all the Magento modules are enabled by default. But then MSI and GraphQL are disabled again, while all Yireo modules are enabled:
Expand Down Expand Up @@ -133,4 +142,4 @@ $ bin/magento integration_tests:check
| Redis port | 6379 |
| Redis reachable | Yes |
+--------------------+--------------------+
```
```
69 changes: 52 additions & 17 deletions Utilities/DisableModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,40 @@ public function enableByMagentoModuleEnv(): DisableModules
return $this;
}

/**
* Enable a specific module by looking up the environment variable MAGENTO_MODULE
*
* @return $this
*/
public function enableByMagentoModuleFolderEnv(): DisableModules
{
$moduleFolder = $this->getVariable('MAGENTO_MODULE_FOLDER');
if (empty($moduleFolder)) {
return $this;
}

$fullModuleFolder = $this->applicationRoot.'/'.$moduleFolder;
if (false === is_dir($fullModuleFolder)) {
return $this;
}

if (false === is_file($fullModuleFolder.'/etc/module.xml')) {
return $this;
}

$moduleXml = file_get_contents($fullModuleFolder.'/etc/module.xml');
$xml = simplexml_load_string($moduleXml, "SimpleXMLElement");
$this->enableByName((string)$xml->module['name']);

if ($xml->module->sequence) {
foreach ($xml->module->sequence->module as $sequence) {
$this->enableByName((string)$sequence['name']);
}
}

return $this;
}

/**
* Enable a specific module by its name (like "Foo_Bar")
*
Expand Down Expand Up @@ -158,6 +192,22 @@ public function disableGraphQl(): DisableModules
return $this->disableByPattern('GraphQl');
}

/**
* Include all modules that are disabled in the global configuration
*
* @return $this
*/
public function disableDisabledAnyway(): DisableModules
{
foreach ($this->existingModules as $moduleName => $moduleStatus) {
if ($moduleStatus === 0) {
$this->disableModules[] = $moduleName;
}
}

return $this;
}

/**
* Get all modules from the configuration
*
Expand All @@ -175,8 +225,6 @@ public function getModulesFromConfig(): array
return $config['modules'];
}



/**
* Check if a given module is enabled in this DisableModules configuration
*
Expand All @@ -199,7 +247,8 @@ public function isModuleEnabled(string $moduleName): bool
*/
public function get(): array
{
$this->disableDisabledAnyway();
$this->enableByMagentoModuleEnv();
$this->enableByMagentoModuleFolderEnv();
sort($this->disableModules);
return array_unique($this->disableModules);
}
Expand Down Expand Up @@ -234,21 +283,7 @@ private function setApplicationRoot(string $applicationRoot)

$this->applicationRoot = $applicationRoot;
}
/**
* Include all modules that are disabled in the global configuration
*
* @return $this
*/
private function disableDisabledAnyway(): DisableModules
{
foreach ($this->existingModules as $moduleName => $moduleStatus) {
if ($moduleStatus === 0) {
$this->disableModules[] = $moduleName;
}
}

return $this;
}

/**
* @param string $variableName
Expand Down

0 comments on commit 9130952

Please sign in to comment.