Moved to folder backend

This commit is contained in:
Mal 2020-08-19 21:06:45 +02:00
parent 4752fedc5c
commit a7d9c0bf51
35 changed files with 41 additions and 51 deletions

8
.gitignore vendored
View File

@ -1,5 +1,5 @@
data/classes/Setting.php
data/cache
data/tmp
data/qr
backend/classes/Setting.php
backend/cache
backend/tmp
backend/qr
.idea

View File

@ -1,8 +1,8 @@
<?php
require '../../data/classes/core/Autoloader.php';
require '../../backend/classes/core/Autoloader.php';
$autoloader = new Autoloader('../../data/cache/');
$autoloader = new Autoloader('../../backend/cache/');
$session = new Session();

View File

@ -4,9 +4,9 @@ declare(strict_types=1);
class Autoloader
{
private const PATH_CLASSES = 'data/classes';
private const PATH_CLASSES = 'backend/classes';
private const PATH_CONTROLLERS = self::PATH_CLASSES . '/controller';
private const PATH_CACHE = 'data/cache/';
private const PATH_CACHE = 'backend/cache/';
public function __construct(string $cachePath = self::PATH_CACHE)
{

View File

@ -21,7 +21,7 @@ interface DatabaseInterface
public function getResult(): array;
/**
* Selects data from a table.
* Selects backend from a table.
*/
public function Select(
string $tableName,
@ -44,7 +44,7 @@ interface DatabaseInterface
public function Insert(string $table, array $fields): ?int;
/**
* Edits data inside a table.
* Edits backend inside a table.
*/
public function Update(string $table, array $fields, array $conditions): void;

View File

@ -16,7 +16,11 @@ final class MySqlDatabase implements DatabaseInterface
string $password = Setting::MYSQL_PASSWORD,
string $database = Setting::MYSQL_DATABASE
) {
$this->connection = new PDO("mysql:host=$hostname;dbname=$database", $user, $password);
try {
$this->connection = new PDO("mysql:host=$hostname;dbname=$database", $user, $password);
} catch (Throwable $e) {
throw new DatabaseException('Couldn\'t connect to the database!');
}
}
public function __destruct()
@ -24,15 +28,15 @@ final class MySqlDatabase implements DatabaseInterface
$this->connection = null;
}
/**
* {@inheritDoc}
*/
public function Query(string $query, array $params = []): void
{
$this->cursor = $this->connection->prepare($query);
if (!$this->cursor) {
throw new Exception('Initialization of database cursor failed');
throw new DatabaseException(
'Initialization of database cursor failed',
DatabaseException::CONNECTION_FAILED
);
}
foreach ($params as $key => $param) {
@ -44,13 +48,10 @@ final class MySqlDatabase implements DatabaseInterface
}
if (!$this->cursor->execute()) {
throw new Exception($this->cursor->errorInfo()[2]);
throw new DatabaseException($this->cursor->errorInfo()[2], $this->cursor->errorInfo()[1]);
}
}
/**
* {@inheritDoc}
*/
public function getResult(): array
{
$result = [];
@ -69,7 +70,7 @@ final class MySqlDatabase implements DatabaseInterface
}
/**
* Selects data from a table.
* Selects backend from a table.
*/
public function Select(
string $tableName,
@ -167,13 +168,10 @@ final class MySqlDatabase implements DatabaseInterface
$this->Query($query, $conditions);
}
/**
* {@inheritDoc}
*/
public function Insert(string $table, array $fields): ?int
{
if (count($fields) === 0) {
throw new Exception('Row to insert is empty!');
throw new DatabaseException('Row to insert is empty!');
}
$fieldNames = implode(',', array_keys($fields));
@ -198,9 +196,6 @@ final class MySqlDatabase implements DatabaseInterface
return $lastInsertedId;
}
/**
* {@inheritDoc}
*/
public function Update(string $table, array $fields, array $conditions): void
{
$conditionPairs = [];
@ -227,9 +222,6 @@ final class MySqlDatabase implements DatabaseInterface
$this->Query($query, array_merge($fields, $conditions));
}
/**
* {@inheritDoc}
*/
public function Count(string $table, array $conditions = []): int
{
$result = $this->Select($table, ['count(*)'], $conditions);
@ -260,9 +252,6 @@ final class MySqlDatabase implements DatabaseInterface
$this->isTransactionOpen = false;
}
/**
* {@inheritDoc}
*/
public function GetLastInsertedId(): int
{
$this->Query('SELECT LAST_INSERT_ID() as ID');

View File

@ -29,6 +29,7 @@ final class Router
if (count($matches[0]) > 0) {
$class = new ReflectionClass($params['controller']);
/** @var AbstractController $controller */
$controller = $class->newInstance($matches[0][0]);
if ($this->requestBody !== null && $this->contentType !== null) {

View File

@ -24,10 +24,9 @@ final class Session
}
}
public function Destroy(): void
public function Destroy(): bool
{
session_unset();
session_destroy();
return session_unset() && session_destroy();
}
public function Login(string $usernameOrEmail, string $password): bool

View File

@ -236,7 +236,7 @@ abstract class Table
}
/**
* Checks if the index is a valid data type.
* Checks if the index is a valid backend type.
*/
public static function IsValidType(int $type): bool
{

View File

@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
final class DatabaseException extends Exception
{
public const CONNECTION_FAILED = 1;
}

View File

@ -0,0 +1,5 @@
<?php
require 'backend/classes/core/Autoloader.php';
Autoloader::BuildCache();

View File

@ -1,7 +0,0 @@
<?php
declare(strict_types=1);
class DatabaseException extends Exception
{
}

View File

@ -1,5 +0,0 @@
<?php
require 'data/classes/core/Autoloader.php';
Autoloader::BuildCache();

View File

@ -1,5 +1,5 @@
<?php
require 'data/classes/core/Autoloader.php';
require 'backend/classes/core/Autoloader.php';
$autoloader = new Autoloader('data/cache');
$autoloader = new Autoloader('backend/cache');