From 9130952637078c7f961f3ac295b0dc352cbd0647 Mon Sep 17 00:00:00 2001 From: Jisse Reitsma Date: Fri, 11 Oct 2024 09:30:24 +0200 Subject: [PATCH] Add MAGENTO_MODULE_FOLDER --- README.md | 19 +++++++--- Utilities/DisableModules.php | 69 +++++++++++++++++++++++++++--------- 2 files changed, 66 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 99feb50..e93867a 100644 --- a/README.md +++ b/README.md @@ -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: @@ -133,4 +142,4 @@ $ bin/magento integration_tests:check | Redis port | 6379 | | Redis reachable | Yes | +--------------------+--------------------+ -``` \ No newline at end of file +``` diff --git a/Utilities/DisableModules.php b/Utilities/DisableModules.php index f9191c3..3dcd6ab 100644 --- a/Utilities/DisableModules.php +++ b/Utilities/DisableModules.php @@ -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") * @@ -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 * @@ -175,8 +225,6 @@ public function getModulesFromConfig(): array return $config['modules']; } - - /** * Check if a given module is enabled in this DisableModules configuration * @@ -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); } @@ -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