rewrite config files to JSON, refactor environment bootstrap #14

This commit is contained in:
ghost 2023-09-24 23:18:51 +03:00
parent e1054cd69e
commit 32c1bbe4a2
13 changed files with 127 additions and 300 deletions

View file

@ -85,7 +85,7 @@ class AppControllerIndex
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
[
@ -93,7 +93,7 @@ class AppControllerIndex
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
]

View file

@ -38,7 +38,7 @@ class AppControllerResponse
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
[
@ -46,7 +46,7 @@ class AppControllerResponse
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
]

View file

@ -92,7 +92,7 @@ class AppControllerWelcome
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
[
@ -100,7 +100,7 @@ class AppControllerWelcome
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
]

View file

@ -1,17 +1,36 @@
<?php
class AppModelDatabase {
class AppModelDatabase
{
private PDO $_db;
private object $_debug;
public function __construct(string $host, int $port, string $database, string $username, string $password) {
public function __construct(object $config)
{
$this->_db = new PDO(
'mysql:dbname=' . $config->name . ';host=' . $config->host . ';port=' . $config->port . ';charset=utf8',
$config->user,
$config->password,
[
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
]
);
$this->_db = new PDO('mysql:dbname=' . $database . ';host=' . $host . ';port=' . $port . ';charset=utf8', $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']);
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$this->_db->setAttribute(PDO::ATTR_TIMEOUT, 600);
$this->_db->setAttribute(
PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION
);
$this->_db->setAttribute(
PDO::ATTR_DEFAULT_FETCH_MODE,
PDO::FETCH_OBJ
);
$this->_db->setAttribute(
PDO::ATTR_TIMEOUT,
600
);
$this->_debug = (object)
[
@ -38,23 +57,23 @@ class AppModelDatabase {
}
// Tools
public function beginTransaction() {
public function beginTransaction() : void
{
$this->_db->beginTransaction();
}
public function commit() {
public function commit() : void
{
$this->_db->commit();
}
public function rollBack() {
public function rollBack() : void
{
$this->_db->rollBack();
}
public function getDebug() {
public function getDebug() : object
{
return $this->_debug;
}

View file

@ -3,42 +3,20 @@
// PHP
declare(strict_types=1);
// Debug
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// Application
define('APP_VERSION', '2.0.0');
define('API_VERSION', APP_VERSION);
define('CSS_VERSION', APP_VERSION);
// Init environment
if (!file_exists(__DIR__ . '/.env'))
{
if ($handle = fopen(__DIR__ . '/.env', 'w+'))
{
fwrite($handle, 'default');
fclose($handle);
// Environment
require_once __DIR__ . '/../library/environment.php';
chmod(__DIR__ . '/.env', 0770);
}
else exit (_('Could not init environment file. Please check permissions.'));
}
define('PHP_ENV', file_get_contents(__DIR__ . '/.env'));
// Init config
if (!file_exists(__DIR__ . '/env.' . PHP_ENV . '.php'))
{
if (copy(__DIR__ . '/../../example/environment/env.example.php',
__DIR__ . '/env.' . PHP_ENV . '.php'))
{
chmod(__DIR__ . '/env.' . PHP_ENV . '.php', 0770);
}
else exit (_('Could not init configuration file. Please check permissions.'));
}
// Load environment
require_once __DIR__ . '/env.' . PHP_ENV . '.php';
// Autoload vendors
// Autoload
require_once __DIR__ . '/../../vendor/autoload.php';
// Route
@ -50,93 +28,109 @@ if (isset($request['_route_']))
{
case 'stars':
require_once(__DIR__ . '/../app/controller/stars.php');
require_once __DIR__ . '/../app/controller/stars.php';
$controller = new AppControllerStars();
$appControllerStars = new AppControllerStars();
$appControllerStars->render();
break;
case 'views':
require_once(__DIR__ . '/../app/controller/views.php');
require_once __DIR__ . '/../app/controller/views.php';
$controller = new AppControllerViews();
$appControllerViews = new AppControllerViews();
$appControllerViews->render();
break;
case 'downloads':
require_once(__DIR__ . '/../app/controller/downloads.php');
require_once __DIR__ . '/../app/controller/downloads.php';
$controller = new AppControllerDownloads();
$appControllerDownloads = new AppControllerDownloads();
$appControllerDownloads->render();
break;
case 'comments':
require_once(__DIR__ . '/../app/controller/comments.php');
require_once __DIR__ . '/../app/controller/comments.php';
$controller = new AppControllerComments();
$appControllerComments = new AppControllerComments();
$appControllerComments->render();
break;
case 'editions':
require_once(__DIR__ . '/../app/controller/editions.php');
require_once __DIR__ . '/../app/controller/editions.php';
$controller = new AppControllerEditions();
$appControllerEditions = new AppControllerEditions();
$appControllerEditions->render();
break;
case 'welcome':
require_once(__DIR__ . '/../app/controller/welcome.php');
require_once __DIR__ . '/../app/controller/welcome.php';
$controller = new AppControllerWelcome();
$appControllerWelcome = new AppControllerWelcome();
$appControllerWelcome->render();
break;
case 'submit':
case 'page/form':
require_once(__DIR__ . '/../app/model/validator.php');
require_once __DIR__ . '/../app/controller/page.php';
$validator = new AppModelValidator(
json_decode(
file_get_contents(
__DIR__ . '/../config/validator.json'
)
)
$appControllerPage = new AppControllerPage(
Environment::config('website')
);
require_once(__DIR__ . '/../app/controller/submit.php');
require_once __DIR__ . '/../app/model/database.php';
require_once __DIR__ . '/../app/model/validator.php';
$controller = new AppControllerSubmit(
$validator
$appControllerPage->renderFormDescription(
new AppModelDatabase(
Environment::config('database')
),
new AppModelValidator(
Environment::config('validator')
)
);
break;
default:
require_once(__DIR__ . '/../app/controller/response.php');
require_once __DIR__ . '/../app/controller/response.php';
$controller = new AppControllerResponse(
$appControllerResponse = new AppControllerResponse(
sprintf(
_('404 - Not found - %s'),
WEBSITE_NAME
Environment::config('website')->name
),
_('404'),
_('Page not found'),
404
);
$appControllerResponse->render();
}
}
else
{
require_once(__DIR__ . '/../app/controller/index.php');
require_once __DIR__ . '/../app/controller/index.php';
$controller = new AppControllerIndex();
$appControllerIndex = new AppControllerIndex();
$appControllerIndex->render();
}
$controller->render();

7
src/config/database.json Normal file
View file

@ -0,0 +1,7 @@
{
"port":3306,
"host":"127.0.0.1",
"name":"",
"user":"",
"password":""
}

View file

@ -0,0 +1,6 @@
{
"port": 11211,
"host": "127.0.0.1",
"namespace": "",
"timeout": 3600
}

View file

@ -0,0 +1,3 @@
[
""
]

4
src/config/sphinx.json Normal file
View file

@ -0,0 +1,4 @@
{
"port":9306,
"host":"127.0.0.1"
}

4
src/config/website.json Normal file
View file

@ -0,0 +1,4 @@
{
"name":"YGGtracker",
"url":""
}