Skip to content

Commit

Permalink
Removes .cnf parse logic and replaces with external library
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Jun 19, 2018
1 parent eb46de7 commit f5ee2eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 38 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"require": {
"symfony/console": "^3.0",
"ifsnop/mysqldump-php": "^2.4",
"cweagans/composer-patches": "^1.6"
"cweagans/composer-patches": "^1.6",
"bomoko/mysql-cnf-parser": "^0.0.2"
},
"license": "GPL-2.0-or-later",
"authors": [
Expand Down
46 changes: 9 additions & 37 deletions src/ConfigParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace machbarmacher\GdprDump;

use bomoko\MysqlCnfParser\MysqlCnfParser;

/**
* Class ConfParser
*
Expand Down Expand Up @@ -38,45 +40,15 @@ public function getFiltered($sections, $keys = null)
return $result;
}

public function addConfig($configString)
{
$lines = preg_split('/\R/mu', $configString);
// This is too lax but good enough.
$keyRE = '[a-zA-Z0-9_-]+';
$sections = [];
$currentSection = 'NONE';
$success = array_walk($lines,
function ($line) use (&$sections, &$currentSection, $keyRE) {
if (preg_match("/^\s*\\[($keyRE)\\]\s*$/u", $line, $m)) {
$currentSection = $m[1];
} elseif (preg_match("/^\s*($keyRE)\s*=\s*(.*)$/u", $line, $m)) {
$key = $m[1];
$value = $m[2];
$value = strtr($value, [
'\b' => chr(8),
'\t' => "\t",
'\n' => "\n",
'\r' => "\r",
'\s' => ' ',
'\\\\' => '\\',
]);
if (preg_match('/^"(.*?)"(\s*#.*)?$/u', $value, $m) ||
preg_match('^/\'(.*?)\'(\s*#.*)?$/u', $value, $m)
) {
$value = $m[1];
}
$sections[$currentSection][$key] = $value;
}
});
if (!$success) {
throw new \LogicException('Error parsing config sections.');
}
$this->config = array_replace_recursive($this->config, $sections);
}

/**
* Takes a .cnf file and adds its configuration settings to internal state
*
* @param $file
*/
public function addFile($file)
{
$this->addConfig(file_get_contents($file));
$sections = MysqlCnfParser::parse($file);
$this->config = array_replace_recursive($this->config, $sections);
}

}

0 comments on commit f5ee2eb

Please sign in to comment.