Compare commits

..

No commits in common. "5e0579463cf0f4203c46e0d4f9c09cd283dbf0b8" and "3893ed22316e84aa4ffab7fbb3d5e823b928b001" have entirely different histories.

13 changed files with 34 additions and 103 deletions

View File

@ -3,3 +3,12 @@
require 'vendor/autoload.php';
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;
}

View File

@ -1,20 +0,0 @@
<?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);
public function loadRelationsFromDb(Model $model, bool $recursive): void;
public function saveToDb(Model $model): void;
public function deleteFromDb(Model $model): void;
}

View File

@ -6,7 +6,7 @@ interface IRedirect
const TEMPORARY = 2;
public function getTarget(): string;
public function getUrl(): string;
public function getHttpCode(): int;
}

View File

@ -1,12 +0,0 @@
<?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;
}

View File

@ -1,16 +0,0 @@
<?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;
}

View File

@ -3,24 +3,11 @@
use Generator;
use SokoWeb\Database\Query\Modify;
use SokoWeb\Database\Query\Select;
use SokoWeb\Interfaces\Database\IConnection;
use SokoWeb\Interfaces\Database\IAuditLogger;
use SokoWeb\Interfaces\Database\IResultSet;
use SokoWeb\Interfaces\PersistentData\IPersistentDataManager;
use SokoWeb\PersistentData\Model\Model;
class PersistentDataManager implements IPersistentDataManager
class PersistentDataManager
{
private IConnection $dbConnection;
private IAuditLogger $auditLogger;
public function __construct(IConnection $dbConnection, IAuditLogger $auditLogger)
{
$this->dbConnection = $dbConnection;
$this->auditLogger = $auditLogger;
}
public function selectFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = [])
{
$select = $this->createSelect($select, $type, $useRelations, $withRelations);
@ -52,7 +39,7 @@ class PersistentDataManager implements IPersistentDataManager
public function selectFromDbById($id, string $type, bool $useRelations = false)
{
$select = new Select($this->dbConnection);
$select = new Select(\Container::$dbConnection);
$select->whereId($id);
return $this->selectFromDb($select, $type, $useRelations);
@ -130,7 +117,7 @@ class PersistentDataManager implements IPersistentDataManager
$modified = $model->toArray();
$id = $model->getId();
$modify = new Modify($this->dbConnection, $model::getTable(), $this->auditLogger);
$modify = new Modify(\Container::$dbConnection, $model::getTable(), \Container::$auditLogger);
if ($id !== null) {
$original = $model->getSnapshot();
@ -162,7 +149,7 @@ class PersistentDataManager implements IPersistentDataManager
public function deleteFromDb(Model $model): void
{
$modify = new Modify($this->dbConnection, $model::getTable(), $this->auditLogger);
$modify = new Modify(\Container::$dbConnection, $model::getTable(), \Container::$auditLogger);
$modify->setId($model->getId());
$modify->fill($model->toArray());
$modify->delete();

View File

@ -64,7 +64,7 @@ class HttpResponse
list($route, $params) = $match;
$this->request->setParsedRouteParams($params);
$handler = $route->getHandler();
$controller = new $handler[0]();
$controller = new $handler[0]($this->request);
if (
$controller instanceof IAuthenticationRequired &&
@ -103,7 +103,7 @@ class HttpResponse
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
$response->render();
} elseif ($response instanceof IRedirect) {
header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode());
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
} else {
$this->render404();
}
@ -113,7 +113,7 @@ class HttpResponse
{
$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());
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
}
private function renderAntiCsrfError(): void
@ -142,13 +142,4 @@ class HttpResponse
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;
}
}

View File

@ -14,9 +14,15 @@ class Redirect implements IRedirect
$this->type = $type;
}
public function getTarget(): string
public function getUrl(): string
{
return $this->target;
if (preg_match('/^http(s)?/', $this->target) === 1) {
$link = $this->target;
} else {
$link = \Container::$request->getBase() . $this->target;
}
return $link;
}
public function getHttpCode(): int

View File

@ -1,8 +1,6 @@
<?php namespace SokoWeb\Routing;
use SokoWeb\Interfaces\Routing\IRoute;
class Route implements IRoute
class Route
{
private string $id;

View File

@ -1,10 +1,8 @@
<?php namespace SokoWeb\Routing;
use Closure;
use SokoWeb\Interfaces\Routing\IRoute;
use SokoWeb\Interfaces\Routing\IRouteCollection;
class RouteCollection implements IRouteCollection
class RouteCollection
{
private array $routes = [];
@ -34,7 +32,7 @@ class RouteCollection implements IRouteCollection
array_pop($this->groupStack);
}
public function getRoute(string $id): ?IRoute
public function getRoute(string $id): ?Route
{
if (!isset($this->routes[$id])) {
return null;

View File

@ -3,23 +3,15 @@
use DateTime;
use SokoWeb\Database\Query\Modify;
use SokoWeb\Database\Query\Select;
use SokoWeb\Interfaces\Database\IConnection;
use SokoWeb\Interfaces\Database\IResultSet;
use SokoWeb\Interfaces\Session\ISessionHandler;
class DatabaseSessionHandler implements ISessionHandler
{
private IConnection $dbConnection;
private bool $exists = false;
private bool $written = false;
public function __construct(IConnection $dbConnection)
{
$this->dbConnection = $dbConnection;
}
public function open($savePath, $sessionName): bool
{
return true;
@ -32,7 +24,7 @@ class DatabaseSessionHandler implements ISessionHandler
public function read($id): string
{
$select = new Select($this->dbConnection, 'sessions');
$select = new Select(\Container::$dbConnection, 'sessions');
$select->columns(['data']);
$select->whereId(substr($id, 0, 32));
@ -49,7 +41,7 @@ class DatabaseSessionHandler implements ISessionHandler
public function write($id, $data): bool
{
$modify = new Modify($this->dbConnection, 'sessions');
$modify = new Modify(\Container::$dbConnection, 'sessions');
if ($this->exists) {
$modify->setId(substr($id, 0, 32));
@ -68,7 +60,7 @@ class DatabaseSessionHandler implements ISessionHandler
public function destroy($id): bool
{
$modify = new Modify($this->dbConnection, 'sessions');
$modify = new Modify(\Container::$dbConnection, 'sessions');
$modify->setId(substr($id, 0, 32));
$modify->delete();
@ -101,7 +93,7 @@ class DatabaseSessionHandler implements ISessionHandler
return true;
}
$modify = new Modify($this->dbConnection, 'sessions');
$modify = new Modify(\Container::$dbConnection, 'sessions');
$modify->setId(substr($id, 0, 32));
$modify->set('updated', (new DateTime())->format('Y-m-d H:i:s'));

View File

@ -16,12 +16,10 @@ class Container
{
static SokoWeb\Interfaces\Database\IConnection $dbConnection;
static SokoWeb\Interfaces\Database\IAuditLogger $auditLogger;
static SokoWeb\Interfaces\PersistentData\IPersistentDataManager $persistentDataManager;
static SokoWeb\Interfaces\Routing\IRouteCollection $routeCollection;
static SokoWeb\Routing\RouteCollection $routeCollection;
static SokoWeb\Interfaces\Session\ISessionHandler $sessionHandler;
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::$auditLogger = new {app}\Database\AuditLogger(Container::$dbConnection, 'audit_log');
Container::$persistentDataManager = new SokoWeb\PersistentData\PersistentDataManager(Container::$dbConnection, Container::$auditLogger);

View File

@ -14,7 +14,7 @@ Container::$routeCollection = new SokoWeb\Routing\RouteCollection();
Container::$routeCollection->get('index', '', [{app}\Controller\HomeController::class, 'getIndex']);
if (isset($_COOKIE['COOKIES_CONSENT'])) {
Container::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler(Container::$dbConnection);
Container::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler();
session_set_save_handler(Container::$sessionHandler, true);
session_start([