From a6c1760622089e2f7be6f82eb6e15db543f251b3 Mon Sep 17 00:00:00 2001
From: ashraf <ashraf.boudawara@oxid-esales.com>
Date: Wed, 29 May 2024 10:01:11 +0200
Subject: [PATCH] OXDEV-7248 Remove env var oxid source directory

---
 source/Core/UtilsFile.php                      |  3 ++-
 source/Core/UtilsPic.php                       |  2 +-
 source/Internal/Container/services.yaml        |  1 -
 .../BootstrapConfigurationFactory.php          |  4 ----
 .../Dao/SystemConfigurationDao.php             |  1 -
 .../DataObject/SystemConfiguration.php         | 10 ----------
 .../Framework/DIContainer/ContainerBuilder.php |  1 +
 source/Internal/Framework/Env/DotenvLoader.php | 18 +++++-------------
 .../Framework/Env/DotenvLoaderInterface.php    |  1 -
 .../Transition/Utility/BasicContext.php        |  2 +-
 source/Setup/Utilities.php                     |  3 ++-
 .../Config/CodeceptionParametersProvider.php   |  4 +++-
 .../Dao/SystemConfigurationDaoTest.php         |  1 -
 .../Internal/Framework/Env/EnvLoaderTest.php   | 12 ------------
 tests/Unit/Internal/BasicContextStub.php       |  5 -----
 15 files changed, 15 insertions(+), 53 deletions(-)

diff --git a/source/Core/UtilsFile.php b/source/Core/UtilsFile.php
index eebad75bb55..a9162f4db35 100644
--- a/source/Core/UtilsFile.php
+++ b/source/Core/UtilsFile.php
@@ -671,9 +671,10 @@ private function addErrorMessageToDisplay($message): void
      */
     private function makePathRelativeToShopSource(string $path): string
     {
+
         return Path::makeRelative(
             $path,
-            (new BootstrapConfigurationFactory())->create()->getShopSourceDirectory()
+            ContainerFacade::getParameter('oxid_shop_source_directory')
         );
     }
 }
diff --git a/source/Core/UtilsPic.php b/source/Core/UtilsPic.php
index aa04ac05c3e..b68c4a6bce8 100644
--- a/source/Core/UtilsPic.php
+++ b/source/Core/UtilsPic.php
@@ -288,7 +288,7 @@ private function makePathRelativeToShopSource(string $path): string
     {
         return Path::makeRelative(
             $path,
-            (new BootstrapConfigurationFactory())->create()->getShopSourceDirectory()
+            ContainerFacade::getParameter('oxid_shop_source_directory')
         );
     }
 }
diff --git a/source/Internal/Container/services.yaml b/source/Internal/Container/services.yaml
index c673cb06f93..7cb18967d7a 100644
--- a/source/Internal/Container/services.yaml
+++ b/source/Internal/Container/services.yaml
@@ -2,7 +2,6 @@ imports:
     - { resource: bootstrap-services.yaml }
 
 parameters:
-    oxid_shop_source_directory: '%env(OXID_SHOP_SOURCE_DIRECTORY)%'
     oxid_cache_directory: '%env(OXID_CACHE_DIRECTORY)%'
     oxid_log_level: '%env(OXID_LOG_LEVEL)%'
 
diff --git a/source/Internal/Framework/Configuration/BootstrapConfigurationFactory.php b/source/Internal/Framework/Configuration/BootstrapConfigurationFactory.php
index 461210611c2..0a030c9dd6f 100644
--- a/source/Internal/Framework/Configuration/BootstrapConfigurationFactory.php
+++ b/source/Internal/Framework/Configuration/BootstrapConfigurationFactory.php
@@ -29,9 +29,5 @@ private function initEnvironment(): void
         $projectRootDirectory = (new BootstrapLocator())->getProjectRoot();
         $dotenvLoader = new DotenvLoader($projectRootDirectory);
         $dotenvLoader->loadEnvironmentVariables();
-        $dotenvLoader->putEnvironmentVariable(
-            'OXID_SHOP_SOURCE_DIRECTORY',
-            Path::join($projectRootDirectory, 'source')
-        );
     }
 }
diff --git a/source/Internal/Framework/Configuration/Dao/SystemConfigurationDao.php b/source/Internal/Framework/Configuration/Dao/SystemConfigurationDao.php
index d2f99c795f1..cf0bbf1ecd4 100644
--- a/source/Internal/Framework/Configuration/Dao/SystemConfigurationDao.php
+++ b/source/Internal/Framework/Configuration/Dao/SystemConfigurationDao.php
@@ -19,7 +19,6 @@ public function get(): SystemConfiguration
     {
         $systemConfiguration = new SystemConfiguration();
         $systemConfiguration->setDatabaseUrl(getenv('OXID_DB_URL'));
-        $systemConfiguration->setShopSourceDirectory(getenv('OXID_SHOP_SOURCE_DIRECTORY'));
         $systemConfiguration->setCacheDirectory(getenv('OXID_CACHE_DIRECTORY'));
         $systemConfiguration->setLogLevel(getenv('OXID_LOG_LEVEL'));
 
diff --git a/source/Internal/Framework/Configuration/DataObject/SystemConfiguration.php b/source/Internal/Framework/Configuration/DataObject/SystemConfiguration.php
index b155c51e0fa..9dabe3e3863 100644
--- a/source/Internal/Framework/Configuration/DataObject/SystemConfiguration.php
+++ b/source/Internal/Framework/Configuration/DataObject/SystemConfiguration.php
@@ -27,16 +27,6 @@ public function setDatabaseUrl(string $databaseUrl): void
         $this->databaseUrl = $databaseUrl;
     }
 
-    public function getShopSourceDirectory(): string
-    {
-        return $this->shopSourceDirectory;
-    }
-
-    public function setShopSourceDirectory(string $shopSourceDirectory): void
-    {
-        $this->shopSourceDirectory = $shopSourceDirectory;
-    }
-
     public function getCacheDirectory(): string
     {
         return $this->cacheDirectory;
diff --git a/source/Internal/Framework/DIContainer/ContainerBuilder.php b/source/Internal/Framework/DIContainer/ContainerBuilder.php
index 5e77edf54c5..24d03c7836d 100644
--- a/source/Internal/Framework/DIContainer/ContainerBuilder.php
+++ b/source/Internal/Framework/DIContainer/ContainerBuilder.php
@@ -41,6 +41,7 @@ public function getContainer(): SymfonyContainerBuilder
         $symfonyContainer = new SymfonyContainerBuilder();
         $symfonyContainer->addCompilerPass(new RegisterListenersPass());
         $symfonyContainer->addCompilerPass(new AddConsoleCommandPass());
+        $symfonyContainer->setParameter('oxid_shop_source_directory', $this->context->getSourcePath());
         $this->loadEditionServices($symfonyContainer);
         $this->loadModuleServices($symfonyContainer);
         $this->loadProjectServices($symfonyContainer);
diff --git a/source/Internal/Framework/Env/DotenvLoader.php b/source/Internal/Framework/Env/DotenvLoader.php
index 519aebc74b9..eddd539b3dd 100644
--- a/source/Internal/Framework/Env/DotenvLoader.php
+++ b/source/Internal/Framework/Env/DotenvLoader.php
@@ -23,20 +23,12 @@ public function __construct(private readonly string $pathToEnvFiles)
     }
 
     public function loadEnvironmentVariables(): void
-    {
-        $this->loadDotEnv()->loadEnv(Path::join($this->pathToEnvFiles, $this->envFile));
-    }
-
-    public function putEnvironmentVariable(string $name, string $value): void
-    {
-        $this->loadDotEnv()->populate([$name => $value]);
-    }
-
-    private function loadDotEnv(): Dotenv
     {
         $dotEnv = new Dotenv($this->envKey, $this->debugKey);
-        $dotEnv->usePutenv();
-
-        return $dotEnv;
+        $dotEnv
+            ->usePutenv()
+            ->loadEnv(
+                Path::join($this->pathToEnvFiles, $this->envFile)
+            );
     }
 }
diff --git a/source/Internal/Framework/Env/DotenvLoaderInterface.php b/source/Internal/Framework/Env/DotenvLoaderInterface.php
index 2f598902ab9..a5e948029b8 100644
--- a/source/Internal/Framework/Env/DotenvLoaderInterface.php
+++ b/source/Internal/Framework/Env/DotenvLoaderInterface.php
@@ -12,5 +12,4 @@
 interface DotenvLoaderInterface
 {
     public function loadEnvironmentVariables(): void;
-    public function putEnvironmentVariable(string $name, string $value): void;
 }
diff --git a/source/Internal/Transition/Utility/BasicContext.php b/source/Internal/Transition/Utility/BasicContext.php
index af18d914340..1a193492db6 100644
--- a/source/Internal/Transition/Utility/BasicContext.php
+++ b/source/Internal/Transition/Utility/BasicContext.php
@@ -83,7 +83,7 @@ public function getActiveModuleServicesFilePath(int $shopId): string
      */
     public function getSourcePath(): string
     {
-        return $this->systemConfiguration->getShopSourceDirectory();
+        return Path::join($this->getShopRootPath(), 'source');
     }
 
     /**
diff --git a/source/Setup/Utilities.php b/source/Setup/Utilities.php
index cff47bbfebc..5f85df7dd50 100644
--- a/source/Setup/Utilities.php
+++ b/source/Setup/Utilities.php
@@ -11,6 +11,7 @@
 use OxidEsales\DatabaseViewsGenerator\ViewsGenerator;
 use OxidEsales\DoctrineMigrationWrapper\Migrations;
 use OxidEsales\DoctrineMigrationWrapper\MigrationsBuilder;
+use OxidEsales\EshopCommunity\Core\Di\ContainerFacade;
 use OxidEsales\EshopCommunity\Internal\Framework\Configuration\BootstrapConfigurationFactory;
 use OxidEsales\Facts\Facts;
 use Symfony\Component\Console\Output\ConsoleOutput;
@@ -483,7 +484,7 @@ public function getRootDirectory(): string
     public function getSqlDirectory(): string
     {
         return Path::join(
-            (new BootstrapConfigurationFactory())->create()->getShopSourceDirectory(),
+            ContainerFacade::getParameter('oxid_shop_source_directory'),
             self::SETUP_DIRECTORY,
             self::DATABASE_SQL_DIRECTORY
         );
diff --git a/tests/Codeception/Config/CodeceptionParametersProvider.php b/tests/Codeception/Config/CodeceptionParametersProvider.php
index 13dcbc1da12..37f6d7cef9e 100644
--- a/tests/Codeception/Config/CodeceptionParametersProvider.php
+++ b/tests/Codeception/Config/CodeceptionParametersProvider.php
@@ -10,6 +10,7 @@
 namespace OxidEsales\EshopCommunity\Tests\Codeception\Config;
 
 use OxidEsales\Codeception\Module\Database;
+use OxidEsales\EshopCommunity\Core\Di\ContainerFacade;
 use OxidEsales\EshopCommunity\Internal\Framework\Configuration\BootstrapConfigurationFactory;
 use OxidEsales\EshopCommunity\Internal\Framework\Configuration\DataObject\DatabaseConfiguration;
 use OxidEsales\EshopCommunity\Internal\Framework\FileSystem\BootstrapLocator;
@@ -29,7 +30,8 @@ public function getParameters(): array
         $this->dbConfig = (new DatabaseConfiguration($databaseUrl));
         return [
             'SHOP_URL' => getenv('SHOP_URL') ?: $facts->getShopUrl(),
-            'SHOP_SOURCE_PATH' => getenv('SHOP_SOURCE_PATH') ?: $systemConfiguration->getShopSourceDirectory(),
+            'SHOP_SOURCE_PATH' => getenv('SHOP_SOURCE_PATH') ?:
+                ContainerFacade::getParameter('oxid_shop_source_directory'),
             'VENDOR_PATH' => $facts->getVendorPath(),
             'DB_NAME' => $this->getDbName(),
             'DB_USERNAME' => $this->getDbUser(),
diff --git a/tests/Integration/Internal/Framework/Configuration/Dao/SystemConfigurationDaoTest.php b/tests/Integration/Internal/Framework/Configuration/Dao/SystemConfigurationDaoTest.php
index cf2ac360a32..d2a1a58326f 100644
--- a/tests/Integration/Internal/Framework/Configuration/Dao/SystemConfigurationDaoTest.php
+++ b/tests/Integration/Internal/Framework/Configuration/Dao/SystemConfigurationDaoTest.php
@@ -51,7 +51,6 @@ public function testGetDatabaseConfigurationWillContainSomeDefaults(): void
 
     public function testGetBootstrapParametersWillContainsDefaults(): void
     {
-        $this->assertNotEmpty($this->systemConfiguration->get()->getShopSourceDirectory());
         $this->assertNotEmpty($this->systemConfiguration->get()->getCacheDirectory());
     }
 }
diff --git a/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php b/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php
index a43facf2391..4a2e521e9ac 100644
--- a/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php
+++ b/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php
@@ -10,7 +10,6 @@
 namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Framework\Env;
 
 use OxidEsales\EshopCommunity\Internal\Framework\Configuration\BootstrapConfigurationFactory;
-use OxidEsales\EshopCommunity\Internal\Framework\Env\DotenvLoader;
 use OxidEsales\EshopCommunity\Tests\ContainerTrait;
 use OxidEsales\EshopCommunity\Tests\EnvTrait;
 use OxidEsales\EshopCommunity\Tests\RequestTrait;
@@ -83,15 +82,4 @@ public function testJsonDSNsWithSpecialCharactersWillBeParsedAsArray(): void
 
         $this->assertEquals($dsnString, $containerParameter[2]);
     }
-
-    public function testCanPutEnvironmentVariable(): void
-    {
-        $someKey = uniqid('some-key', true);
-        $someValue = uniqid('some-value', true);
-
-        $dotEnvLoader = new DotenvLoader($this->fixtures);
-        $dotEnvLoader->putEnvironmentVariable($someKey, $someValue);
-
-        $this->assertEquals($someValue, getenv($someKey));
-    }
 }
diff --git a/tests/Unit/Internal/BasicContextStub.php b/tests/Unit/Internal/BasicContextStub.php
index ed4c24f66f5..5c694adb90d 100644
--- a/tests/Unit/Internal/BasicContextStub.php
+++ b/tests/Unit/Internal/BasicContextStub.php
@@ -205,11 +205,6 @@ public function getShopRootPath(): string
         return $this->shopRootPath;
     }
 
-    public function setShopRootPath(string $shopRootPath): void
-    {
-        $this->shopRootPath = $shopRootPath;
-    }
-
     public function getOutPath(): string
     {
         return $this->outPath;