Compare commits
26
Commits
v0.3
...
219b42f995
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
219b42f995
|
||
|
|
70a9e492e3
|
||
|
|
e9bfe4e4ad
|
||
|
|
cf329a20e5
|
||
|
|
88a2a99527
|
||
|
|
e37ea7c09c
|
||
|
|
ad7ea0de9d
|
||
|
|
445774e59a
|
||
|
|
7650b33cd2
|
||
|
|
5e0579463c
|
||
|
|
4fe463fcc5
|
||
|
|
0f87a9c6e3
|
||
|
|
3893ed2231
|
||
|
|
2c18f74f97
|
||
|
|
831433f9d8
|
||
|
|
aa31c857c7
|
||
|
|
73272fa6e5
|
||
|
|
0b047b0bcc
|
||
|
|
ad7b8c3eda
|
||
|
|
51801d4228
|
||
|
|
644197fae8
|
||
|
|
f43121769b
|
||
|
|
948b36c80d
|
||
|
|
635b68ab46
|
||
|
|
8df2e64872
|
||
|
|
427426bebe
|
No files matched your search
Vendored
+3
-3
@@ -32,7 +32,7 @@ pipeline {
|
|||||||
sh 'vendor/bin/phpunit --log-junit unit_test_results.xml --testdox tests'
|
sh 'vendor/bin/phpunit --log-junit unit_test_results.xml --testdox tests'
|
||||||
}
|
}
|
||||||
post {
|
post {
|
||||||
success {
|
always {
|
||||||
archiveArtifacts 'unit_test_results.xml'
|
archiveArtifacts 'unit_test_results.xml'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,10 +47,10 @@ pipeline {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
steps {
|
steps {
|
||||||
sh 'php vendor/bin/phpstan analyse -c phpstan.neon --error-format=prettyJson > static_code_analysis_results.json'
|
sh 'php -d memory_limit=1G vendor/bin/phpstan analyse -c phpstan.neon --error-format=prettyJson > static_code_analysis_results.json'
|
||||||
}
|
}
|
||||||
post {
|
post {
|
||||||
success {
|
always {
|
||||||
archiveArtifacts 'static_code_analysis_results.json'
|
archiveArtifacts 'static_code_analysis_results.json'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,3 @@
|
|||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
const ROOT = __DIR__;
|
const ROOT = __DIR__;
|
||||||
|
|
||||||
class Container
|
|
||||||
{
|
|
||||||
static SokoWeb\Interfaces\Database\IConnection $dbConnection;
|
|
||||||
static SokoWeb\Routing\RouteCollection $routeCollection;
|
|
||||||
static SokoWeb\Interfaces\Session\ISessionHandler $sessionHandler;
|
|
||||||
static SokoWeb\Interfaces\Request\IRequest $request;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
<?php namespace SokoWeb\Database;
|
||||||
|
|
||||||
|
use SokoWeb\Interfaces\Database\IAuditLogger;
|
||||||
|
use SokoWeb\Interfaces\Database\IConnection;
|
||||||
|
|
||||||
|
abstract class AuditLoggerBase implements IAuditLogger
|
||||||
|
{
|
||||||
|
const LOG_TYPE_INSERT = 'insert';
|
||||||
|
const LOG_TYPE_UPDATE = 'update';
|
||||||
|
const LOG_TYPE_DELETE = 'delete';
|
||||||
|
|
||||||
|
private IConnection $connection;
|
||||||
|
|
||||||
|
private string $logTable;
|
||||||
|
|
||||||
|
public function __construct(IConnection $connection, string $logTable)
|
||||||
|
{
|
||||||
|
$this->connection = $connection;
|
||||||
|
$this->logTable = $logTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function logInsert(string $localTable, $localId): void
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'local_table' => $localTable,
|
||||||
|
'local_id' => $localId,
|
||||||
|
'type' => static::LOG_TYPE_INSERT,
|
||||||
|
'modifier_id' => $this->getModifierId(),
|
||||||
|
];
|
||||||
|
|
||||||
|
$query = 'INSERT INTO ' . Utils::backtick($this->logTable) . ' SET ' . Utils::generateColumnsWithBinding(array_keys($data));
|
||||||
|
$stmt = $this->connection->prepare($query);
|
||||||
|
$stmt->execute($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function logUpdate(string $localTable, $localId, array $diff): void
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'local_table' => $localTable,
|
||||||
|
'local_id' => $localId,
|
||||||
|
'type' => static::LOG_TYPE_UPDATE,
|
||||||
|
'modifier_id' => $this->getModifierId(),
|
||||||
|
'column' => null,
|
||||||
|
'old' => null,
|
||||||
|
'new' => null,
|
||||||
|
];
|
||||||
|
|
||||||
|
$query = 'INSERT INTO ' . Utils::backtick($this->logTable) . ' SET ' . Utils::generateColumnsWithBinding(array_keys($data));
|
||||||
|
$stmt = $this->connection->prepare($query);
|
||||||
|
|
||||||
|
foreach ($diff as $name => $values) {
|
||||||
|
$data['column'] = $name;
|
||||||
|
$data['old'] = $values['old'];
|
||||||
|
$data['new'] = $values['new'];
|
||||||
|
$stmt->execute($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function logDelete(string $localTable, $localId, array $attributes): void
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'local_table' => $localTable,
|
||||||
|
'local_id' => $localId,
|
||||||
|
'type' => static::LOG_TYPE_DELETE,
|
||||||
|
'modifier_id' => $this->getModifierId(),
|
||||||
|
'old' => $attributes,
|
||||||
|
];
|
||||||
|
|
||||||
|
$query = 'INSERT INTO ' . Utils::backtick($this->logTable) . ' SET ' . Utils::generateColumnsWithBinding(array_keys($data));
|
||||||
|
$stmt = $this->connection->prepare($query);
|
||||||
|
$stmt->execute($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract protected function getModifierId();
|
||||||
|
}
|
||||||
@@ -19,15 +19,9 @@ class Connection implements IConnection
|
|||||||
$socket = (string) ini_get('mysqli.default_socket');
|
$socket = (string) ini_get('mysqli.default_socket');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
||||||
$this->connection = new mysqli($host, $user, $password, $db, $port, $socket);
|
$this->connection = new mysqli($host, $user, $password, $db, $port, $socket);
|
||||||
|
$this->connection->set_charset('utf8mb4');
|
||||||
if ($this->connection->connect_error) {
|
|
||||||
throw new \Exception('Connection failed: ' . $this->connection->connect_error);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->connection->set_charset('utf8mb4')) {
|
|
||||||
throw new \Exception($this->connection->error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
@@ -37,30 +31,24 @@ class Connection implements IConnection
|
|||||||
|
|
||||||
public function startTransaction(): void
|
public function startTransaction(): void
|
||||||
{
|
{
|
||||||
if (!$this->connection->autocommit(false)) {
|
$this->connection->autocommit(false);
|
||||||
throw new \Exception($this->connection->error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function commit(): void
|
public function commit(): void
|
||||||
{
|
{
|
||||||
if (!$this->connection->commit() || !$this->connection->autocommit(true)) {
|
$this->connection->commit();
|
||||||
throw new \Exception($this->connection->error);
|
$this->connection->autocommit(true);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rollback(): void
|
public function rollback(): void
|
||||||
{
|
{
|
||||||
if (!$this->connection->rollback() || !$this->connection->autocommit(true)) {
|
$this->connection->rollback();
|
||||||
throw new \Exception($this->connection->error);
|
$this->connection->autocommit(true);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function query(string $query): ?IResultSet
|
public function query(string $query): ?IResultSet
|
||||||
{
|
{
|
||||||
if (!($result = $this->connection->query($query))) {
|
$result = $this->connection->query($query);
|
||||||
throw new \Exception($this->connection->error . '. Query: ' . $query);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($result !== true) {
|
if ($result !== true) {
|
||||||
return new ResultSet($result);
|
return new ResultSet($result);
|
||||||
@@ -71,9 +59,7 @@ class Connection implements IConnection
|
|||||||
|
|
||||||
public function multiQuery(string $query): array
|
public function multiQuery(string $query): array
|
||||||
{
|
{
|
||||||
if (!$this->connection->multi_query($query)) {
|
$this->connection->multi_query($query);
|
||||||
throw new \Exception($this->connection->error . '. Query: ' . $query);
|
|
||||||
}
|
|
||||||
|
|
||||||
$ret = [];
|
$ret = [];
|
||||||
do {
|
do {
|
||||||
@@ -86,18 +72,12 @@ class Connection implements IConnection
|
|||||||
$this->connection->more_results();
|
$this->connection->more_results();
|
||||||
} while ($this->connection->next_result());
|
} while ($this->connection->next_result());
|
||||||
|
|
||||||
if ($this->connection->error) {
|
|
||||||
throw new \Exception($this->connection->error . '. Query: ' . $query);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function prepare(string $query): IStatement
|
public function prepare(string $query): IStatement
|
||||||
{
|
{
|
||||||
if (!($stmt = $this->connection->prepare($query))) {
|
$stmt = $this->connection->prepare($query);
|
||||||
throw new \Exception($this->connection->error . '. Query: ' . $query);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Statement($stmt);
|
return new Statement($stmt);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
use SokoWeb\Interfaces\Database\IConnection;
|
use SokoWeb\Interfaces\Database\IConnection;
|
||||||
use SokoWeb\Database\Utils;
|
use SokoWeb\Database\Utils;
|
||||||
|
use SokoWeb\Interfaces\Database\IAuditLogger;
|
||||||
|
use SokoWeb\Interfaces\Database\IResultSet;
|
||||||
|
|
||||||
class Modify
|
class Modify
|
||||||
{
|
{
|
||||||
@@ -9,6 +11,8 @@ class Modify
|
|||||||
|
|
||||||
private string $table;
|
private string $table;
|
||||||
|
|
||||||
|
private ?IAuditLogger $auditLogger;
|
||||||
|
|
||||||
private string $idName = 'id';
|
private string $idName = 'id';
|
||||||
|
|
||||||
private array $attributes = [];
|
private array $attributes = [];
|
||||||
@@ -17,10 +21,13 @@ class Modify
|
|||||||
|
|
||||||
private bool $autoIncrement = true;
|
private bool $autoIncrement = true;
|
||||||
|
|
||||||
public function __construct(IConnection $connection, string $table)
|
private ?array $diff = null;
|
||||||
|
|
||||||
|
public function __construct(IConnection $connection, string $table, ?IAuditLogger $auditLogger = null)
|
||||||
{
|
{
|
||||||
$this->connection = $connection;
|
$this->connection = $connection;
|
||||||
$this->table = $table;
|
$this->table = $table;
|
||||||
|
$this->auditLogger = $auditLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setIdName(string $idName): Modify
|
public function setIdName(string $idName): Modify
|
||||||
@@ -65,6 +72,13 @@ class Modify
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setDiff(array $diff): Modify
|
||||||
|
{
|
||||||
|
$this->diff = $diff;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function getId()
|
public function getId()
|
||||||
{
|
{
|
||||||
return $this->attributes[$this->idName];
|
return $this->attributes[$this->idName];
|
||||||
@@ -89,6 +103,10 @@ class Modify
|
|||||||
|
|
||||||
$stmt = $this->connection->prepare($query);
|
$stmt = $this->connection->prepare($query);
|
||||||
$stmt->execute([$this->idName => $this->attributes[$this->idName]]);
|
$stmt->execute([$this->idName => $this->attributes[$this->idName]]);
|
||||||
|
|
||||||
|
if ($this->auditLogger !== null) {
|
||||||
|
$this->auditLogger->logDelete($this->table, $this->attributes[$this->idName], $this->attributes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function insert(): void
|
private function insert(): void
|
||||||
@@ -99,7 +117,7 @@ class Modify
|
|||||||
$this->attributes[$this->idName] = $this->generateKey();
|
$this->attributes[$this->idName] = $this->generateKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
$set = $this->generateColumnsWithBinding(array_keys($this->attributes));
|
$set = Utils::generateColumnsWithBinding(array_keys($this->attributes));
|
||||||
|
|
||||||
$query = 'INSERT INTO ' . Utils::backtick($this->table) . ' SET ' . $set;
|
$query = 'INSERT INTO ' . Utils::backtick($this->table) . ' SET ' . $set;
|
||||||
|
|
||||||
@@ -109,32 +127,62 @@ class Modify
|
|||||||
if ($this->autoIncrement) {
|
if ($this->autoIncrement) {
|
||||||
$this->attributes[$this->idName] = $this->connection->lastId();
|
$this->attributes[$this->idName] = $this->connection->lastId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->auditLogger !== null) {
|
||||||
|
$this->auditLogger->logInsert($this->table, $this->attributes[$this->idName]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function update(): void
|
private function update(): void
|
||||||
{
|
{
|
||||||
|
if ($this->auditLogger !== null) {
|
||||||
|
$this->generateDiff();
|
||||||
|
if (count($this->diff) === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$attributes = $this->attributes;
|
$attributes = $this->attributes;
|
||||||
unset($attributes[$this->idName]);
|
unset($attributes[$this->idName]);
|
||||||
|
|
||||||
$set = $this->generateColumnsWithBinding(array_keys($attributes));
|
$set = Utils::generateColumnsWithBinding(array_keys($attributes));
|
||||||
|
|
||||||
$query = 'UPDATE ' . Utils::backtick($this->table) . ' SET ' . $set . ' WHERE ' . Utils::backtick($this->idName) . '=?';
|
$query = 'UPDATE ' . Utils::backtick($this->table) . ' SET ' . $set . ' WHERE ' . Utils::backtick($this->idName) . '=?';
|
||||||
|
|
||||||
$stmt = $this->connection->prepare($query);
|
$stmt = $this->connection->prepare($query);
|
||||||
$stmt->execute(array_merge($attributes, [$this->idName => $this->attributes[$this->idName]]));
|
$stmt->execute(array_merge($attributes, [$this->idName => $this->attributes[$this->idName]]));
|
||||||
|
|
||||||
|
if ($this->auditLogger !== null) {
|
||||||
|
$this->auditLogger->logUpdate($this->table, $this->attributes[$this->idName], $this->diff);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function generateColumnsWithBinding(array $columns): string
|
|
||||||
{
|
|
||||||
array_walk($columns, function(&$value, $key) {
|
|
||||||
$value = Utils::backtick($value) . '=?';
|
|
||||||
});
|
|
||||||
|
|
||||||
return implode(',', $columns);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function generateKey(): string
|
private function generateKey(): string
|
||||||
{
|
{
|
||||||
return substr(hash('sha256', serialize($this->attributes) . random_bytes(5) . microtime()), 0, 7);
|
return substr(hash('sha256', serialize($this->attributes) . random_bytes(5) . microtime()), 0, 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function generateDiff(): void
|
||||||
|
{
|
||||||
|
if (isset($this->diff)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->diff = [];
|
||||||
|
$original = $this->readFromDb(array_keys($this->attributes));
|
||||||
|
foreach ($original as $key => $value) {
|
||||||
|
if ($value !== $this->attributes[$key]) {
|
||||||
|
$this->diff[$key] = ['old' => $value, 'new' => $this->attributes[$key]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function readFromDb($columns): array
|
||||||
|
{
|
||||||
|
return (new Select($this->connection, $this->table))
|
||||||
|
->columns($columns)
|
||||||
|
->where($this->idName, '=', $this->attributes[$this->idName])
|
||||||
|
->execute()
|
||||||
|
->fetch(IResultSet::FETCH_ASSOC);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -276,13 +276,12 @@ class Select
|
|||||||
return [(string) $table, $params];
|
return [(string) $table, $params];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($table instanceof Select)
|
if ($table instanceof Select) {
|
||||||
{
|
|
||||||
return $table->generateQuery();
|
return $table->generateQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->tableAliases[$table])) {
|
if (isset($this->tableAliases[$table])) {
|
||||||
$queryString = ($defineAlias ? Utils::backtick($this->tableAliases[$table]) . ' ' . Utils::backtick($table) : Utils::backtick($table));
|
$queryString = $defineAlias ? Utils::backtick($this->tableAliases[$table]) . ' ' . Utils::backtick($table) : Utils::backtick($table);
|
||||||
return [$queryString, $params];
|
return [$queryString, $params];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,24 +294,17 @@ class Select
|
|||||||
return (string) $column;
|
return (string) $column;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_array($column)) {
|
if (!is_array($column)) {
|
||||||
$out = '';
|
$column = [$this->table, $column];
|
||||||
|
|
||||||
if ($column[0]) {
|
|
||||||
list($tableName, $params) = $this->generateTable($column[0]);
|
|
||||||
$out .= $tableName . '.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$out .= Utils::backtick($column[1]);
|
list($tableName, $params) = $this->generateTable($column[0]);
|
||||||
|
$out = $tableName . '.' . Utils::backtick($column[1]);
|
||||||
if (!empty($column[2])) {
|
if (!empty($column[2])) {
|
||||||
$out .= ' ' . Utils::backtick($column[2]);
|
$out .= ' ' . Utils::backtick($column[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $out;
|
return $out;
|
||||||
} else {
|
|
||||||
return Utils::backtick($column);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function generateColumns(): string
|
private function generateColumns(): string
|
||||||
|
|||||||
@@ -5,4 +5,13 @@ class Utils {
|
|||||||
{
|
{
|
||||||
return '`' . $name . '`';
|
return '`' . $name . '`';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function generateColumnsWithBinding(array $columns): string
|
||||||
|
{
|
||||||
|
array_walk($columns, function(&$value, $key) {
|
||||||
|
$value = static::backtick($value) . '=?';
|
||||||
|
});
|
||||||
|
|
||||||
|
return implode(',', $columns);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php namespace SokoWeb\Interfaces\Database;
|
||||||
|
|
||||||
|
interface IAuditLogger
|
||||||
|
{
|
||||||
|
public function logInsert(string $localTable, $localId): void;
|
||||||
|
|
||||||
|
public function logUpdate(string $localTable, $localId, array $diff): void;
|
||||||
|
|
||||||
|
public function logDelete(string $localTable, $localId, array $attributes): void;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?php namespace SokoWeb\Interfaces\PersistentData;
|
||||||
|
|
||||||
|
use Generator;
|
||||||
|
use SokoWeb\Database\Query\Select;
|
||||||
|
use SokoWeb\PersistentData\Model\Model;
|
||||||
|
|
||||||
|
interface IPersistentDataManager
|
||||||
|
{
|
||||||
|
public function selectFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []);
|
||||||
|
|
||||||
|
public function selectMultipleFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Generator;
|
||||||
|
|
||||||
|
public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = []);
|
||||||
|
|
||||||
|
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void;
|
||||||
|
|
||||||
|
public function saveToDb(Model $model): void;
|
||||||
|
|
||||||
|
public function deleteFromDb(Model $model): void;
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ interface IRedirect
|
|||||||
|
|
||||||
const TEMPORARY = 2;
|
const TEMPORARY = 2;
|
||||||
|
|
||||||
public function getUrl(): string;
|
public function getTarget(): string;
|
||||||
|
|
||||||
public function getHttpCode(): int;
|
public function getHttpCode(): int;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<?php namespace SokoWeb\Interfaces\Routing;
|
||||||
|
|
||||||
|
interface IRoute
|
||||||
|
{
|
||||||
|
public function getId(): string;
|
||||||
|
|
||||||
|
public function getHandler(): array;
|
||||||
|
|
||||||
|
public function generateLink(array $parameters = []): string;
|
||||||
|
|
||||||
|
public function testAgainst(array $path): ?array;
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?php namespace SokoWeb\Interfaces\Routing;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
|
||||||
|
interface IRouteCollection
|
||||||
|
{
|
||||||
|
public function get(string $id, string $pattern, array $handler): void;
|
||||||
|
|
||||||
|
public function post(string $id, string $pattern, array $handler): void;
|
||||||
|
|
||||||
|
public function group(string $pattern, Closure $group): void;
|
||||||
|
|
||||||
|
public function getRoute(string $id): ?IRoute;
|
||||||
|
|
||||||
|
public function match(string $method, string $uri): ?array;
|
||||||
|
}
|
||||||
@@ -3,11 +3,24 @@
|
|||||||
use Generator;
|
use Generator;
|
||||||
use SokoWeb\Database\Query\Modify;
|
use SokoWeb\Database\Query\Modify;
|
||||||
use SokoWeb\Database\Query\Select;
|
use SokoWeb\Database\Query\Select;
|
||||||
|
use SokoWeb\Interfaces\Database\IConnection;
|
||||||
|
use SokoWeb\Interfaces\Database\IAuditLogger;
|
||||||
use SokoWeb\Interfaces\Database\IResultSet;
|
use SokoWeb\Interfaces\Database\IResultSet;
|
||||||
|
use SokoWeb\Interfaces\PersistentData\IPersistentDataManager;
|
||||||
use SokoWeb\PersistentData\Model\Model;
|
use SokoWeb\PersistentData\Model\Model;
|
||||||
|
|
||||||
class PersistentDataManager
|
class PersistentDataManager implements IPersistentDataManager
|
||||||
{
|
{
|
||||||
|
private IConnection $dbConnection;
|
||||||
|
|
||||||
|
private ?IAuditLogger $auditLogger;
|
||||||
|
|
||||||
|
public function __construct(IConnection $dbConnection, ?IAuditLogger $auditLogger = null)
|
||||||
|
{
|
||||||
|
$this->dbConnection = $dbConnection;
|
||||||
|
$this->auditLogger = $auditLogger;
|
||||||
|
}
|
||||||
|
|
||||||
public function selectFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = [])
|
public function selectFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = [])
|
||||||
{
|
{
|
||||||
$select = $this->createSelect($select, $type, $useRelations, $withRelations);
|
$select = $this->createSelect($select, $type, $useRelations, $withRelations);
|
||||||
@@ -37,19 +50,157 @@ class PersistentDataManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function selectFromDbById($id, string $type, bool $useRelations = false)
|
public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = [])
|
||||||
{
|
{
|
||||||
$select = new Select(\Container::$dbConnection);
|
$select = new Select($this->dbConnection);
|
||||||
$select->whereId($id);
|
$select->whereId($id);
|
||||||
|
|
||||||
return $this->selectFromDb($select, $type, $useRelations);
|
return $this->selectFromDb($select, $type, $useRelations, $withRelations);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
|
public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void
|
||||||
{
|
{
|
||||||
$relations = $model::getRelations();
|
$relations = $model::getRelations();
|
||||||
if (count($withRelations)) {
|
if (count($withRelations)) {
|
||||||
$relations = array_intersect($relations, $withRelations);
|
$relations = array_intersect_key($relations, array_flip($withRelations));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($relations as $relation => $relationType) {
|
||||||
|
$camel = str_replace('_', '', ucwords($relation, '_'));
|
||||||
|
|
||||||
|
$methodGet = 'get' . $camel . 'Id';
|
||||||
|
$methodSet = 'set' . $camel;
|
||||||
|
|
||||||
|
$relationId = $model->$methodGet();
|
||||||
|
|
||||||
|
if ($relationId !== null) {
|
||||||
|
$relationModel = $this->selectFromDbById($relationId, $relationType, $recursive, $withRelations);
|
||||||
|
|
||||||
|
$model->$methodSet($relationModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function saveToDb(Model $model): void
|
||||||
|
{
|
||||||
|
$this->syncRelations($model);
|
||||||
|
|
||||||
|
$modified = $model->toArray();
|
||||||
|
$id = $model->getId();
|
||||||
|
|
||||||
|
$modify = new Modify($this->dbConnection, $model::getTable(), $this->auditLogger);
|
||||||
|
|
||||||
|
if ($id !== null) {
|
||||||
|
$original = $model->getSnapshot();
|
||||||
|
$diff = [];
|
||||||
|
|
||||||
|
foreach ($original as $key => $value) {
|
||||||
|
if ($value === $modified[$key]) {
|
||||||
|
unset($modified[$key]);
|
||||||
|
} else {
|
||||||
|
$diff[$key] = ['old' => $value, 'new' => $modified[$key]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($modified) > 0) {
|
||||||
|
$modify->setId($id);
|
||||||
|
$modify->setDiff($diff);
|
||||||
|
$modify->fill($modified);
|
||||||
|
$modify->save();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$modify->fill($modified);
|
||||||
|
$modify->save();
|
||||||
|
|
||||||
|
$model->setId($modify->getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
$model->saveSnapshot();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteFromDb(Model $model): void
|
||||||
|
{
|
||||||
|
$modify = new Modify($this->dbConnection, $model::getTable(), $this->auditLogger);
|
||||||
|
$modify->setId($model->getId());
|
||||||
|
$modify->fill($model->toArray());
|
||||||
|
$modify->delete();
|
||||||
|
|
||||||
|
$model->setId(null);
|
||||||
|
$model->resetSnapshot();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createSelect(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Select
|
||||||
|
{
|
||||||
|
$table = call_user_func([$type, 'getTable']);
|
||||||
|
$fields = call_user_func([$type, 'getFields']);
|
||||||
|
|
||||||
|
$columns = [];
|
||||||
|
|
||||||
|
foreach ($fields as $field) {
|
||||||
|
$columns[] = [$table, $field];
|
||||||
|
}
|
||||||
|
|
||||||
|
$select->from($table);
|
||||||
|
|
||||||
|
if ($useRelations) {
|
||||||
|
$relations = call_user_func([$type, 'getRelations']);
|
||||||
|
if (count($withRelations)) {
|
||||||
|
$relations = array_intersect_key($relations, array_flip($withRelations));
|
||||||
|
}
|
||||||
|
|
||||||
|
$columns = array_merge($columns, $this->getRelationColumns($table, $relations, $withRelations));
|
||||||
|
|
||||||
|
$this->leftJoinRelations($select, $table, $relations, $withRelations);
|
||||||
|
$select->columns($columns);
|
||||||
|
} else {
|
||||||
|
$select->columns($columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $select;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getRelationColumns(string $table, array $relations, array $withRelations): array
|
||||||
|
{
|
||||||
|
$columns = [];
|
||||||
|
|
||||||
|
foreach ($relations as $relation => $relationType) {
|
||||||
|
$relationTableAlias = $table . '__' . $relation;
|
||||||
|
$relationTable = call_user_func([$relationType, 'getTable']);
|
||||||
|
foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
|
||||||
|
$columns[] = [$relationTableAlias, $relationField, $relation . '__' . $relationField];
|
||||||
|
}
|
||||||
|
|
||||||
|
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
||||||
|
if (count($withRelations)) {
|
||||||
|
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
|
||||||
|
}
|
||||||
|
$columns = array_merge($columns, $this->getRelationColumns($relationTable, $relationsOfRelation, $withRelations));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
|
||||||
|
{
|
||||||
|
foreach ($relations as $relation => $relationType) {
|
||||||
|
$relationTableAlias = $table . '__' . $relation;
|
||||||
|
$relationTable = call_user_func([$relationType, 'getTable']);
|
||||||
|
$select->setTableAliases([$relationTableAlias => $relationTable]);
|
||||||
|
$select->leftJoin($relationTableAlias, [$relationTableAlias, 'id'], '=', [$table, $relation . '_id']);
|
||||||
|
|
||||||
|
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
||||||
|
if (count($withRelations)) {
|
||||||
|
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
|
||||||
|
}
|
||||||
|
$this->leftJoinRelations($select, $relationTable, $relationsOfRelation, $withRelations);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
|
||||||
|
{
|
||||||
|
$relations = $model::getRelations();
|
||||||
|
if (count($withRelations)) {
|
||||||
|
$relations = array_intersect_key($relations, array_flip($withRelations));
|
||||||
}
|
}
|
||||||
|
|
||||||
while (key($data)) {
|
while (key($data)) {
|
||||||
@@ -76,12 +227,16 @@ class PersistentDataManager
|
|||||||
|
|
||||||
next($data);
|
next($data);
|
||||||
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
|
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
|
||||||
|
if ($data[$relation . '__id'] !== null) {
|
||||||
$relationType = current($relations);
|
$relationType = current($relations);
|
||||||
$relationModel = new $relationType();
|
$relationModel = new $relationType();
|
||||||
$this->fillWithData($data, $relationModel, $withRelations, $relation);
|
$this->fillWithData($data, $relationModel, $withRelations, $relation);
|
||||||
|
|
||||||
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
|
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
|
||||||
$model->$method($relationModel);
|
$model->$method($relationModel);
|
||||||
|
} else {
|
||||||
|
next($data);
|
||||||
|
}
|
||||||
|
|
||||||
next($relations);
|
next($relations);
|
||||||
} else {
|
} else {
|
||||||
@@ -92,131 +247,6 @@ class PersistentDataManager
|
|||||||
$model->saveSnapshot();
|
$model->saveSnapshot();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loadRelationsFromDb(Model $model, bool $recursive): void
|
|
||||||
{
|
|
||||||
foreach ($model::getRelations() as $relation => $relationType) {
|
|
||||||
$camel = str_replace('_', '', ucwords($relation, '_'));
|
|
||||||
|
|
||||||
$methodGet = 'get' . $camel . 'Id';
|
|
||||||
$methodSet = 'set' . $camel;
|
|
||||||
|
|
||||||
$relationId = $model->$methodGet();
|
|
||||||
|
|
||||||
if ($relationId !== null) {
|
|
||||||
$relationModel = $this->selectFromDbById($relationId, $relationType, $recursive);
|
|
||||||
|
|
||||||
$model->$methodSet($relationModel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function saveToDb(Model $model): void
|
|
||||||
{
|
|
||||||
$this->syncRelations($model);
|
|
||||||
|
|
||||||
$modified = $model->toArray();
|
|
||||||
$id = $model->getId();
|
|
||||||
|
|
||||||
$modify = new Modify(\Container::$dbConnection, $model::getTable());
|
|
||||||
|
|
||||||
if ($id !== null) {
|
|
||||||
$original = $model->getSnapshot();
|
|
||||||
|
|
||||||
foreach ($original as $key => $value) {
|
|
||||||
if ($value === $modified[$key]) {
|
|
||||||
unset($modified[$key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($modified) > 0) {
|
|
||||||
$modify->setId($id);
|
|
||||||
$modify->fill($modified);
|
|
||||||
$modify->save();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$modify->fill($modified);
|
|
||||||
$modify->save();
|
|
||||||
|
|
||||||
$model->setId($modify->getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
$model->saveSnapshot();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function deleteFromDb(Model $model): void
|
|
||||||
{
|
|
||||||
$modify = new Modify(\Container::$dbConnection, $model::getTable());
|
|
||||||
$modify->setId($model->getId());
|
|
||||||
$modify->delete();
|
|
||||||
|
|
||||||
$model->setId(null);
|
|
||||||
$model->resetSnapshot();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function createSelect(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Select
|
|
||||||
{
|
|
||||||
$table = call_user_func([$type, 'getTable']);
|
|
||||||
$fields = call_user_func([$type, 'getFields']);
|
|
||||||
|
|
||||||
$columns = [];
|
|
||||||
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$columns[] = [$table, $field];
|
|
||||||
}
|
|
||||||
|
|
||||||
$select->from($table);
|
|
||||||
|
|
||||||
if ($useRelations) {
|
|
||||||
$relations = call_user_func([$type, 'getRelations']);
|
|
||||||
if (count($withRelations)) {
|
|
||||||
$relations = array_intersect($relations, $withRelations);
|
|
||||||
}
|
|
||||||
|
|
||||||
$columns = array_merge($columns, $this->getRelationColumns($relations, $withRelations));
|
|
||||||
|
|
||||||
$this->leftJoinRelations($select, $table, $relations, $withRelations);
|
|
||||||
$select->columns($columns);
|
|
||||||
} else {
|
|
||||||
$select->columns($columns);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $select;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getRelationColumns(array $relations, array $withRelations): array
|
|
||||||
{
|
|
||||||
$columns = [];
|
|
||||||
|
|
||||||
foreach ($relations as $relation => $relationType) {
|
|
||||||
$relationTable = call_user_func([$relationType, 'getTable']);
|
|
||||||
foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
|
|
||||||
$columns[] = [$relationTable, $relationField, $relation . '__' . $relationField];
|
|
||||||
}
|
|
||||||
|
|
||||||
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
|
|
||||||
if (count($withRelations)) {
|
|
||||||
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
|
|
||||||
}
|
|
||||||
$columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations, $withRelations));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $columns;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
|
|
||||||
{
|
|
||||||
foreach ($relations as $relation => $relationType) {
|
|
||||||
$relationTable = call_user_func([$relationType, 'getTable']);
|
|
||||||
$select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']);
|
|
||||||
|
|
||||||
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
|
|
||||||
if (count($withRelations)) {
|
|
||||||
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
|
|
||||||
}
|
|
||||||
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations, $withRelations);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function syncRelations(Model $model): void
|
private function syncRelations(Model $model): void
|
||||||
{
|
{
|
||||||
foreach ($model::getRelations() as $relation => $relationType) {
|
foreach ($model::getRelations() as $relation => $relationType) {
|
||||||
|
|||||||
@@ -0,0 +1,154 @@
|
|||||||
|
<?php namespace SokoWeb\Response;
|
||||||
|
|
||||||
|
use ErrorException;
|
||||||
|
use Exception;
|
||||||
|
use SokoWeb\Interfaces\Response\IRedirect;
|
||||||
|
use SokoWeb\Interfaces\Response\IContent;
|
||||||
|
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
|
||||||
|
use SokoWeb\Interfaces\Authorization\ISecured;
|
||||||
|
use SokoWeb\Interfaces\Database\IConnection;
|
||||||
|
use SokoWeb\Interfaces\Request\IRequest;
|
||||||
|
use SokoWeb\Response\Redirect;
|
||||||
|
use SokoWeb\Response\HtmlContent;
|
||||||
|
use SokoWeb\Response\JsonContent;
|
||||||
|
use SokoWeb\Routing\RouteCollection;
|
||||||
|
|
||||||
|
class HttpResponse
|
||||||
|
{
|
||||||
|
private IRequest $request;
|
||||||
|
|
||||||
|
private IConnection $dbConnection;
|
||||||
|
|
||||||
|
private RouteCollection $routeCollection;
|
||||||
|
|
||||||
|
private array $appConfig;
|
||||||
|
|
||||||
|
private string $method;
|
||||||
|
|
||||||
|
private string $rawUrl;
|
||||||
|
|
||||||
|
private array $parsedUrl;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
IRequest $request,
|
||||||
|
IConnection $dbConnection,
|
||||||
|
RouteCollection $routeCollection,
|
||||||
|
array $appConfig,
|
||||||
|
string $requestMethod,
|
||||||
|
string $requestUrl
|
||||||
|
) {
|
||||||
|
set_error_handler([$this, 'exceptionsErrorHandler']);
|
||||||
|
|
||||||
|
$this->request = $request;
|
||||||
|
$this->dbConnection = $dbConnection;
|
||||||
|
$this->routeCollection = $routeCollection;
|
||||||
|
$this->appConfig = $appConfig;
|
||||||
|
$this->method = strtolower($requestMethod);
|
||||||
|
$this->parsedUrl = parse_url($requestUrl);
|
||||||
|
$this->rawUrl = $requestUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exceptionsErrorHandler($severity, $message, $filename, $lineno)
|
||||||
|
{
|
||||||
|
throw new ErrorException($message, 0, $severity, $filename, $lineno);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(): void
|
||||||
|
{
|
||||||
|
$match = $this->routeCollection->match($this->method, $this->parsedUrl['path']);
|
||||||
|
if ($match === null) {
|
||||||
|
$this->render404();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
list($route, $params) = $match;
|
||||||
|
$this->request->setParsedRouteParams($params);
|
||||||
|
$handler = $route->getHandler();
|
||||||
|
$controller = new $handler[0]();
|
||||||
|
|
||||||
|
if (
|
||||||
|
$controller instanceof IAuthenticationRequired &&
|
||||||
|
$controller->isAuthenticationRequired() &&
|
||||||
|
$this->request->user() === null
|
||||||
|
) {
|
||||||
|
$this->redirectToLogin();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
$this->method === 'post' &&
|
||||||
|
!in_array($this->parsedUrl['path'], $this->appConfig['antiCsrfTokenExceptions']) &&
|
||||||
|
$this->request->post($this->appConfig['antiCsrfTokenName']) !== $this->request->session()->get($this->appConfig['antiCsrfTokenName'])
|
||||||
|
) {
|
||||||
|
$this->renderAntiCsrfError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($controller instanceof ISecured && !$controller->authorize()) {
|
||||||
|
$this->render404();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->dbConnection->startTransaction();
|
||||||
|
try {
|
||||||
|
$response = call_user_func([$controller, $handler[1]]);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
$this->dbConnection->rollback();
|
||||||
|
$this->render500($exception);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$this->dbConnection->commit();
|
||||||
|
|
||||||
|
if ($response instanceof IContent) {
|
||||||
|
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
|
||||||
|
$response->render();
|
||||||
|
} elseif ($response instanceof IRedirect) {
|
||||||
|
header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode());
|
||||||
|
} else {
|
||||||
|
$this->render404();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function redirectToLogin(): void
|
||||||
|
{
|
||||||
|
$this->request->session()->set('redirect_after_login', $this->rawUrl);
|
||||||
|
$response = new Redirect($this->routeCollection->getRoute($this->appConfig['loginRouteId'])->generateLink(), IRedirect::TEMPORARY);
|
||||||
|
header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
private function renderAntiCsrfError(): void
|
||||||
|
{
|
||||||
|
$content = new JsonContent($this->appConfig['antiCsrfTokenErrorResponse']);
|
||||||
|
header('Content-Type: text/html; charset=UTF-8', true, 403);
|
||||||
|
$content->render();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function render404(): void
|
||||||
|
{
|
||||||
|
$content = new HtmlContent($this->appConfig['error404View']);
|
||||||
|
header('Content-Type: text/html; charset=UTF-8', true, 404);
|
||||||
|
$content->render();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function render500(Exception $exception): void
|
||||||
|
{
|
||||||
|
if (empty($_ENV['DEV'])) {
|
||||||
|
$exceptionToPrint = null;
|
||||||
|
} else {
|
||||||
|
$exceptionToPrint = (string)$exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = new HtmlContent($this->appConfig['error500View'], ['exceptionToPrint' => $exceptionToPrint]);
|
||||||
|
header('Content-Type: text/html; charset=UTF-8', true, 500);
|
||||||
|
$content->render();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getRedirectUrl(IRedirect $redirect): string
|
||||||
|
{
|
||||||
|
$url = $redirect->getTarget();
|
||||||
|
if (preg_match('/^http(s)?/', $url) !== 1) {
|
||||||
|
$url = $this->request->getBase() . $url;
|
||||||
|
}
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,15 +14,9 @@ class Redirect implements IRedirect
|
|||||||
$this->type = $type;
|
$this->type = $type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUrl(): string
|
public function getTarget(): string
|
||||||
{
|
{
|
||||||
if (preg_match('/^http(s)?/', $this->target) === 1) {
|
return $this->target;
|
||||||
$link = $this->target;
|
|
||||||
} else {
|
|
||||||
$link = \Container::$request->getBase() . '/' . $this->target;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $link;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getHttpCode(): int
|
public function getHttpCode(): int
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
<?php namespace SokoWeb\Routing;
|
<?php namespace SokoWeb\Routing;
|
||||||
|
|
||||||
class Route
|
use SokoWeb\Interfaces\Routing\IRoute;
|
||||||
|
|
||||||
|
class Route implements IRoute
|
||||||
{
|
{
|
||||||
private string $id;
|
private string $id;
|
||||||
|
|
||||||
@@ -53,7 +55,7 @@ class Route
|
|||||||
|
|
||||||
$query = count($queryParams) > 0 ? '?' . http_build_query($queryParams) : '';
|
$query = count($queryParams) > 0 ? '?' . http_build_query($queryParams) : '';
|
||||||
|
|
||||||
return implode('/', $link) . $query;
|
return '/' . implode('/', $link) . $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAgainst(array $path): ?array
|
public function testAgainst(array $path): ?array
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
<?php namespace SokoWeb\Routing;
|
<?php namespace SokoWeb\Routing;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
|
use SokoWeb\Interfaces\Routing\IRoute;
|
||||||
|
use SokoWeb\Interfaces\Routing\IRouteCollection;
|
||||||
|
|
||||||
class RouteCollection
|
class RouteCollection implements IRouteCollection
|
||||||
{
|
{
|
||||||
private array $routes = [];
|
private array $routes = [];
|
||||||
|
|
||||||
@@ -32,7 +34,7 @@ class RouteCollection
|
|||||||
array_pop($this->groupStack);
|
array_pop($this->groupStack);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRoute(string $id): ?Route
|
public function getRoute(string $id): ?IRoute
|
||||||
{
|
{
|
||||||
if (!isset($this->routes[$id])) {
|
if (!isset($this->routes[$id])) {
|
||||||
return null;
|
return null;
|
||||||
@@ -41,9 +43,10 @@ class RouteCollection
|
|||||||
return $this->routes[$id];
|
return $this->routes[$id];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function match(string $method, array $uri): ?array
|
public function match(string $method, string $uri): ?array
|
||||||
{
|
{
|
||||||
$groupNumber = count($uri);
|
$path = $uri === '/' ? [] : explode('/', ltrim($uri, '/'));
|
||||||
|
$groupNumber = count($path);
|
||||||
|
|
||||||
// response to HEAD request with the GET content
|
// response to HEAD request with the GET content
|
||||||
if ($method === 'head') {
|
if ($method === 'head') {
|
||||||
@@ -55,7 +58,7 @@ class RouteCollection
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->searchTable[$method][$groupNumber] as $route) {
|
foreach ($this->searchTable[$method][$groupNumber] as $route) {
|
||||||
if (($parameters = $route->testAgainst($uri)) !== null) {
|
if (($parameters = $route->testAgainst($path)) !== null) {
|
||||||
return [$route, $parameters];
|
return [$route, $parameters];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,15 +3,23 @@
|
|||||||
use DateTime;
|
use DateTime;
|
||||||
use SokoWeb\Database\Query\Modify;
|
use SokoWeb\Database\Query\Modify;
|
||||||
use SokoWeb\Database\Query\Select;
|
use SokoWeb\Database\Query\Select;
|
||||||
|
use SokoWeb\Interfaces\Database\IConnection;
|
||||||
use SokoWeb\Interfaces\Database\IResultSet;
|
use SokoWeb\Interfaces\Database\IResultSet;
|
||||||
use SokoWeb\Interfaces\Session\ISessionHandler;
|
use SokoWeb\Interfaces\Session\ISessionHandler;
|
||||||
|
|
||||||
class DatabaseSessionHandler implements ISessionHandler
|
class DatabaseSessionHandler implements ISessionHandler
|
||||||
{
|
{
|
||||||
|
private IConnection $dbConnection;
|
||||||
|
|
||||||
private bool $exists = false;
|
private bool $exists = false;
|
||||||
|
|
||||||
private bool $written = false;
|
private bool $written = false;
|
||||||
|
|
||||||
|
public function __construct(IConnection $dbConnection)
|
||||||
|
{
|
||||||
|
$this->dbConnection = $dbConnection;
|
||||||
|
}
|
||||||
|
|
||||||
public function open($savePath, $sessionName): bool
|
public function open($savePath, $sessionName): bool
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
@@ -24,7 +32,7 @@ class DatabaseSessionHandler implements ISessionHandler
|
|||||||
|
|
||||||
public function read($id): string
|
public function read($id): string
|
||||||
{
|
{
|
||||||
$select = new Select(\Container::$dbConnection, 'sessions');
|
$select = new Select($this->dbConnection, 'sessions');
|
||||||
$select->columns(['data']);
|
$select->columns(['data']);
|
||||||
$select->whereId(substr($id, 0, 32));
|
$select->whereId(substr($id, 0, 32));
|
||||||
|
|
||||||
@@ -41,7 +49,7 @@ class DatabaseSessionHandler implements ISessionHandler
|
|||||||
|
|
||||||
public function write($id, $data): bool
|
public function write($id, $data): bool
|
||||||
{
|
{
|
||||||
$modify = new Modify(\Container::$dbConnection, 'sessions');
|
$modify = new Modify($this->dbConnection, 'sessions');
|
||||||
|
|
||||||
if ($this->exists) {
|
if ($this->exists) {
|
||||||
$modify->setId(substr($id, 0, 32));
|
$modify->setId(substr($id, 0, 32));
|
||||||
@@ -60,7 +68,7 @@ class DatabaseSessionHandler implements ISessionHandler
|
|||||||
|
|
||||||
public function destroy($id): bool
|
public function destroy($id): bool
|
||||||
{
|
{
|
||||||
$modify = new Modify(\Container::$dbConnection, 'sessions');
|
$modify = new Modify($this->dbConnection, 'sessions');
|
||||||
$modify->setId(substr($id, 0, 32));
|
$modify->setId(substr($id, 0, 32));
|
||||||
$modify->delete();
|
$modify->delete();
|
||||||
|
|
||||||
@@ -93,7 +101,7 @@ class DatabaseSessionHandler implements ISessionHandler
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$modify = new Modify(\Container::$dbConnection, 'sessions');
|
$modify = new Modify($this->dbConnection, 'sessions');
|
||||||
|
|
||||||
$modify->setId(substr($id, 0, 32));
|
$modify->setId(substr($id, 0, 32));
|
||||||
$modify->set('updated', (new DateTime())->format('Y-m-d H:i:s'));
|
$modify->set('updated', (new DateTime())->format('Y-m-d H:i:s'));
|
||||||
|
|||||||
@@ -15,9 +15,13 @@ $dotenv->load();
|
|||||||
class Container
|
class Container
|
||||||
{
|
{
|
||||||
static SokoWeb\Interfaces\Database\IConnection $dbConnection;
|
static SokoWeb\Interfaces\Database\IConnection $dbConnection;
|
||||||
static SokoWeb\Routing\RouteCollection $routeCollection;
|
static SokoWeb\Interfaces\Database\IAuditLogger $auditLogger;
|
||||||
|
static SokoWeb\Interfaces\PersistentData\IPersistentDataManager $persistentDataManager;
|
||||||
|
static SokoWeb\Interfaces\Routing\IRouteCollection $routeCollection;
|
||||||
static SokoWeb\Interfaces\Session\ISessionHandler $sessionHandler;
|
static SokoWeb\Interfaces\Session\ISessionHandler $sessionHandler;
|
||||||
static SokoWeb\Interfaces\Request\IRequest $request;
|
static SokoWeb\Interfaces\Request\IRequest $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
Container::$dbConnection = new SokoWeb\Database\Mysql\Connection($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
|
Container::$dbConnection = new SokoWeb\Database\Mysql\Connection($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
|
||||||
|
Container::$auditLogger = new {app}\Database\AuditLogger(Container::$dbConnection, 'audit_log');
|
||||||
|
Container::$persistentDataManager = new SokoWeb\PersistentData\PersistentDataManager(Container::$dbConnection, Container::$auditLogger);
|
||||||
@@ -13,3 +13,16 @@ CREATE TABLE `users` (
|
|||||||
UNIQUE KEY `email` (`email`),
|
UNIQUE KEY `email` (`email`),
|
||||||
UNIQUE KEY `google_sub` (`google_sub`)
|
UNIQUE KEY `google_sub` (`google_sub`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
||||||
|
CREATE TABLE `audit_log` (
|
||||||
|
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||||
|
`local_table` varchar(255) NOT NULL,
|
||||||
|
`local_id` int(10) unsigned NOT NULL,
|
||||||
|
`type` enum('insert','update','delete') NOT NULL,
|
||||||
|
`date` timestamp NOT NULL DEFAULT current_timestamp(),
|
||||||
|
`modifier_id` int(10) unsigned NULL,
|
||||||
|
`column` varchar(255) NULL,
|
||||||
|
`old` text NULL,
|
||||||
|
`new` text NULL,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php namespace {app}\Database;
|
||||||
|
|
||||||
|
use SokoWeb\Database\AuditLoggerBase;
|
||||||
|
|
||||||
|
class AuditLogger extends AuditLoggerBase
|
||||||
|
{
|
||||||
|
protected function getModifierId()
|
||||||
|
{
|
||||||
|
\Container::$request->user()->getUniqueId();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,7 +14,7 @@ Container::$routeCollection = new SokoWeb\Routing\RouteCollection();
|
|||||||
Container::$routeCollection->get('index', '', [{app}\Controller\HomeController::class, 'getIndex']);
|
Container::$routeCollection->get('index', '', [{app}\Controller\HomeController::class, 'getIndex']);
|
||||||
|
|
||||||
if (isset($_COOKIE['COOKIES_CONSENT'])) {
|
if (isset($_COOKIE['COOKIES_CONSENT'])) {
|
||||||
Container::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler();
|
Container::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler(Container::$dbConnection);
|
||||||
|
|
||||||
session_set_save_handler(Container::$sessionHandler, true);
|
session_set_save_handler(Container::$sessionHandler, true);
|
||||||
session_start([
|
session_start([
|
||||||
|
|||||||
Reference in new issue
Block a user