Compare commits

...

63 Commits
v0.5 ... master

Author SHA1 Message Date
4b089b4e84
modern handling of http request
Some checks failed
soko-web/pipeline/head There was a failure building this commit
2025-03-13 23:41:13 +01:00
d78a82c14c
sync timezone for mysql session
All checks were successful
soko-web/pipeline/head This commit looks good
2025-02-20 22:54:12 +01:00
d504f1d5bb
encode and decode parameters in routes
All checks were successful
soko-web/pipeline/head This commit looks good
2024-11-08 12:21:54 +01:00
5534f10cee
use RFC3986 for query parameter encoding 2024-11-08 12:21:13 +01:00
c1fe1bb0e0
do not encode query parameters 2024-11-08 12:20:30 +01:00
ee7d9623a3
set redirect_after_login in query parameter as well
All checks were successful
soko-web/pipeline/head This commit looks good
2024-10-24 22:15:22 +02:00
ecec258a64
allow starttls and smtps for mailing 2024-10-19 22:44:51 +02:00
66040d69db
use IRouteCollection for HttpResponse
All checks were successful
soko-web/pipeline/head This commit looks good
2024-10-18 18:18:13 +02:00
fc2de8e1ab
do not use $_ENV vars for mail template 2024-10-18 18:05:53 +02:00
6cb90d5ea2
Merge pull request 'implement cors' (#30) from implement-cors into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #30
2024-08-02 02:07:28 +02:00
e67afc401b
implement cors
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2024-08-02 01:42:30 +02:00
e59d627080
Merge pull request 'feature/update-to-php81' (#29) from feature/update-to-php81 into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #29
2023-09-27 22:11:05 +02:00
3acca19d49
use new interface of TestCase
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-09-27 22:05:56 +02:00
2226b88a88
adapt signature of DatabaseSessionHandler::gc to the parent class
Some checks failed
soko-web/pipeline/pr-master There was a failure building this commit
2023-09-27 21:46:32 +02:00
8e08b09ae8
generate composer.lock
Some checks failed
soko-web/pipeline/pr-master There was a failure building this commit
2023-09-27 21:41:17 +02:00
a3bce1f2aa
update composer packages 2023-09-27 21:38:59 +02:00
a84d3a3976
update to php 8.1 2023-09-27 21:35:44 +02:00
7210b24aa3
Merge pull request 'log erros that were already caught' (#28) from feature/error-log into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #28
2023-09-27 00:26:55 +02:00
2d48f20aed
log erros that were already caught
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-09-27 00:04:26 +02:00
ebe1fa2aa6
Merge pull request 'lazy create mysql connecion' (#27) from feature/lazy-create-mysql-connection into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #27
2023-09-16 23:56:41 +02:00
bccee89c13
lazy create mysql connecion
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-09-16 13:18:42 +02:00
8bf495c89b
Merge pull request 'where should accept $relation as null' (#26) from bugfix/fix-where-closure into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #26
2023-07-08 14:38:40 +02:00
74cb576a2e
where should accept $relation as null
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-07-08 14:36:22 +02:00
4283bc9bb1
Merge pull request 'implement multi relation loading' (#25) from feature/multiple-relations into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #25
2023-06-17 14:32:56 +02:00
fc4c3234a7
implement multi relation loading
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-06-17 14:15:46 +02:00
8d490e48aa
Merge pull request 'RVRNEXT-31 change logic of checking external assets' (#24) from bugfix/RVRNEXT-31-allow-special-characters-for-local-assets into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #24
2023-05-28 21:13:20 +02:00
1dcab1abe2
RVRNEXT-31 change logic of checking external assets
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-05-28 21:00:43 +02:00
bda12177eb
Merge pull request 'Revert "fix the case when relation is set to null"' (#23) from bugfix/fix-syncrelations into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #23
2023-05-28 15:56:14 +02:00
f037de014e
Revert "fix the case when relation is set to null"
All checks were successful
soko-web/pipeline/pr-master This commit looks good
This reverts commit 25548176bbd12318097a09300380ab9d44299cc3.
2023-05-28 15:54:54 +02:00
8a1820275d
Merge pull request 'iterate in $data until fields for empty relation are over' (#22) from bugfix/fix-model-loading-when-left-join-is-empty into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #22
2023-05-28 15:48:31 +02:00
da2801560d
iterate in $data until fields for empty relation are over
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-05-28 15:47:35 +02:00
dd855a6d6b
Merge pull request 'fix the case when relation is set to null' (#21) from bugfix/fix-when-relation-is-set-to-null into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #21
2023-05-28 15:47:27 +02:00
25548176bb
fix the case when relation is set to null
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-05-28 15:46:13 +02:00
f31b801c03
Merge pull request 'generated unique slug should be part of diff' (#20) from bugfix/generated-unique-slug-should-be-part-of-diff into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #20
2023-05-28 15:45:58 +02:00
346b1a0ca9
generated unique slug should be part of diff
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-05-28 15:45:19 +02:00
6fdd25ae4b
Merge pull request 'MAPG-238 include view with @include' (#19) from feature/MAPG-238-include-possibility-for-templating-engine into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #19
2023-05-28 15:45:10 +02:00
754a23706a
MAPG-238 include view with @include
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-05-27 10:56:20 +02:00
78f891fbff
Merge pull request 'feature/slug-handling' (#18) from feature/slug-handling into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #18
2023-05-07 01:52:34 +02:00
6989e1dcf3
call static instead of self in Model
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-05-07 01:51:20 +02:00
1288a33ff6
handle slugs in persistent data manager 2023-05-07 01:51:20 +02:00
a5bfc61db8
add class that handles model with slug 2023-05-07 01:51:20 +02:00
9637ebc52b
install cocur/slugify 2023-05-06 22:59:49 +02:00
585d469b69
Merge pull request 'use the correct table alias in joins 'on clause'' (#17) from bugfix/fix-multiple-level-join-table-alias into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #17
2023-05-02 18:06:33 +02:00
a17db2c79c
use the correct table alias in joins 'on clause'
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-05-02 18:04:35 +02:00
fd286c9cff
Merge pull request 'check session validity by DatabaseSessionHandler' (#16) from feature/sessions-should-expire into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #16
2023-05-02 12:55:47 +02:00
72fc78220f
check session validity by DatabaseSessionHandler
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-05-02 12:37:31 +02:00
0da1d00c3d
Merge pull request 'feature/implement-separate-remember-me' (#15) from feature/implement-separate-remember-me into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #15
2023-05-02 12:08:16 +02:00
c57d1d40d4
fixup! remove unnecessary "pass by reference" variables from Request
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-05-02 12:02:30 +02:00
6a35344210
pass Session object to Request
Some checks failed
soko-web/pipeline/pr-master There was a failure building this commit
2023-05-02 11:47:08 +02:00
9ade08d8bd
session handler should receive table name in the constructor 2023-05-02 10:52:22 +02:00
a982be6645
remove unnecessary "pass by reference" variables from Request 2023-05-02 10:36:04 +02:00
219b42f995
Merge pull request 'feature/use-the-same-table-in-multiple-joins' (#14) from feature/use-the-same-table-in-multiple-joins into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #14
2023-05-01 19:08:22 +02:00
70a9e492e3
make left joins really work
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-05-01 18:56:36 +02:00
e9bfe4e4ad
use the table name is column names if table is not specified 2023-05-01 18:56:36 +02:00
cf329a20e5
make it possible to use the same table in multiple joins 2023-05-01 18:56:36 +02:00
88a2a99527
Merge pull request 'feature/withrelations-should-contain-names-instead-of-types' (#13) from feature/withrelations-should-contain-names-instead-of-types into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #13
2023-04-30 20:20:27 +02:00
e37ea7c09c
make PersistentDataManager::$fillWithData private
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-04-30 19:21:04 +02:00
ad7ea0de9d
withRelations in methods of PersistentDataManager should contain relation names instead of types 2023-04-30 19:19:00 +02:00
445774e59a
Merge pull request 'make auditlogger optional in persistent data manager' (#12) from bugfix/make-auditlogger-optional into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #12
2023-04-20 00:24:31 +02:00
7650b33cd2
make auditlogger optional in persistent data manager
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-04-20 00:23:14 +02:00
5e0579463c
Merge pull request 'feature/get-rid-of-container-usage' (#11) from feature/get-rid-of-container-usage into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #11
2023-04-19 23:35:03 +02:00
4fe463fcc5
do not use app container in classes
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-04-19 23:26:24 +02:00
0f87a9c6e3
add interfaces for route and route collection 2023-04-19 22:15:49 +02:00
27 changed files with 1026 additions and 835 deletions

View File

@ -1,4 +1,4 @@
FROM php:7.4.7-cli-buster
FROM php:8.1-cli-bookworm
RUN apt-get update && apt-get install -y unzip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

View File

@ -5,11 +5,12 @@
"license": "GNU GPL 3.0",
"require": {
"vlucas/phpdotenv": "^5.5",
"symfony/console": "^5.4",
"phpmailer/phpmailer": "^6.8"
"symfony/console": "^6.3",
"phpmailer/phpmailer": "^6.8",
"cocur/slugify": "^4.5"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"phpunit/phpunit": "^10.3",
"phpstan/phpstan": "^1.10"
},
"autoload": {

994
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -3,12 +3,3 @@
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

@ -4,51 +4,57 @@ use SokoWeb\Interfaces\Database\IConnection;
use SokoWeb\Interfaces\Database\IResultSet;
use SokoWeb\Interfaces\Database\IStatement;
use mysqli;
use DateTime;
use DateTimeZone;
class Connection implements IConnection
{
private mysqli $connection;
private string $host;
private string $user;
private string $password;
private string $db;
private int $port;
private string $socket;
private ?mysqli $connection = null;
public function __construct(string $host, string $user, string $password, string $db, int $port = -1, string $socket = null)
{
if ($port < 0) {
$port = (int) ini_get('mysqli.default_port');
}
if ($socket === null) {
$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->set_charset('utf8mb4');
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->db = $db;
$this->port = $port < 0 ? (int) ini_get('mysqli.default_port') : $port;
$this->socket = $socket === null ? (string) ini_get('mysqli.default_socket') : $socket;
}
public function __destruct()
{
if ($this->connection === null) {
return;
}
$this->connection->close();
}
public function startTransaction(): void
{
$this->connection->autocommit(false);
$this->getConnection()->autocommit(false);
}
public function commit(): void
{
$this->connection->commit();
$this->connection->autocommit(true);
$this->getConnection()->commit();
$this->getConnection()->autocommit(true);
}
public function rollback(): void
{
$this->connection->rollback();
$this->connection->autocommit(true);
$this->getConnection()->rollback();
$this->getConnection()->autocommit(true);
}
public function query(string $query): ?IResultSet
{
$result = $this->connection->query($query);
$result = $this->getConnection()->query($query);
if ($result !== true) {
return new ResultSet($result);
@ -59,36 +65,62 @@ class Connection implements IConnection
public function multiQuery(string $query): array
{
$this->connection->multi_query($query);
$this->getConnection()->multi_query($query);
$ret = [];
do {
if ($result = $this->connection->store_result()) {
if ($result = $this->getConnection()->store_result()) {
$ret[] = new ResultSet($result);
} else {
$ret[] = null;
}
$this->connection->more_results();
} while ($this->connection->next_result());
$this->getConnection()->more_results();
} while ($this->getConnection()->next_result());
return $ret;
}
public function prepare(string $query): IStatement
{
$stmt = $this->connection->prepare($query);
$stmt = $this->getConnection()->prepare($query);
return new Statement($stmt);
}
public function lastId(): int
{
return $this->connection->insert_id;
return $this->getConnection()->insert_id;
}
public function getAffectedRows(): int
{
return $this->connection->affected_rows;
return $this->getConnection()->affected_rows;
}
private function getConnection(): mysqli
{
if ($this->connection === null) {
$this->createConnection();
}
return $this->connection;
}
private function createConnection(): void
{
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$this->connection = new mysqli($this->host, $this->user, $this->password, $this->db, $this->port, $this->socket);
$this->connection->set_charset('utf8mb4');
$this->connection->query('SET time_zone = \'' . $this->getTimeZone() . '\'');
}
private function getTimeZone(): string {
$tz = new DateTimeZone(date_default_timezone_get());
$offset = $tz->getOffset(new DateTime('now', new DateTimeZone('UTC')));
$hours = intdiv($offset, 3600);
$minutes = abs(($offset % 3600) / 60);
return sprintf("%+03d:%02d", $hours, $minutes);
}
}

View File

@ -97,28 +97,28 @@ class Select
return $this;
}
public function where($column, string $relation = null, $value = null): Select
public function where($column, ?string $relation = null, $value = null): Select
{
$this->addWhereCondition('AND', $column, $relation, $value);
return $this;
}
public function orWhere($column, string $relation = null, $value = null): Select
public function orWhere($column, ?string $relation = null, $value = null): Select
{
$this->addWhereCondition('OR', $column, $relation, $value);
return $this;
}
public function having($column, string $relation = null, $value = null): Select
public function having($column, ?string $relation = null, $value = null): Select
{
$this->addHavingCondition('AND', $column, $relation, $value);
return $this;
}
public function orHaving($column, string $relation = null, $value = null): Select
public function orHaving($column, ?string $relation = null, $value = null): Select
{
$this->addHavingCondition('OR', $column, $relation, $value);
@ -211,12 +211,12 @@ class Select
$this->joins[] = [$type, $table, $column1, $relation, $column2];
}
private function addWhereCondition(string $logic, $column, string $relation, $value): void
private function addWhereCondition(string $logic, $column, ?string $relation, $value): void
{
$this->conditions[self::CONDITION_WHERE][] = [$logic, $column, $relation, $value];
}
private function addHavingCondition(string $logic, $column, string $relation, $value): void
private function addHavingCondition(string $logic, $column, ?string $relation, $value): void
{
$this->conditions[self::CONDITION_HAVING][] = [$logic, $column, $relation, $value];
}
@ -276,13 +276,12 @@ class Select
return [(string) $table, $params];
}
if ($table instanceof Select)
{
if ($table instanceof Select) {
return $table->generateQuery();
}
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];
}
@ -295,24 +294,17 @@ class Select
return (string) $column;
}
if (is_array($column)) {
$out = '';
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);
if (!is_array($column)) {
$column = [$this->table, $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

View File

@ -7,13 +7,15 @@ class Request implements IRequest
{
private string $url;
private int $method;
private ?string $method = null;
private string $query = '';
private ?string $body = null;
private array $headers = [];
public function __construct(string $url = '', int $method = self::HTTP_GET)
public function __construct(string $url = '', ?string $method = null)
{
$this->url = $url;
$this->method = $method;
@ -24,7 +26,7 @@ class Request implements IRequest
$this->url = $url;
}
public function setMethod(int $method): void
public function setMethod(string $method): void
{
$this->method = $method;
}
@ -38,6 +40,11 @@ class Request implements IRequest
}
}
public function setBody(string $body): void
{
$this->body = $body;
}
public function setHeaders(array $headers): void
{
$this->headers = array_merge($this->headers, $headers);
@ -47,13 +54,20 @@ class Request implements IRequest
{
$ch = curl_init();
if ($this->method === self::HTTP_POST) {
$url = $this->url;
$url = $this->url . '?' . $this->query;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->query);
} else {
$url = $this->url . '?' . $this->query;
if ($this->body !== null) {
if ($this->method === null) {
$this->method = self::HTTP_POST;
}
if ($this->method === self::HTTP_POST) {
curl_setopt($ch, CURLOPT_POST, 1);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
}
curl_setopt($ch, CURLOPT_URL, $url);

View File

@ -2,16 +2,24 @@
interface IRequest
{
const HTTP_GET = 0;
const HTTP_GET = 'GET';
const HTTP_POST = 1;
const HTTP_POST = 'POST';
const HTTP_PUT = 'PUT';
const HTTP_PATCH = 'PATCH';
const HTTP_DELETE = 'DELETE';
public function setUrl(string $url): void;
public function setMethod(int $method): void;
public function setMethod(string $method): void;
public function setQuery($query): void;
public function setBody(string $body): void;
public function setHeaders(array $headers): void;
public function send(): IResponse;

View File

@ -0,0 +1,24 @@
<?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 loadMultiRelationsFromDb(array $models, string $relation, bool $useRelations = false, array $withRelations = []): void;
public function saveToDb(Model $model): void;
public function deleteFromDb(Model $model): void;
}

View File

@ -4,7 +4,7 @@ use SokoWeb\Interfaces\Authentication\IUser;
interface IRequest
{
public function setParsedRouteParams(array &$routeParams): void;
public function setParsedRouteParams(array $routeParams): void;
public function getBase(): string;

View File

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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -29,13 +29,6 @@ class Mail
{
$this->body = file_get_contents(ROOT . '/mail/' . $template . '.html');
$baseParameters = [
'APP_NAME' => $_ENV['APP_NAME'],
'BASE_URL' => $_ENV['APP_URL'],
];
$params = array_merge($baseParameters, $params);
foreach ($params as $key => $param) {
$this->body = str_replace('{{' . $key . '}}', $param, $this->body);
}
@ -51,8 +44,10 @@ class Mail
if (!empty($_ENV['MAIL_HOST'])) {
$mailer->Mailer = 'smtp';
$mailer->Host = $_ENV['MAIL_HOST'];
$mailer->Port = !empty($_ENV['MAIL_PORT']) ? $_ENV['MAIL_PORT'] : 25;
$mailer->SMTPSecure = !empty($_ENV['MAIL_SECURE']) ? $_ENV['MAIL_SECURE'] : '';
$mailer->Port = !empty($_ENV['MAIL_PORT']) ? $_ENV['MAIL_PORT'] : 587;
$secureMaping = ['none' => '', 'starttls' => PHPMailer::ENCRYPTION_STARTTLS, 'smtps' => PHPMailer::ENCRYPTION_SMTPS];
$mailer->SMTPSecure = !empty($_ENV['MAIL_SECURE']) ? $secureMaping[$_ENV['MAIL_SECURE']] : '';
if (!empty($_ENV['MAIL_USER'])) {
$mailer->SMTPAuth = true;

View File

@ -8,6 +8,8 @@ abstract class Model
protected static array $relations = [];
protected static array $multiRelations = [];
protected $id = null;
private array $snapshot = [];
@ -27,6 +29,11 @@ abstract class Model
return static::$relations;
}
public static function getMultiRelations(): array
{
return static::$multiRelations;
}
public function setId($id): void
{
$this->id = $id;
@ -41,7 +48,7 @@ abstract class Model
{
$array = [];
foreach (self::getFields() as $key) {
foreach (static::getFields() as $key) {
$method = 'get' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($this, $method)) {

View File

@ -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;
}
}

View File

@ -3,11 +3,27 @@
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;
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 = [])
{
$select = $this->createSelect($select, $type, $useRelations, $withRelations);
@ -37,19 +53,231 @@ 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);
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();
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 loadMultiRelationsFromDb(array $models, string $relation, bool $useRelations = false, array $withRelations = []): void
{
if (count($models) === 0) {
return;
}
$parentModelType = get_class($models[0]);
$relationModelsSetter = 'set' . str_replace('_', '', ucwords($relation, '_'));
[$relationModelType, $reverseRelation] = call_user_func([$parentModelType, 'getMultiRelations'])[$relation];
$reverseRelationIdGetter = 'get' . str_replace('_', '', ucwords($reverseRelation, '_')) . 'Id';
$parentModelsById = [];
foreach ($models as $model) {
$parentModelsById[$model->getId()] = $model;
}
$select = new Select($this->dbConnection);
$select->where($reverseRelation . '_id', 'IN', array_keys($parentModelsById));
$relationsByParentModelId = [];
foreach ($this->selectMultipleFromDb($select, $relationModelType, $useRelations, $withRelations) as $relationModel) {
$reverseRelationId = $relationModel->$reverseRelationIdGetter();
if (!isset($relationsByParentModelId[$reverseRelationId])) {
$relationsByParentModelId[$reverseRelationId] = [];
}
$relationsByParentModelId[$reverseRelationId][] = $relationModel;
}
foreach ($parentModelsById as $parentModelId => $parentModel) {
$relationModels = $relationsByParentModelId[$parentModelId] ?? [];
$parentModel->$relationModelsSetter($relationModels);
}
}
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'])) {
$diff['slug']['new'] = $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)) {
@ -76,12 +304,19 @@ class PersistentDataManager
next($data);
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
$relationType = current($relations);
$relationModel = new $relationType();
$this->fillWithData($data, $relationModel, $withRelations, $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);
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
$model->$method($relationModel);
} else {
while (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
next($data);
$key = key($data);
}
}
next($relations);
} else {
@ -92,136 +327,6 @@ class PersistentDataManager
$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
{
foreach ($model::getRelations() as $relation => $relationType) {

View File

@ -17,23 +17,23 @@ class Request implements IRequest
private array $headers;
private Session $session;
private ISession $session;
private ?IUser $user = null;
public function __construct(
string $base,
array &$get,
array &$post,
array $get,
array $post,
array $headers,
array &$session,
ISession $session,
IUserRepository $userRepository)
{
$this->base = $base;
$this->get = &$get;
$this->post = &$post;
$this->get = $get;
$this->post = $post;
$this->headers = $headers;
$this->session = new Session($session);
$this->session = $session;
$userId = $this->session->get('userId');
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

View File

@ -6,12 +6,12 @@ use SokoWeb\Interfaces\Response\IRedirect;
use SokoWeb\Interfaces\Response\IContent;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Interfaces\Routing\IRouteCollection;
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
{
@ -19,7 +19,7 @@ class HttpResponse
private IConnection $dbConnection;
private RouteCollection $routeCollection;
private IRouteCollection $routeCollection;
private array $appConfig;
@ -32,7 +32,7 @@ class HttpResponse
public function __construct(
IRequest $request,
IConnection $dbConnection,
RouteCollection $routeCollection,
IRouteCollection $routeCollection,
array $appConfig,
string $requestMethod,
string $requestUrl
@ -55,6 +55,11 @@ class HttpResponse
public function render(): void
{
$this->handleCors();
if ($this->method === 'options') {
return;
}
$match = $this->routeCollection->match($this->method, $this->parsedUrl['path']);
if ($match === null) {
$this->render404();
@ -64,7 +69,7 @@ class HttpResponse
list($route, $params) = $match;
$this->request->setParsedRouteParams($params);
$handler = $route->getHandler();
$controller = new $handler[0]($this->request);
$controller = new $handler[0]();
if (
$controller instanceof IAuthenticationRequired &&
@ -94,6 +99,7 @@ class HttpResponse
$response = call_user_func([$controller, $handler[1]]);
} catch (Exception $exception) {
$this->dbConnection->rollback();
error_log($exception);
$this->render500($exception);
return;
}
@ -103,17 +109,70 @@ class HttpResponse
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
$response->render();
} elseif ($response instanceof IRedirect) {
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode());
} else {
$this->render404();
}
}
private function handleCors(): void
{
$origin = $this->request->header('Origin');
if (!$origin) {
return;
}
if (isset($this->appConfig['cors']['allow_origins'])) {
if (in_array($origin, $this->appConfig['cors']['allow_origins']) || in_array('*', $this->appConfig['cors']['allow_origins'])) {
header("Access-Control-Allow-Origin: {$origin}");
}
}
if (!empty($this->appConfig['cors']['allow_credentials'])) {
header('Access-Control-Allow-Credentials: true');
}
if ($this->method !== 'options') {
return;
}
if (isset($this->appConfig['cors']['allow_headers'])) {
$headers = explode(',', $this->request->header('Access-Control-Request-Headers'));
if (in_array('*', $this->appConfig['cors']['allow_headers'])) {
$allow_headers = $headers;
} else {
$allow_headers = array_intersect($this->appConfig['cors']['allow_headers'], $headers);
}
if (count($allow_headers) > 0) {
header('Access-Control-Allow-Headers: ' . join(', ', $allow_headers));
}
}
if (isset($this->appConfig['cors']['allow_methods'])) {
if (in_array('*', $this->appConfig['cors']['allow_methods'])) {
$allow_methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT'];
} else {
$allow_methods = $this->appConfig['cors']['allow_methods'];
}
if (count($allow_methods) > 0) {
header('Access-Control-Allow-Methods: ' . join(', ', $allow_methods));
}
}
$max_age = $this->appConfig['cors']['max_age'] ?? 600;
header("Access-Control-Max-Age: {$max_age}");
}
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: ' . $response->getUrl(), true, $response->getHttpCode());
$response = new Redirect(
$this->routeCollection->getRoute($this->appConfig['loginRouteId'])
->generateLink(['redirect_after_login' => $this->rawUrl]),
IRedirect::TEMPORARY);
header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode());
}
private function renderAntiCsrfError(): void
@ -142,4 +201,13 @@ 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,15 +14,9 @@ class Redirect implements IRedirect
$this->type = $type;
}
public function getUrl(): string
public function getTarget(): string
{
if (preg_match('/^http(s)?/', $this->target) === 1) {
$link = $this->target;
} else {
$link = \Container::$request->getBase() . $this->target;
}
return $link;
return $this->target;
}
public function getHttpCode(): int

View File

@ -1,6 +1,8 @@
<?php namespace SokoWeb\Routing;
class Route
use SokoWeb\Interfaces\Routing\IRoute;
class Route implements IRoute
{
private string $id;
@ -32,7 +34,7 @@ class Route
foreach ($this->pattern as $fragment) {
if (preg_match('/^{(\\w+)(\\?)?}$/', $fragment, $matches) === 1) {
if (isset($parameters[$matches[1]])) {
$link[] = $parameters[$matches[1]];
$link[] = rawurlencode($parameters[$matches[1]]);
unset($parameters[$matches[1]]);
} elseif (!isset($matches[2])) {//TODO: why? parameter not found but not optional
$link[] = $fragment;
@ -51,7 +53,7 @@ class Route
$queryParams[$key] = $value;
}
$query = count($queryParams) > 0 ? '?' . http_build_query($queryParams) : '';
$query = count($queryParams) > 0 ? '?' . http_build_query($queryParams, encoding_type: PHP_QUERY_RFC3986) : '';
return '/' . implode('/', $link) . $query;
}
@ -62,7 +64,7 @@ class Route
foreach ($path as $i => $fragment) {
if (preg_match('/^{(\\w+)(?:\\?)?}$/', $this->pattern[$i], $matches) === 1) {
$parameters[$matches[1]] = $fragment;
$parameters[$matches[1]] = rawurldecode($fragment);
} elseif ($fragment != $this->pattern[$i]) {
return null;
}

View File

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

View File

@ -3,15 +3,29 @@
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 string $table;
private DateTime $shouldBeNewerThan;
private bool $exists = 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
{
return true;
@ -24,24 +38,27 @@ class DatabaseSessionHandler implements ISessionHandler
public function read($id): string
{
$select = new Select(\Container::$dbConnection, 'sessions');
$select->columns(['data']);
$select = new Select($this->dbConnection, $this->table);
$select->columns(['data', 'updated']);
$select->whereId(substr($id, 0, 32));
$result = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
if ($result === null) {
return '';
}
$this->exists = true;
if (new DateTime($result['updated']) < $this->shouldBeNewerThan) {
return '';
}
return $result['data'];
}
public function write($id, $data): bool
{
$modify = new Modify(\Container::$dbConnection, 'sessions');
$modify = new Modify($this->dbConnection, $this->table);
if ($this->exists) {
$modify->setId(substr($id, 0, 32));
@ -60,7 +77,7 @@ class DatabaseSessionHandler implements ISessionHandler
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->delete();
@ -69,12 +86,12 @@ class DatabaseSessionHandler implements ISessionHandler
return true;
}
public function gc($maxlifetime): bool
public function gc($maxlifetime): int|false
{
// empty on purpose
// old sessions are deleted by MaintainDatabaseCommand
return true;
return 1;
}
public function create_sid(): string
@ -93,7 +110,7 @@ class DatabaseSessionHandler implements ISessionHandler
return true;
}
$modify = new Modify(\Container::$dbConnection, 'sessions');
$modify = new Modify($this->dbConnection, $this->table);
$modify->setId(substr($id, 0, 32));
$modify->set('updated', (new DateTime())->format('Y-m-d H:i:s'));

View File

@ -97,7 +97,12 @@ class Linker
fwrite($outputFileHandle, $extra[0]);
while (($line = fgets($inputFileHandle)) !== false) {
fwrite($outputFileHandle, $line);
if (preg_match('/^\s*@include\((.*)\)\s*$/', $line, $matches) === 1) {
$include = file_get_contents(ROOT . '/views/' . $matches[1] . '.php');
fwrite($outputFileHandle, $include);
} else {
fwrite($outputFileHandle, $line);
}
}
fwrite($outputFileHandle, $extra[1]);
@ -134,7 +139,7 @@ class Linker
{
$output = [];
if (preg_match('/^[\w\/\.]+$/', $asset) === 1) {
if (preg_match('/^http(s)?/', $asset) !== 1) {
if (
empty($_ENV['DEV']) &&
filesize(ROOT . '/public/static/' . $asset) < self::INLINE_ASSET_LIMIT

View File

@ -16,10 +16,12 @@ class Container
{
static SokoWeb\Interfaces\Database\IConnection $dbConnection;
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\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::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler(Container::$dbConnection, 'sessions');
session_set_save_handler(Container::$sessionHandler, true);
session_start([

View File

@ -17,7 +17,7 @@ final class GoogleOAuthTest extends TestCase
$redirectUrl = 'http://example.com/oauth';
$requestMock = $this->getMockBuilder(IRequest::class)
->setMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send'])
->onlyMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send'])
->getMock();
$googleOAuth = new GoogleOAuth($requestMock);
@ -48,10 +48,10 @@ final class GoogleOAuthTest extends TestCase
$redirectUrl = 'http://example.com/oauth';
$requestMock = $this->getMockBuilder(IRequest::class)
->setMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send'])
->onlyMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send'])
->getMock();
$responseMock = $this->getMockBuilder(IResponse::class)
->setMethods(['getBody', 'getHeaders'])
->onlyMethods(['getBody', 'getHeaders'])
->getMock();
$googleOAuth = new GoogleOAuth($requestMock);