Skip to content

Commit

Permalink
rewrite config files to JSON, refactor environment bootstrap #14
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost committed Sep 24, 2023
1 parent e1054cd commit 32c1bbe
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 300 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
216 changes: 0 additions & 216 deletions example/environment/env.example.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/app/controller/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ public function render()
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
]
Expand Down
4 changes: 2 additions & 2 deletions src/app/controller/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public function render()
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
]
Expand Down
4 changes: 2 additions & 2 deletions src/app/controller/welcome.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ public function render()
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
CSS_VERSION
),
],
]
Expand Down
49 changes: 34 additions & 15 deletions src/app/model/database.php
Original file line number Diff line number Diff line change
@@ -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->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)
[
Expand All @@ -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;
}

Expand Down
Loading

0 comments on commit 32c1bbe

Please sign in to comment.