From 8839ef7ad03dfe327ba5d54cf05260d59096edf1 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Mon, 19 Jun 2023 14:23:48 +0200 Subject: [PATCH] feat: add and use new ConfigBuilder --- src/Classes/Config.php | 37 +------------- src/Classes/ConfigBuilder.php | 91 +++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 35 deletions(-) create mode 100644 src/Classes/ConfigBuilder.php diff --git a/src/Classes/Config.php b/src/Classes/Config.php index effb1575..3af4456b 100644 --- a/src/Classes/Config.php +++ b/src/Classes/Config.php @@ -74,41 +74,8 @@ public static function writeConfiguration(array $options): void $configOld = file_get_contents($configPath); $configNew = ''; - foreach ($options as $key => $lineNewValue) { - $matches = []; - - /** - * Look for line in config which matches: - * '$key' => 'foobar' (i. e.: 'username' => 'root') - * - * Look for $lineNewValue in found line and replace it: - * '$key' => 'foobar' becomes '$key' => '$lineNewValue' - */ - $regex = '/(\'' . $key . '\')[ ]*=>[ ]*(\'.*\')/'; - - preg_match($regex, $configOld, $matches); - - switch (count($matches)) { - case 3: - $lineOld = $matches[0]; - $lineOldValue = $matches[2]; - $lineNewValue = '\'' . $lineNewValue . '\''; - $lineNew = str_replace($lineOldValue, $lineNewValue, $lineOld); - - $configNew = str_replace($lineOld, $lineNew, $configOld); - $configOld = $configNew; - break; - - case 0: - /** - * To do: add option if it doesn't exist - * instead of showing an error. - */ - $configNew = $configOld; - throw new \RuntimeException('Cannot write option. Option "' . $key . '" does not not exist in ' . $configPath . '.'); - break; - } - } + $configBuilder = new ConfigBuilder(); + $configNew = $configBuilder->update($configOld, $options); file_put_contents($configPath, $configNew); diff --git a/src/Classes/ConfigBuilder.php b/src/Classes/ConfigBuilder.php new file mode 100644 index 00000000..dbd765f9 --- /dev/null +++ b/src/Classes/ConfigBuilder.php @@ -0,0 +1,91 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace RobinTheHood\ModifiedModuleLoaderClient; + +class ConfigBuilder +{ + public function update(string $configString, array $options) + { + $newConfigString = $configString; + foreach ($options as $key => $value) { + $newConfigString = $this->updateOption($newConfigString, $key, $value); + } + return $newConfigString; + } + + private function updateOption(string $configString, string $key, $value): string + { + if ($this->hasEntry($configString, $key)) { + return $this->replaceEntry($configString, $key, $value); + } else { + return $this->addEntry($configString, $key, $value); + } + } + + private function replaceEntry(string $configString, string $key, $value): string + { + /** + * Look for line which matches: + * '$key' => 'foobar' (i. e.: 'username' => 'root') + */ + $regex = "/^[ |\t]*'$key'\s*=>\s*'.*',*/m"; + $newLine = " '$key' => '$value',"; + return preg_replace($regex, $newLine, $configString); + } + + private function addEntry(string $configString, string $key, $value): string + { + $lastEntry = $this->getLastEntry($configString); + $newEntry = " '$key' => '$value',"; + + $lastEntryAndNewEntry = $this->addCommaIfNeeded($lastEntry) . "\n" . $newEntry; + + return str_replace($lastEntry, $lastEntryAndNewEntry, $configString); + } + + private function hasEntry(string $configString, string $key): bool + { + /** + * Look for line which matches: + * '$key' => 'foobar' (i. e.: 'username' => 'root') + */ + $regex = "/^[ |\t]*'$key'\s*=>\s*'.*',*/m"; + preg_match($regex, $configString, $matches); + + if (count($matches) === 1) { + return true; + } + + return false; + } + + private function getLastEntry(string $configString): string + { + /** + * Look for line which matches: + * 'xxx' => 'xxx' + */ + $regex = "/^[ |\t]*'.*'\s*=>\s*'.*',*/m"; + preg_match_all($regex, $configString, $matches); + return end($matches[0]); + } + + private function addCommaIfNeeded(string $string): string + { + if (substr($string, -1) !== ',') { + $string .= ','; + } + return $string; + } +}