diff --git a/CHANGELOG.md b/CHANGELOG.md index a08e19669..7394a7a9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [unreleased] Unreleased +## Changed + +- Inherit environment variables while parsing configuration files in the `WPConfigFile` class. + ## [4.3.8] 2024-11-27; ## Added diff --git a/config/typos.toml b/config/typos.toml index 6e1fc9967..66bfe12e2 100644 --- a/config/typos.toml +++ b/config/typos.toml @@ -3,6 +3,7 @@ extend-exclude = [ ".git/", "includes/", "vendor/", + "tests/_data" ] ignore-hidden = false diff --git a/src/Process/Loop.php b/src/Process/Loop.php index edcd53c69..edaffa21a 100644 --- a/src/Process/Loop.php +++ b/src/Process/Loop.php @@ -41,6 +41,7 @@ class Loop private bool $fastFailureFlagRaised = false; private bool $useFilePayloads = false; + private bool $inheritEnv = false; /** * @param array $workers @@ -67,7 +68,8 @@ public function __construct( * rethrow?: bool, * requireFiles?: array, * cwd?: string, - * use_file_payloads?: bool, + * useFilePayloads?: bool, + * inheritEnv?: bool * } $options * * @throws ProcessException @@ -78,10 +80,14 @@ public static function executeClosure(Closure $closure, int $timeout = 30, array { $loop = new self([$closure], 1, true, $timeout, $options); - if (!empty($options['use_file_payloads'])) { + if (!empty($options['useFilePayloads'])) { $loop->setUseFilePayloads(true); } + if (!empty($options['inheritEnv'])) { + $loop->setInheritEnv(true); + } + $loop->run(); $results = $loop->getResults(); $result = $results[0]; @@ -191,7 +197,7 @@ private function startWorker(): void } try { - $w = Running::fromWorker($runnableWorker, $this->useFilePayloads); + $w = Running::fromWorker($runnableWorker, $this->useFilePayloads, $this->inheritEnv); $this->started[$w->getId()] = $w; $this->running[$w->getId()] = $w; $this->peakParallelism = max((int)$this->peakParallelism, count($this->running)); @@ -382,4 +388,9 @@ private function buildWorker(string $id, callable $worker): Worker { return new Worker($id, $worker, [], []); } + + public function setInheritEnv(bool $inheritEnv):void + { + $this->inheritEnv = $inheritEnv; + } } diff --git a/src/Process/Worker/Running.php b/src/Process/Worker/Running.php index 3c8baca34..ac425de9a 100644 --- a/src/Process/Worker/Running.php +++ b/src/Process/Worker/Running.php @@ -36,7 +36,7 @@ public function __construct( /** * @throws ConfigurationException|ProcessException */ - public static function fromWorker(Worker $worker, bool $useFilePayloads = false): Running + public static function fromWorker(Worker $worker, bool $useFilePayloads = false, bool $inheritEnv = false): Running { $workerCallable = $worker->getCallable(); $workerClosure = $workerCallable instanceof Closure ? @@ -48,11 +48,17 @@ public static function fromWorker(Worker $worker, bool $useFilePayloads = false) $workerScriptPathname = __DIR__ . '/worker-script.php'; $control = $worker->getControl(); + + if ($inheritEnv) { + $control['env'] = array_merge($control['env'] ?? [], getenv()); + } + $workerSerializableClosure = new SerializableClosure($workerClosure); $request = new Request($control, $workerSerializableClosure); $request->setUseFilePayloads($useFilePayloads); + try { $workerProcess = new WorkerProcess([PHP_BINARY, $workerScriptPathname, $request->getPayload()]); $workerProcess->start(); diff --git a/src/WordPress/WPConfigFile.php b/src/WordPress/WPConfigFile.php index 7972ea132..bd77bbc4e 100644 --- a/src/WordPress/WPConfigFile.php +++ b/src/WordPress/WPConfigFile.php @@ -120,7 +120,7 @@ private function includeFile(): void try { $result = Loop::executeClosure(function () use ($wpSettingsFile, $wpConfigFile): array { return $this->toIncludeFile($wpConfigFile, $wpSettingsFile); - }); + }, 30, ['inheritEnv' => true]); $returnValue = $result->getReturnValue(); diff --git a/tests/_data/wp-config-files/ddev/wp-config-ddev.php b/tests/_data/wp-config-files/ddev/wp-config-ddev.php new file mode 100644 index 000000000..1b1b0a701 --- /dev/null +++ b/tests/_data/wp-config-files/ddev/wp-config-ddev.php @@ -0,0 +1,41 @@ +isDdevProjectEnvVar = (string)getenv('IS_DDEV_PROJECT'); + } + + protected function tearDown(): void + { + parent::tearDown(); + putenv('IS_DDEV_PROJECT=' . (string)$this->isDdevProjectEnvVar); + } + + /** * It should throw if building on non existing root directory * @@ -225,4 +240,13 @@ public function should_detect_use_of_sq_lite_database(): void $this->assertFalse($wpConfigFile->usesMySQL()); $this->assertTrue($wpConfigFile->usesSQLite()); } + + public function test_it_inherits_env():void{ + $configFile = codecept_data_dir('wp-config-files/ddev/wp-config.php'); + putenv('IS_DDEV_PROJECT=true'); + + $wpConfigFile = new WPConfigFile(dirname($configFile), $configFile); + + $this->assertEquals($wpConfigFile->getConstant('DB_NAME'),'db'); + } }