From 32c1bbe4a2c3e11f4061d7d9caf60af77c36d5c7 Mon Sep 17 00:00:00 2001 From: ghost Date: Sun, 24 Sep 2023 23:18:51 +0300 Subject: [PATCH] rewrite config files to JSON, refactor environment bootstrap #14 --- .gitignore | 5 + README.md | 5 +- example/environment/env.example.php | 216 ---------------------------- src/app/controller/index.php | 4 +- src/app/controller/response.php | 4 +- src/app/controller/welcome.php | 4 +- src/app/model/database.php | 49 +++++-- src/config/bootstrap.php | 116 +++++++-------- src/config/database.json | 7 + src/config/memcached.json | 6 + src/config/moderators.json | 3 + src/config/sphinx.json | 4 + src/config/website.json | 4 + 13 files changed, 127 insertions(+), 300 deletions(-) delete mode 100644 example/environment/env.example.php create mode 100644 src/config/database.json create mode 100644 src/config/memcached.json create mode 100644 src/config/moderators.json create mode 100644 src/config/sphinx.json create mode 100644 src/config/website.json diff --git a/.gitignore b/.gitignore index afb0638..9dca305 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,12 @@ /src/config/* !/src/config/bootstrap.php +!/src/config/website.json +!/src/config/sphinx.json +!/src/config/memcached.json +!/src/config/database.json !/src/config/validator.json +!/src/config/moderators.json !/src/config/nodes.json !/src/config/trackers.json !/src/config/peers.json diff --git a/README.md b/README.md index c1dcba8..6cb88b8 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,9 @@ memcached * The web root dir is `/src/public` * Deploy the database using [MySQL Workbench](https://www.mysql.com/products/workbench) project presented in the `/database` folder * Install [Sphinx Search Server](https://sphinxsearch.com) -* Configuration examples presented at `/example/environment` folder. On first app launch, configuration file will be auto-generated in `/src/config` -* Make sure `/src/api` folder writable +* Server environment examples presented at `/example/environment` folder +* App config available at `/src/config` folder in JSON format. + + To make environment-based configuration for JSON files, create subfolder `/src/config/env` and define `env` in `/src/config/.env` file #### Contribute diff --git a/example/environment/env.example.php b/example/environment/env.example.php deleted file mode 100644 index 2e839d7..0000000 --- a/example/environment/env.example.php +++ /dev/null @@ -1,216 +0,0 @@ - 'text/css', 'href' => sprintf( 'assets/theme/default/css/common.css?%s', - WEBSITE_CSS_VERSION + CSS_VERSION ), ], [ @@ -93,7 +93,7 @@ public function render() 'type' => 'text/css', 'href' => sprintf( 'assets/theme/default/css/framework.css?%s', - WEBSITE_CSS_VERSION + CSS_VERSION ), ], ] diff --git a/src/app/controller/response.php b/src/app/controller/response.php index 9826b13..3961e4b 100644 --- a/src/app/controller/response.php +++ b/src/app/controller/response.php @@ -38,7 +38,7 @@ public function render() 'type' => 'text/css', 'href' => sprintf( 'assets/theme/default/css/common.css?%s', - WEBSITE_CSS_VERSION + CSS_VERSION ), ], [ @@ -46,7 +46,7 @@ public function render() 'type' => 'text/css', 'href' => sprintf( 'assets/theme/default/css/framework.css?%s', - WEBSITE_CSS_VERSION + CSS_VERSION ), ], ] diff --git a/src/app/controller/welcome.php b/src/app/controller/welcome.php index 611be0d..0f034dd 100644 --- a/src/app/controller/welcome.php +++ b/src/app/controller/welcome.php @@ -92,7 +92,7 @@ public function render() 'type' => 'text/css', 'href' => sprintf( 'assets/theme/default/css/common.css?%s', - WEBSITE_CSS_VERSION + CSS_VERSION ), ], [ @@ -100,7 +100,7 @@ public function render() 'type' => 'text/css', 'href' => sprintf( 'assets/theme/default/css/framework.css?%s', - WEBSITE_CSS_VERSION + CSS_VERSION ), ], ] diff --git a/src/app/model/database.php b/src/app/model/database.php index ec435cb..6d67d26 100644 --- a/src/app/model/database.php +++ b/src/app/model/database.php @@ -1,17 +1,36 @@ _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->setAttribute( + PDO::ATTR_ERRMODE, + PDO::ERRMODE_EXCEPTION + ); + + $this->_db->setAttribute( + PDO::ATTR_DEFAULT_FETCH_MODE, + PDO::FETCH_OBJ + ); - $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_TIMEOUT, + 600 + ); $this->_debug = (object) [ @@ -38,23 +57,23 @@ public function __construct(string $host, int $port, string $database, string $u } // 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; } diff --git a/src/config/bootstrap.php b/src/config/bootstrap.php index 22f72b8..9617217 100644 --- a/src/config/bootstrap.php +++ b/src/config/bootstrap.php @@ -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); - - 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.')); -} +// Environment +require_once __DIR__ . '/../library/environment.php'; -// Load environment -require_once __DIR__ . '/env.' . PHP_ENV . '.php'; - -// Autoload vendors +// Autoload require_once __DIR__ . '/../../vendor/autoload.php'; // Route @@ -50,93 +28,109 @@ { case 'stars': - require_once(__DIR__ . '/../app/controller/stars.php'); + require_once __DIR__ . '/../app/controller/stars.php'; + + $appControllerStars = new AppControllerStars(); - $controller = 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'; + + $appControllerDownloads = new AppControllerDownloads(); - $controller = 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'; + + $appControllerEditions = new AppControllerEditions(); - $controller = new AppControllerEditions(); + $appControllerEditions->render(); break; case 'welcome': - require_once(__DIR__ . '/../app/controller/welcome.php'); + require_once __DIR__ . '/../app/controller/welcome.php'; + + $appControllerWelcome = new AppControllerWelcome(); - $controller = 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(); -$controller->render(); \ No newline at end of file + $appControllerIndex->render(); +} diff --git a/src/config/database.json b/src/config/database.json new file mode 100644 index 0000000..64d4306 --- /dev/null +++ b/src/config/database.json @@ -0,0 +1,7 @@ +{ + "port":3306, + "host":"127.0.0.1", + "name":"", + "user":"", + "password":"" +} \ No newline at end of file diff --git a/src/config/memcached.json b/src/config/memcached.json new file mode 100644 index 0000000..c6dd071 --- /dev/null +++ b/src/config/memcached.json @@ -0,0 +1,6 @@ +{ + "port": 11211, + "host": "127.0.0.1", + "namespace": "", + "timeout": 3600 +} \ No newline at end of file diff --git a/src/config/moderators.json b/src/config/moderators.json new file mode 100644 index 0000000..b344d1e --- /dev/null +++ b/src/config/moderators.json @@ -0,0 +1,3 @@ +[ + "" +] diff --git a/src/config/sphinx.json b/src/config/sphinx.json new file mode 100644 index 0000000..6115883 --- /dev/null +++ b/src/config/sphinx.json @@ -0,0 +1,4 @@ +{ + "port":9306, + "host":"127.0.0.1" +} \ No newline at end of file diff --git a/src/config/website.json b/src/config/website.json new file mode 100644 index 0000000..580a5a9 --- /dev/null +++ b/src/config/website.json @@ -0,0 +1,4 @@ +{ + "name":"YGGtracker", + "url":"" +} \ No newline at end of file