Skip to content

Commit

Permalink
Update configurator.php
Browse files Browse the repository at this point in the history
  • Loading branch information
mevdschee authored Nov 2, 2022
1 parent cb452b3 commit 9f9e6c9
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

// Use default autoload implementation
require 'vendor/autoload.php';
// Load the config parameters
require 'config/config.php';

$filename = 'config/config.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$code = Configurator::loadCode($filename);
Expand Down Expand Up @@ -56,7 +56,6 @@ public static function writeCode($filename, $code)
if (!file_exists($filename)) {
throw new \Exception("Could not write: $filename");
}

}

public static function mergePost($config, $post)
Expand Down Expand Up @@ -123,15 +122,16 @@ public static function generateForm($config)

public static function parseConfig($code)
{
$config = array();
$config = [];
$lines = preg_split("/\r?\n/", $code);
foreach ($lines as $line) {
if (preg_match('/^\s*class ([a-z]+)/i', $line, $matches)) {
if (preg_match('/^\s*MintyPHP\\\([a-z]+)::\$([a-z]+)\s*=(.*);\s*(\/\/(.*))?$/i', $line, $matches)) {
$class = $matches[1];
$config[$class] = array();
} else if (preg_match('/^\s*public static \$([a-z]+)\s*=(.*);\s*(\/\/(.*))?$/i', $line, $matches)) {
$name = $matches[1];
$value = trim($matches[2]);
if (!isset($config[$class])) {
$config[$class] = [];
}
$name = $matches[2];
$value = trim($matches[3]);
if (is_numeric($value) && strpos($value, '.') !== false) {
$value = (float) $value;
} else if (is_numeric($value)) {
Expand All @@ -142,8 +142,8 @@ public static function parseConfig($code)
$value = trim($value, '\'"');
}

if (isset($matches[4])) {
$comment = trim($matches[4]);
if (isset($matches[5])) {
$comment = trim($matches[5]);
} else {
$comment = false;
}
Expand All @@ -162,41 +162,36 @@ public static function generateCode($config)
} else {
return var_export($v['value'], true);
}

};
$code = "<?php\n";
$code .= "namespace MintyPHP\Config;\n";
$code = "<?php\n\n";
foreach ($config as $class => $variables) {
$nameChars = $valueChars = 0;
$nameChars = 0;
foreach ($variables as $v) {
$nameChars = max($nameChars, strlen($v['name']));
$valueChars = max($valueChars, strlen($export($v)));
}
$code .= "\nclass $class\n{\n";
foreach ($variables as $v) {
$name = sprintf("%-${nameChars}s", $v['name']);
$value = sprintf("%-${valueChars}s", $export($v));
$code .= "\tpublic static \$$name = $value;";
$value = sprintf("%s", $export($v));
$code .= "MintyPHP\\$class::\$$name = $value;";
if ($v['comment']) {
$code .= " // $v[comment]";
}

$code .= "\n";
}
$code .= "}\n";
$code .= "\n";
}
return $code;
}

public function testConfig(&$config)
public static function testConfig(&$config)
{
$parameters = array();
foreach ($config as $class => &$variables) {
foreach ($variables as &$v) {
$parameters[$class . '_' . $v['name']] = &$v['value'];
}
}

mysqli_report(MYSQLI_REPORT_ERROR);
$mysqli = new mysqli($parameters['DB_host'], $parameters['DB_username'], $parameters['DB_password']);
if ($mysqli->connect_error) {
echo "ERROR: MySQL connect: ($mysqli->connect_errno) $mysqli->connect_error\n";
Expand Down

0 comments on commit 9f9e6c9

Please sign in to comment.