changed dir structure to handle second version of the book
This commit is contained in:
46
Vanilla/src/Service/DatabaseConnection.php
Normal file
46
Vanilla/src/Service/DatabaseConnection.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use PDO;
|
||||
|
||||
/**
|
||||
* Take care of the PDO object.
|
||||
*/
|
||||
class DatabaseConnection
|
||||
{
|
||||
private PDO $dbConnection;
|
||||
|
||||
// Currently no prefixes are used, but could be easily added to config.json.
|
||||
const TABLE_PREFIX = '';
|
||||
const TABLE_USERS = self::TABLE_PREFIX . "users";
|
||||
const TABLE_ADDRESSES = self::TABLE_PREFIX . "addresses";
|
||||
|
||||
public function __construct(private readonly Config $config)
|
||||
{
|
||||
$dbHost = $this->config->getConfig(configKey: 'dbHost');
|
||||
$dbPort = $this->config->getConfig(configKey: 'dbPort');
|
||||
$dbDatabase = $this->config->getConfig(configKey: 'dbDatabase');
|
||||
$dbUser = $this->config->getConfig(configKey: 'dbUser');
|
||||
$dbPassword = $this->config->getConfig(configKey: 'dbPassword');
|
||||
|
||||
$this->dbConnection = new PDO(
|
||||
dsn: "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase",
|
||||
username: $dbUser,
|
||||
password: $dbPassword
|
||||
);
|
||||
$this->dbConnection->setAttribute(attribute: PDO::ATTR_ERRMODE, value: PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
|
||||
public function getConnection(): PDO
|
||||
{
|
||||
return $this->dbConnection;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user