From b1da73b743adbde56eb81681d39848cbdc57ca7e Mon Sep 17 00:00:00 2001 From: "maxime.batista" Date: Mon, 30 Oct 2023 15:55:06 +0100 Subject: [PATCH] add PDO --- .gitignore | 8 +++++--- Documentation/how-to-dev.md | 3 +-- ci/.drone.yml | 2 +- composer.json | 4 +++- config.php | 4 ++++ profiles/dev-config-profile.php | 10 +++++++++- profiles/prod-config-profile.php | 6 ++++++ public/index.php | 2 ++ sql/database.php | 32 ++++++++++++++++++++++++++++++++ sql/setup-tables.sql | 8 ++++++++ 10 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 sql/database.php create mode 100644 sql/setup-tables.sql diff --git a/.gitignore b/.gitignore index b4c3368..48a117a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,13 @@ -.idea -.vs -.code +.* vendor + composer.lock *.phar /dist +# sqlite database files +*.sqlite + views-mappings.php # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. diff --git a/Documentation/how-to-dev.md b/Documentation/how-to-dev.md index 88e9d75..0af8da5 100644 --- a/Documentation/how-to-dev.md +++ b/Documentation/how-to-dev.md @@ -102,6 +102,7 @@ the file is replaced with `prod-config-file.php` by the CI when deploying to the The two profiles declares an `_asset(string $uri)` function, used by the `/config.php::asset` method, but with different implementations : ### Development profile + ```php $hostname = getHostName(); $front_url = "http://$hostname:5173"; @@ -110,8 +111,6 @@ function _asset(string $assetURI): string { global $front_url; return $front_url . "/" . $assetURI; } - - ``` The simplest profile, simply redirect all assets to the development server diff --git a/ci/.drone.yml b/ci/.drone.yml index cb1a90f..e4380db 100644 --- a/ci/.drone.yml +++ b/ci/.drone.yml @@ -48,5 +48,5 @@ steps: - echo "$SERVER_PRIVATE_KEY" > ~/.ssh/id_rsa - chmod 0600 ~/.ssh - chmod 0500 ~/.ssh/id_rsa* - - rsync -avz -e "ssh -p 80 -o 'StrictHostKeyChecking=no'" --delete /outputs/* iqball@maxou.dev:/server/nginx/IQBall/$DRONE_BRANCH + - rsync -rvz -e "ssh -p 80 -o 'StrictHostKeyChecking=no'" --delete /outputs/* iqball@maxou.dev:/server/nginx/IQBall/$DRONE_BRANCH diff --git a/composer.json b/composer.json index 7cfbb6b..c3bb579 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,8 @@ }, "require": { "altorouter/altorouter": "1.2.0", - "ext-json": "*" + "ext-json": "*", + "ext-pdo": "*", + "ext-pdo_sqlite": "*" } } \ No newline at end of file diff --git a/config.php b/config.php index 11e7c2c..592ee38 100644 --- a/config.php +++ b/config.php @@ -16,4 +16,8 @@ function asset(string $assetURI): string { return _asset($assetURI); } +global $_data_source_name; +$data_source_name = $_data_source_name; +const DATABASE_USER = _DATABASE_USER; +const DATABASE_PASSWORD = _DATABASE_PASSWORD; diff --git a/profiles/dev-config-profile.php b/profiles/dev-config-profile.php index 3ce4986..316ff44 100644 --- a/profiles/dev-config-profile.php +++ b/profiles/dev-config-profile.php @@ -4,9 +4,17 @@ $hostname = getHostName(); $front_url = "http://$hostname:5173"; const _SUPPORTS_FAST_REFRESH = true; +$_data_source_name = "sqlite:${_SERVER['DOCUMENT_ROOT']}/../dev-database.sqlite"; -function _asset(string $assetURI): string { +// no user and password needed for sqlite databases +const _DATABASE_USER = null; +const _DATABASE_PASSWORD = null; + +function _asset(string $assetURI): string +{ global $front_url; return $front_url . "/" . $assetURI; } + + diff --git a/profiles/prod-config-profile.php b/profiles/prod-config-profile.php index d76826c..dfd4d02 100644 --- a/profiles/prod-config-profile.php +++ b/profiles/prod-config-profile.php @@ -5,6 +5,12 @@ require "../views-mappings.php"; const _SUPPORTS_FAST_REFRESH = false; +$database_file = __DIR__ . "/../database.sqlite"; +$_data_source_name = "sqlite:/$database_file"; + +// no user and password needed for sqlite databases +const _DATABASE_USER = null; +const _DATABASE_PASSWORD = null; function _asset(string $assetURI): string { diff --git a/public/index.php b/public/index.php index 23737f5..71c24e3 100644 --- a/public/index.php +++ b/public/index.php @@ -2,6 +2,7 @@ require "../vendor/autoload.php"; require "../config.php"; +require "../sql/database.php"; use App\Controller\SampleFormController; @@ -21,6 +22,7 @@ function get_base_path() { } $basePath = get_base_path(); +$pdo = get_database(); // routes initialization $router = new AltoRouter(); diff --git a/sql/database.php b/sql/database.php new file mode 100644 index 0000000..00a9188 --- /dev/null +++ b/sql/database.php @@ -0,0 +1,32 @@ +query($req); + } + } + } + + touch(__DIR__ . "/.guard"); + + return $pdo; +} + + + diff --git a/sql/setup-tables.sql b/sql/setup-tables.sql new file mode 100644 index 0000000..0c6fbe7 --- /dev/null +++ b/sql/setup-tables.sql @@ -0,0 +1,8 @@ + +-- drop tables here +DROP TABLE IF EXISTS FormEntries; + +CREATE TABLE FormEntries(name varchar, description varchar); + + +