inicializovany composer pre projekt,

instalovane balicky tpsoft/dbmodel a tpsoft/apilite.
vygenerovane subory dbmodel-files
This commit is contained in:
2026-02-09 06:51:07 +01:00
parent 36a1cb5704
commit d9a283cfdf
6 changed files with 258 additions and 0 deletions

27
backend/composer.json Normal file
View File

@ -0,0 +1,27 @@
{
"name": "tpsoft/nutrio",
"description": "Backend pre jedalnicky s vypoctom kalorii.",
"type": "project",
"license": "MIT",
"autoload": {
"psr-4": {
"TPsoft\\Nutrio\\": "src/"
},
"files": [
"config/Configuration.php"
]
},
"authors": [
{
"name": "igor",
"email": "mino@tpsoft.org"
}
],
"require": {
"tpsoft/apilite": "^1.0",
"tpsoft/dbmodel": "^1.1"
},
"scripts": {
"model": "php scripts/createModel.php"
}
}

113
backend/composer.lock generated Normal file
View File

@ -0,0 +1,113 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "9b92dc81ccf6b0cc7f350c56793c6523",
"packages": [
{
"name": "tpsoft/apilite",
"version": "v1.0.3",
"source": {
"type": "git",
"url": "https://gitea.tpsoft.org/TPsoft.org/APIlite.git",
"reference": "c0fd7b3fe5270ee44a84a92e9255ada2438812b7"
},
"require": {
"php": ">=8.2"
},
"type": "library",
"autoload": {
"psr-4": {
"TPsoft\\APIlite\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"GPL-3.0-or-later"
],
"authors": [
{
"name": "Igor Mino",
"email": "mino@tpsoft.org",
"homepage": "https://www.tpsoft.org",
"role": "Developer"
}
],
"description": "A set of tools to simplify the work of creating backend APIs for your frontend projects.",
"keywords": [
"api",
"json",
"php",
"rest",
"typescript"
],
"funding": [
{
"url": "https://www.anycoin.cz/donate/igormino",
"type": "other"
}
],
"time": "2025-10-13T21:36:34+00:00"
},
{
"name": "tpsoft/dbmodel",
"version": "v1.1.1",
"source": {
"type": "git",
"url": "https://gitea.tpsoft.org/TPsoft.org/DBmodel.git",
"reference": "ff5663282f1efaa6c79d36760afca25ac25c16e6"
},
"require": {
"ext-pdo": "*",
"php": ">=8.2"
},
"bin": [
"bin/dbmodel-files"
],
"type": "library",
"autoload": {
"psr-4": {
"TPsoft\\DBmodel\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"GPL-3.0-or-later"
],
"authors": [
{
"name": "Igor Mino",
"email": "mino@tpsoft.org",
"homepage": "https://www.tpsoft.org",
"role": "Developer"
}
],
"description": "This library extends the builtin PDO object by several useful features. ",
"keywords": [
"db",
"model",
"mysql",
"pdo",
"sqlite"
],
"funding": [
{
"url": "https://www.anycoin.cz/donate/igormino",
"type": "other"
}
],
"time": "2026-02-09T05:39:02+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {},
"prefer-stable": false,
"prefer-lowest": false,
"platform": {},
"platform-dev": {},
"plugin-api-version": "2.6.0"
}

View File

@ -0,0 +1,31 @@
<?php
$others_config = scandir(__DIR__);
$loaded = false;
foreach ($others_config as $file) {
if ($file == basename(__FILE__)) continue;
if (substr($file, -4) == '.php') {
require_once __DIR__ . '/' . $file;
$loaded = true;
}
}
if (!$loaded) {
class Configuration
{
public const DB_TYPE = 'sqlite';
// MySQL
public const DB_HOST = 'localhost';
public const DB_USER = 'username';
public const DB_PASS = '****************';
public const DB_NAME = 'databasename';
// SQLite
public const DB_FILEPATH = __DIR__ . '/../../data/database.db';
}
}
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https://" : "http://";
$host = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI']; // obsahuje aj query string
define('URL_PREFIX', $protocol.$host.str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']));

View File

@ -0,0 +1,9 @@
<?php
require_once __DIR__ . '/../src/Init.php';
use \TPsoft\DBmodel\Creator;
global $dbh;
$creator = new Creator($dbh);
$creator->rootDir(realpath(__DIR__.'/../src/'));
$creator->interact();

20
backend/src/Init.php Normal file
View File

@ -0,0 +1,20 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
use \Exception;
use \TPsoft\DBmodel\DBmodel;
use \TPsoft\Nutrio\Maintenance;
global $dbh;
if (Configuration::DB_TYPE == 'mysql') {
$dbh = new DBmodel(sprintf('mysql:host=%s;dbname=%s;charset=utf8mb4', Configuration::DB_HOST, Configuration::DB_NAME), Configuration::DB_USER, Configuration::DB_PASS);
} else if (Configuration::DB_TYPE == 'sqlite') {
$dbh = new DBmodel(sprintf('sqlite:%s', Configuration::DB_FILEPATH));
} else {
throw new Exception('Unknown database type');
}
$maintenance = new Maintenance($dbh);
$maintenance->database();

View File

@ -0,0 +1,58 @@
<?php
namespace TPsoft\Nutrio\;
class Maintenance extends \TPsoft\DBmodel\Maintenance
{
public function database()
{
if (!$this->existsTable('options')) {
$this->checkDBTable('options', '
`key` VARCHAR(255) NOT NULL PRIMARY KEY,
`value` VARCHAR(255) NOT NULL
');
$this->dbver(1);
}
$dbver = $this->dbver();
if ($dbver == 1) {
// create new
}
}
protected function settings(string $key, ?string $value = null): string|false
{
if (is_null($value)) {
return $this->dbh->getOne(sprintf('SELECT `value` FROM `options` WHERE `key` = %s', $this->dbh->quote($key)));
} else {
$db_type = $this->dbh->getDBtype();
switch ($db_type) {
case 'mysql':
return $this->dbh->query(sprintf(
'INSERT INTO `options` (`key`, `value`) VALUES (%s, %s) ON DUPLICATE KEY UPDATE `value` = %s',
$this->dbh->quote($key),
$this->dbh->quote($value),
$this->dbh->quote($value)
)) !== false;
break;
case 'sqlite':
return $this->dbh->query(sprintf(
'INSERT INTO `options` (`key`, `value`) VALUES (%s, %s) ON CONFLICT(`key`) DO UPDATE SET `value` = %s',
$this->dbh->quote($key),
$this->dbh->quote($value),
$this->dbh->quote($value)
)) !== false;
break;
default:
new \Exception('Unknown DB type: ' . $db_type);
return false;
break;
}
}
}
protected function dbver(?string $ver = null)
{
return $this->settings('version', $ver);
}
}