Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 2 additions & 35 deletions src/Classes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
91 changes: 91 additions & 0 deletions src/Classes/ConfigBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

/*
* This file is part of MMLC - ModifiedModuleLoaderClient.
*
* (c) Robin Wieschendorf <mail@robinwieschendorf.de>
*
* 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;
}
}