Compare commits
12
Commits
v0.5
..
219b42f995
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
219b42f995
|
||
|
|
70a9e492e3
|
||
|
|
e9bfe4e4ad
|
||
|
|
cf329a20e5
|
||
|
|
88a2a99527
|
||
|
|
e37ea7c09c
|
||
|
|
ad7ea0de9d
|
||
|
|
445774e59a
|
||
|
|
7650b33cd2
|
||
|
|
5e0579463c
|
||
|
|
4fe463fcc5
|
||
|
|
0f87a9c6e3
|
No files matched your search
@@ -3,12 +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\Interfaces\Database\IAuditLogger $auditLogger;
|
|
||||||
static SokoWeb\Routing\RouteCollection $routeCollection;
|
|
||||||
static SokoWeb\Interfaces\Session\ISessionHandler $sessionHandler;
|
|
||||||
static SokoWeb\Interfaces\Request\IRequest $request;
|
|
||||||
}
|
|
||||||
@@ -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]);
|
|
||||||
|
|
||||||
if (!empty($column[2])) {
|
|
||||||
$out .= ' ' . Utils::backtick($column[2]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $out;
|
|
||||||
} else {
|
|
||||||
return Utils::backtick($column);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
list($tableName, $params) = $this->generateTable($column[0]);
|
||||||
|
$out = $tableName . '.' . Utils::backtick($column[1]);
|
||||||
|
if (!empty($column[2])) {
|
||||||
|
$out .= ' ' . Utils::backtick($column[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function generateColumns(): string
|
private function generateColumns(): string
|
||||||
|
|||||||
@@ -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,64 +50,22 @@ 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
while (key($data)) {
|
foreach ($relations as $relation => $relationType) {
|
||||||
$key = key($data);
|
|
||||||
$value = current($data);
|
|
||||||
$relation = key($relations);
|
|
||||||
|
|
||||||
if (strpos($key, '__') === false) {
|
|
||||||
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
|
|
||||||
|
|
||||||
if (method_exists($model, $method) && isset($value)) {
|
|
||||||
$model->$method($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
next($data);
|
|
||||||
} else if (isset($modelKey) && substr($key, 0, strlen($modelKey . '__')) === $modelKey . '__') {
|
|
||||||
$key = substr($key, strlen($modelKey) + 2);
|
|
||||||
|
|
||||||
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
|
|
||||||
|
|
||||||
if (method_exists($model, $method) && isset($value)) {
|
|
||||||
$model->$method($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
next($data);
|
|
||||||
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
|
|
||||||
$relationType = current($relations);
|
|
||||||
$relationModel = new $relationType();
|
|
||||||
$this->fillWithData($data, $relationModel, $withRelations, $relation);
|
|
||||||
|
|
||||||
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
|
|
||||||
$model->$method($relationModel);
|
|
||||||
|
|
||||||
next($relations);
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$model->saveSnapshot();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function loadRelationsFromDb(Model $model, bool $recursive): void
|
|
||||||
{
|
|
||||||
foreach ($model::getRelations() as $relation => $relationType) {
|
|
||||||
$camel = str_replace('_', '', ucwords($relation, '_'));
|
$camel = str_replace('_', '', ucwords($relation, '_'));
|
||||||
|
|
||||||
$methodGet = 'get' . $camel . 'Id';
|
$methodGet = 'get' . $camel . 'Id';
|
||||||
@@ -103,7 +74,7 @@ class PersistentDataManager
|
|||||||
$relationId = $model->$methodGet();
|
$relationId = $model->$methodGet();
|
||||||
|
|
||||||
if ($relationId !== null) {
|
if ($relationId !== null) {
|
||||||
$relationModel = $this->selectFromDbById($relationId, $relationType, $recursive);
|
$relationModel = $this->selectFromDbById($relationId, $relationType, $recursive, $withRelations);
|
||||||
|
|
||||||
$model->$methodSet($relationModel);
|
$model->$methodSet($relationModel);
|
||||||
}
|
}
|
||||||
@@ -117,7 +88,7 @@ class PersistentDataManager
|
|||||||
$modified = $model->toArray();
|
$modified = $model->toArray();
|
||||||
$id = $model->getId();
|
$id = $model->getId();
|
||||||
|
|
||||||
$modify = new Modify(\Container::$dbConnection, $model::getTable(), \Container::$auditLogger);
|
$modify = new Modify($this->dbConnection, $model::getTable(), $this->auditLogger);
|
||||||
|
|
||||||
if ($id !== null) {
|
if ($id !== null) {
|
||||||
$original = $model->getSnapshot();
|
$original = $model->getSnapshot();
|
||||||
@@ -149,7 +120,7 @@ class PersistentDataManager
|
|||||||
|
|
||||||
public function deleteFromDb(Model $model): void
|
public function deleteFromDb(Model $model): void
|
||||||
{
|
{
|
||||||
$modify = new Modify(\Container::$dbConnection, $model::getTable(), \Container::$auditLogger);
|
$modify = new Modify($this->dbConnection, $model::getTable(), $this->auditLogger);
|
||||||
$modify->setId($model->getId());
|
$modify->setId($model->getId());
|
||||||
$modify->fill($model->toArray());
|
$modify->fill($model->toArray());
|
||||||
$modify->delete();
|
$modify->delete();
|
||||||
@@ -174,10 +145,10 @@ class PersistentDataManager
|
|||||||
if ($useRelations) {
|
if ($useRelations) {
|
||||||
$relations = call_user_func([$type, 'getRelations']);
|
$relations = call_user_func([$type, 'getRelations']);
|
||||||
if (count($withRelations)) {
|
if (count($withRelations)) {
|
||||||
$relations = array_intersect($relations, $withRelations);
|
$relations = array_intersect_key($relations, array_flip($withRelations));
|
||||||
}
|
}
|
||||||
|
|
||||||
$columns = array_merge($columns, $this->getRelationColumns($relations, $withRelations));
|
$columns = array_merge($columns, $this->getRelationColumns($table, $relations, $withRelations));
|
||||||
|
|
||||||
$this->leftJoinRelations($select, $table, $relations, $withRelations);
|
$this->leftJoinRelations($select, $table, $relations, $withRelations);
|
||||||
$select->columns($columns);
|
$select->columns($columns);
|
||||||
@@ -188,21 +159,22 @@ class PersistentDataManager
|
|||||||
return $select;
|
return $select;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getRelationColumns(array $relations, array $withRelations): array
|
private function getRelationColumns(string $table, array $relations, array $withRelations): array
|
||||||
{
|
{
|
||||||
$columns = [];
|
$columns = [];
|
||||||
|
|
||||||
foreach ($relations as $relation => $relationType) {
|
foreach ($relations as $relation => $relationType) {
|
||||||
|
$relationTableAlias = $table . '__' . $relation;
|
||||||
$relationTable = call_user_func([$relationType, 'getTable']);
|
$relationTable = call_user_func([$relationType, 'getTable']);
|
||||||
foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
|
foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
|
||||||
$columns[] = [$relationTable, $relationField, $relation . '__' . $relationField];
|
$columns[] = [$relationTableAlias, $relationField, $relation . '__' . $relationField];
|
||||||
}
|
}
|
||||||
|
|
||||||
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
|
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
||||||
if (count($withRelations)) {
|
if (count($withRelations)) {
|
||||||
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
|
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
|
||||||
}
|
}
|
||||||
$columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations, $withRelations));
|
$columns = array_merge($columns, $this->getRelationColumns($relationTable, $relationsOfRelation, $withRelations));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $columns;
|
return $columns;
|
||||||
@@ -211,17 +183,70 @@ class PersistentDataManager
|
|||||||
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
|
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
|
||||||
{
|
{
|
||||||
foreach ($relations as $relation => $relationType) {
|
foreach ($relations as $relation => $relationType) {
|
||||||
|
$relationTableAlias = $table . '__' . $relation;
|
||||||
$relationTable = call_user_func([$relationType, 'getTable']);
|
$relationTable = call_user_func([$relationType, 'getTable']);
|
||||||
$select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']);
|
$select->setTableAliases([$relationTableAlias => $relationTable]);
|
||||||
|
$select->leftJoin($relationTableAlias, [$relationTableAlias, 'id'], '=', [$table, $relation . '_id']);
|
||||||
|
|
||||||
$nextOrderRelations = call_user_func([$relationType, 'getRelations']);
|
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
||||||
if (count($withRelations)) {
|
if (count($withRelations)) {
|
||||||
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations);
|
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
|
||||||
}
|
}
|
||||||
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations, $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)) {
|
||||||
|
$key = key($data);
|
||||||
|
$value = current($data);
|
||||||
|
$relation = key($relations);
|
||||||
|
|
||||||
|
if (strpos($key, '__') === false) {
|
||||||
|
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
|
||||||
|
|
||||||
|
if (method_exists($model, $method) && isset($value)) {
|
||||||
|
$model->$method($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
next($data);
|
||||||
|
} else if (isset($modelKey) && substr($key, 0, strlen($modelKey . '__')) === $modelKey . '__') {
|
||||||
|
$key = substr($key, strlen($modelKey) + 2);
|
||||||
|
|
||||||
|
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
|
||||||
|
|
||||||
|
if (method_exists($model, $method) && isset($value)) {
|
||||||
|
$model->$method($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
next($data);
|
||||||
|
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
|
||||||
|
if ($data[$relation . '__id'] !== null) {
|
||||||
|
$relationType = current($relations);
|
||||||
|
$relationModel = new $relationType();
|
||||||
|
$this->fillWithData($data, $relationModel, $withRelations, $relation);
|
||||||
|
|
||||||
|
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
|
||||||
|
$model->$method($relationModel);
|
||||||
|
} else {
|
||||||
|
next($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
next($relations);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$model->saveSnapshot();
|
||||||
|
}
|
||||||
|
|
||||||
private function syncRelations(Model $model): void
|
private function syncRelations(Model $model): void
|
||||||
{
|
{
|
||||||
foreach ($model::getRelations() as $relation => $relationType) {
|
foreach ($model::getRelations() as $relation => $relationType) {
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ class HttpResponse
|
|||||||
list($route, $params) = $match;
|
list($route, $params) = $match;
|
||||||
$this->request->setParsedRouteParams($params);
|
$this->request->setParsedRouteParams($params);
|
||||||
$handler = $route->getHandler();
|
$handler = $route->getHandler();
|
||||||
$controller = new $handler[0]($this->request);
|
$controller = new $handler[0]();
|
||||||
|
|
||||||
if (
|
if (
|
||||||
$controller instanceof IAuthenticationRequired &&
|
$controller instanceof IAuthenticationRequired &&
|
||||||
@@ -103,7 +103,7 @@ class HttpResponse
|
|||||||
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
|
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
|
||||||
$response->render();
|
$response->render();
|
||||||
} elseif ($response instanceof IRedirect) {
|
} elseif ($response instanceof IRedirect) {
|
||||||
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
|
header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode());
|
||||||
} else {
|
} else {
|
||||||
$this->render404();
|
$this->render404();
|
||||||
}
|
}
|
||||||
@@ -113,7 +113,7 @@ class HttpResponse
|
|||||||
{
|
{
|
||||||
$this->request->session()->set('redirect_after_login', $this->rawUrl);
|
$this->request->session()->set('redirect_after_login', $this->rawUrl);
|
||||||
$response = new Redirect($this->routeCollection->getRoute($this->appConfig['loginRouteId'])->generateLink(), IRedirect::TEMPORARY);
|
$response = new Redirect($this->routeCollection->getRoute($this->appConfig['loginRouteId'])->generateLink(), IRedirect::TEMPORARY);
|
||||||
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
|
header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
private function renderAntiCsrfError(): void
|
private function renderAntiCsrfError(): void
|
||||||
@@ -142,4 +142,13 @@ class HttpResponse
|
|||||||
header('Content-Type: text/html; charset=UTF-8', true, 500);
|
header('Content-Type: text/html; charset=UTF-8', true, 500);
|
||||||
$content->render();
|
$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;
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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'));
|
||||||
|
|||||||
@@ -16,10 +16,12 @@ class Container
|
|||||||
{
|
{
|
||||||
static SokoWeb\Interfaces\Database\IConnection $dbConnection;
|
static SokoWeb\Interfaces\Database\IConnection $dbConnection;
|
||||||
static SokoWeb\Interfaces\Database\IAuditLogger $auditLogger;
|
static SokoWeb\Interfaces\Database\IAuditLogger $auditLogger;
|
||||||
static SokoWeb\Routing\RouteCollection $routeCollection;
|
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::$auditLogger = new {app}\Database\AuditLogger(Container::$dbConnection, 'audit_log');
|
||||||
|
Container::$persistentDataManager = new SokoWeb\PersistentData\PersistentDataManager(Container::$dbConnection, Container::$auditLogger);
|
||||||
@@ -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