Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78f891fbff
|
||
|
|
6989e1dcf3
|
||
|
|
1288a33ff6
|
||
|
|
a5bfc61db8
|
||
|
|
9637ebc52b
|
||
|
|
585d469b69
|
||
|
|
a17db2c79c
|
||
|
|
fd286c9cff
|
||
|
|
72fc78220f
|
||
|
|
0da1d00c3d
|
||
|
|
c57d1d40d4
|
||
|
|
6a35344210
|
||
|
|
9ade08d8bd
|
||
|
|
a982be6645
|
||
|
|
219b42f995
|
||
|
|
70a9e492e3
|
||
|
|
e9bfe4e4ad
|
||
|
|
cf329a20e5
|
||
|
|
88a2a99527
|
||
|
|
e37ea7c09c
|
||
|
|
ad7ea0de9d
|
||
|
|
445774e59a
|
||
|
|
7650b33cd2
|
||
|
|
5e0579463c
|
||
|
|
4fe463fcc5
|
||
|
|
0f87a9c6e3
|
No files matched your search
+2
-1
@@ -6,7 +6,8 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"vlucas/phpdotenv": "^5.5",
|
"vlucas/phpdotenv": "^5.5",
|
||||||
"symfony/console": "^5.4",
|
"symfony/console": "^5.4",
|
||||||
"phpmailer/phpmailer": "^6.8"
|
"phpmailer/phpmailer": "^6.8",
|
||||||
|
"cocur/slugify": "^4.3"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^9.6",
|
"phpunit/phpunit": "^9.6",
|
||||||
|
|||||||
Generated
+75
-1
@@ -4,8 +4,82 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "079e8a8443fdc25cbe3b03322494b73b",
|
"content-hash": "d873b245eaaf660d70c21e06ee4bcf0a",
|
||||||
"packages": [
|
"packages": [
|
||||||
|
{
|
||||||
|
"name": "cocur/slugify",
|
||||||
|
"version": "v4.3.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/cocur/slugify.git",
|
||||||
|
"reference": "652234ef5f1be844a2ae1c36ad1b4c88b05160f9"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/cocur/slugify/zipball/652234ef5f1be844a2ae1c36ad1b4c88b05160f9",
|
||||||
|
"reference": "652234ef5f1be844a2ae1c36ad1b4c88b05160f9",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"ext-mbstring": "*",
|
||||||
|
"php": "^7.1 || ~8.0.0 || ~8.1.0 || ~8.2.0"
|
||||||
|
},
|
||||||
|
"conflict": {
|
||||||
|
"symfony/config": "<3.4 || >=4,<4.3",
|
||||||
|
"symfony/dependency-injection": "<3.4 || >=4,<4.3",
|
||||||
|
"symfony/http-kernel": "<3.4 || >=4,<4.3",
|
||||||
|
"twig/twig": "<2.12.1"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"laravel/framework": "^5.0|^6.0|^7.0|^8.0",
|
||||||
|
"latte/latte": "~2.2",
|
||||||
|
"league/container": "^2.2.0",
|
||||||
|
"mikey179/vfsstream": "~1.6.8",
|
||||||
|
"mockery/mockery": "^1.3",
|
||||||
|
"nette/di": "~2.4",
|
||||||
|
"pimple/pimple": "~1.1",
|
||||||
|
"plumphp/plum": "~0.1",
|
||||||
|
"symfony/config": "^3.4 || ^4.3 || ^5.0 || ^6.0",
|
||||||
|
"symfony/dependency-injection": "^3.4 || ^4.3 || ^5.0 || ^6.0",
|
||||||
|
"symfony/http-kernel": "^3.4 || ^4.3 || ^5.0 || ^6.0",
|
||||||
|
"symfony/phpunit-bridge": "^5.4 || ^6.0",
|
||||||
|
"twig/twig": "^2.12.1 || ~3.0",
|
||||||
|
"zendframework/zend-modulemanager": "~2.2",
|
||||||
|
"zendframework/zend-servicemanager": "~2.2",
|
||||||
|
"zendframework/zend-view": "~2.2"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Cocur\\Slugify\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Florian Eckerstorfer",
|
||||||
|
"email": "florian@eckerstorfer.co",
|
||||||
|
"homepage": "https://florian.ec"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ivo Bathke",
|
||||||
|
"email": "ivo.bathke@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Converts a string into a slug.",
|
||||||
|
"keywords": [
|
||||||
|
"slug",
|
||||||
|
"slugify"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/cocur/slugify/issues",
|
||||||
|
"source": "https://github.com/cocur/slugify/tree/v4.3.0"
|
||||||
|
},
|
||||||
|
"time": "2022-12-07T19:48:48+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "graham-campbell/result-type",
|
"name": "graham-campbell/result-type",
|
||||||
"version": "v1.1.1",
|
"version": "v1.1.1",
|
||||||
|
|||||||
@@ -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]);
|
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
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?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 selectFromDbBySlug(string $slug, 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;
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ use SokoWeb\Interfaces\Authentication\IUser;
|
|||||||
|
|
||||||
interface IRequest
|
interface IRequest
|
||||||
{
|
{
|
||||||
public function setParsedRouteParams(array &$routeParams): void;
|
public function setParsedRouteParams(array $routeParams): void;
|
||||||
|
|
||||||
public function getBase(): string;
|
public function getBase(): string;
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -41,7 +41,7 @@ abstract class Model
|
|||||||
{
|
{
|
||||||
$array = [];
|
$array = [];
|
||||||
|
|
||||||
foreach (self::getFields() as $key) {
|
foreach (static::getFields() as $key) {
|
||||||
$method = 'get' . str_replace('_', '', ucwords($key, '_'));
|
$method = 'get' . str_replace('_', '', ucwords($key, '_'));
|
||||||
|
|
||||||
if (method_exists($this, $method)) {
|
if (method_exists($this, $method)) {
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php namespace SokoWeb\PersistentData\Model;
|
||||||
|
|
||||||
|
use Cocur\Slugify\Slugify;
|
||||||
|
|
||||||
|
abstract class ModelWithSlug extends Model
|
||||||
|
{
|
||||||
|
protected static string $slugSource;
|
||||||
|
|
||||||
|
protected ?string $slug = null;
|
||||||
|
|
||||||
|
public static function getFields(): array
|
||||||
|
{
|
||||||
|
return array_merge(['id', 'slug'], static::$fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setSlug(?string $slug): void
|
||||||
|
{
|
||||||
|
$this->slug = $slug;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSlug(): ?string
|
||||||
|
{
|
||||||
|
return $this->slug;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateSlug(): string
|
||||||
|
{
|
||||||
|
$slugSourceGetMethod = 'get' . str_replace('_', '', ucwords(static::$slugSource, '_'));
|
||||||
|
$this->slug = Slugify::create()->slugify($this->$slugSourceGetMethod());
|
||||||
|
return $this->slug;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,11 +3,27 @@
|
|||||||
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;
|
||||||
|
use SokoWeb\PersistentData\Model\ModelWithSlug;
|
||||||
|
|
||||||
class PersistentDataManager
|
class PersistentDataManager implements IPersistentDataManager
|
||||||
{
|
{
|
||||||
|
const SLUG_MAX_LENGTH = 255;
|
||||||
|
|
||||||
|
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 +53,197 @@ 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 selectFromDbBySlug(string $slug, string $type, bool $useRelations = false, array $withRelations = [])
|
||||||
|
{
|
||||||
|
$select = new Select($this->dbConnection);
|
||||||
|
$select->where('slug', '=', $slug);
|
||||||
|
|
||||||
|
return $this->selectFromDb($select, $type, $useRelations, $withRelations);
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
if ($model instanceof ModelWithSlug && isset($modified['slug'])) {
|
||||||
|
$modified['slug'] = $this->generateUniqueSlug($model, $modified['slug']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$modify->setId($id);
|
||||||
|
$modify->setDiff($diff);
|
||||||
|
$modify->fill($modified);
|
||||||
|
$modify->save();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ($model instanceof ModelWithSlug) {
|
||||||
|
$slug = $model->generateSlug();
|
||||||
|
$modified['slug'] = $this->generateUniqueSlug($model, $slug);
|
||||||
|
}
|
||||||
|
|
||||||
|
$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 generateUniqueSlug(ModelWithSlug $model, string $notUniqueSlug): string
|
||||||
|
{
|
||||||
|
$numbered = 1;
|
||||||
|
|
||||||
|
do {
|
||||||
|
if ($numbered > 1) {
|
||||||
|
$slug = substr($notUniqueSlug, 0, static::SLUG_MAX_LENGTH - (strlen((string)$numbered) + 1));
|
||||||
|
$slug = $notUniqueSlug . '_' . (string)$numbered;
|
||||||
|
} else {
|
||||||
|
$slug = substr($notUniqueSlug, 0, static::SLUG_MAX_LENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
$select = new Select($this->dbConnection, $model::getTable());
|
||||||
|
$select->where('slug', '=', $slug);
|
||||||
|
|
||||||
|
$numbered++;
|
||||||
|
} while ($select->count() != 0);
|
||||||
|
|
||||||
|
$model->setSlug($slug);
|
||||||
|
|
||||||
|
return $slug;
|
||||||
|
}
|
||||||
|
|
||||||
|
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, $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, string $tableAlias, 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'], '=', [$tableAlias, $relation . '_id']);
|
||||||
|
|
||||||
|
$relationsOfRelation = call_user_func([$relationType, 'getRelations']);
|
||||||
|
if (count($withRelations)) {
|
||||||
|
$relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
|
||||||
|
}
|
||||||
|
$this->leftJoinRelations($select, $relationTable, $relationTableAlias, $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 +270,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,136 +290,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(), \Container::$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(\Container::$dbConnection, $model::getTable(), \Container::$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($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) {
|
||||||
|
|||||||
@@ -17,23 +17,23 @@ class Request implements IRequest
|
|||||||
|
|
||||||
private array $headers;
|
private array $headers;
|
||||||
|
|
||||||
private Session $session;
|
private ISession $session;
|
||||||
|
|
||||||
private ?IUser $user = null;
|
private ?IUser $user = null;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
string $base,
|
string $base,
|
||||||
array &$get,
|
array $get,
|
||||||
array &$post,
|
array $post,
|
||||||
array $headers,
|
array $headers,
|
||||||
array &$session,
|
ISession $session,
|
||||||
IUserRepository $userRepository)
|
IUserRepository $userRepository)
|
||||||
{
|
{
|
||||||
$this->base = $base;
|
$this->base = $base;
|
||||||
$this->get = &$get;
|
$this->get = $get;
|
||||||
$this->post = &$post;
|
$this->post = $post;
|
||||||
$this->headers = $headers;
|
$this->headers = $headers;
|
||||||
$this->session = new Session($session);
|
$this->session = $session;
|
||||||
|
|
||||||
$userId = $this->session->get('userId');
|
$userId = $this->session->get('userId');
|
||||||
if ($userId !== null) {
|
if ($userId !== null) {
|
||||||
@@ -41,9 +41,9 @@ class Request implements IRequest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setParsedRouteParams(array &$routeParams): void
|
public function setParsedRouteParams(array $routeParams): void
|
||||||
{
|
{
|
||||||
$this->routeParams = &$routeParams;
|
$this->routeParams = $routeParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBase(): string
|
public function getBase(): string
|
||||||
|
|||||||
@@ -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,29 @@
|
|||||||
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 string $table;
|
||||||
|
|
||||||
|
private DateTime $shouldBeNewerThan;
|
||||||
|
|
||||||
private bool $exists = false;
|
private bool $exists = false;
|
||||||
|
|
||||||
private bool $written = false;
|
private bool $written = false;
|
||||||
|
|
||||||
|
public function __construct(IConnection $dbConnection, string $table, DateTime $shouldBeNewerThan)
|
||||||
|
{
|
||||||
|
$this->dbConnection = $dbConnection;
|
||||||
|
$this->table = $table;
|
||||||
|
$this->shouldBeNewerThan = $shouldBeNewerThan;
|
||||||
|
}
|
||||||
|
|
||||||
public function open($savePath, $sessionName): bool
|
public function open($savePath, $sessionName): bool
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
@@ -24,24 +38,27 @@ 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, $this->table);
|
||||||
$select->columns(['data']);
|
$select->columns(['data', 'updated']);
|
||||||
$select->whereId(substr($id, 0, 32));
|
$select->whereId(substr($id, 0, 32));
|
||||||
|
|
||||||
$result = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
$result = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
||||||
|
|
||||||
if ($result === null) {
|
if ($result === null) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->exists = true;
|
$this->exists = true;
|
||||||
|
|
||||||
|
if (new DateTime($result['updated']) < $this->shouldBeNewerThan) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
return $result['data'];
|
return $result['data'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function write($id, $data): bool
|
public function write($id, $data): bool
|
||||||
{
|
{
|
||||||
$modify = new Modify(\Container::$dbConnection, 'sessions');
|
$modify = new Modify($this->dbConnection, $this->table);
|
||||||
|
|
||||||
if ($this->exists) {
|
if ($this->exists) {
|
||||||
$modify->setId(substr($id, 0, 32));
|
$modify->setId(substr($id, 0, 32));
|
||||||
@@ -60,7 +77,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, $this->table);
|
||||||
$modify->setId(substr($id, 0, 32));
|
$modify->setId(substr($id, 0, 32));
|
||||||
$modify->delete();
|
$modify->delete();
|
||||||
|
|
||||||
@@ -93,7 +110,7 @@ class DatabaseSessionHandler implements ISessionHandler
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$modify = new Modify(\Container::$dbConnection, 'sessions');
|
$modify = new Modify($this->dbConnection, $this->table);
|
||||||
|
|
||||||
$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, 'sessions');
|
||||||
|
|
||||||
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