jan-vandenberg / cruddiy

No-code Bootstrap PHP CRUD generator
http://cruddiy.com
GNU Affero General Public License v3.0
253 stars 80 forks source link

Created a clean template system for app/config generator #69

Closed germain-italic closed 1 year ago

germain-italic commented 1 year ago

This PR is taken from master (aef1114) to avoid huge PR or breaking dependency while it is being reviewed.

I tried to commit locally it on master and got a merge conflict (solved by accepting the more recent version):


By design the Cruddiy code generator is writing a lot of strings into files. Many of these are stored in a hard to read, syntax-coloration blocking, error-prone, bulky format. Example: the config file generator:

    $txt  = "<?php \n";
    $txt .= "\$db_server = '$server'; \n";
    $txt .= "\$db_name = '$database'; \n";
    $txt .= "\$db_user = '$username'; \n";
    $txt .= "\$db_password = '$password'; \n";
    $txt .= "\$no_of_records_per_page = $numrecordsperpage; \n";
    $txt .= "\$appname = '$appname'; \n\n";
    $txt .= "\$protocol=(\$_SERVER['HTTPS'] == 'on' ? 'https' : 'http');\n";
    $txt .= "\$domain = \$protocol . '://' . \$_SERVER['SCRIPT_NAME']; //Replace domain with your domain name. (Locally typically something like localhost)\n\n";
    $txt .= "\$link = mysqli_connect(\$db_server, \$db_user, \$db_password, \$db_name); \n";

    $txt .= '$query = "SHOW VARIABLES LIKE \'character_set_database\'";' ."\n";
    $txt .= 'if ($result = mysqli_query($link, $query)) {' ."\n";
    $txt .= '    while ($row = mysqli_fetch_row($result)) {' ."\n";
    $txt .= '        if (!$link->set_charset($row[1])) {' ."\n";
    $txt .= '            printf("Error loading character set $row[1]: %s\n", $link->error);' ."\n";
    $txt .= '            exit();' ."\n";
    $txt .= '        } else {' ."\n";
    $txt .= '            // printf("Current character set: %s", $link->character_set_name());' ."\n";
    $txt .= '        }' ."\n";
    $txt .= '    }' ."\n";
    $txt .= '}' ."\n";

    $txt .= "\n?>";

This PR replaces the long string concatenation by a clean template which is opened for value replacement and written to the config file:

(...)
$db_server              = '{{db_server}}';
$db_name                = '{{db_name}}';
$db_user                = '{{db_user}}';
$db_password            = '{{db_password}}';
$no_of_records_per_page = '{{no_of_records_per_page}}';
$appname                = '{{appname}}';
(...)

I hope this template is the first step to a lot of decluttering of the core.

I've also eliminated config file path repetition by using a variable, and removed extra trailing whitespaces (editor plugin).

jan-vandenberg commented 1 year ago

Hi, thanks for the code updates!

I have merged #68 and #71 but I cannot merge this request (or #70) because there is a conflict?

germain-italic commented 1 year ago

🤞