This commit is contained in:
Kilian Hofmann 2024-07-23 01:22:16 +02:00
parent 85d20e034a
commit 1bff7f46d7
9 changed files with 40 additions and 12 deletions

View File

@ -16,7 +16,7 @@ use Pecee\SimpleRouter\Handlers\EventHandler;
use Pecee\SimpleRouter\Event\EventArgument; use Pecee\SimpleRouter\Event\EventArgument;
use Pecee\SimpleRouter\Route\ILoadableRoute; use Pecee\SimpleRouter\Route\ILoadableRoute;
use Pecee\SimpleRouter\Route\IGroupRoute; use Pecee\SimpleRouter\Route\IGroupRoute;
use Config\Config; use Khofmann\Config\Config;
// Router base path // Router base path
$basePath = Config::getBasePath() . "api"; $basePath = Config::getBasePath() . "api";
// Router event handler for prepending base path // Router event handler for prepending base path

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Config; namespace Khofmann\Config;
use Exception; use Exception;
@ -11,17 +11,18 @@ class Config
private array $app; private array $app;
private array $database; private array $database;
protected function __construct() private function __construct()
{ {
$this->app = require_once __DIR__ . "/app.php"; $this->app = require_once __DIR__ . "/../../config/app.php";
$this->database = require_once __DIR__ . "/database.php"; $this->database = require_once __DIR__ . "/../../config/database.php";
} }
protected function __clone() private function __clone()
{ {
throw new Exception("Cannot clone a singleton.");
} }
public function __wakeup() private function __wakeup()
{ {
throw new Exception("Cannot unserialize a singleton."); throw new Exception("Cannot unserialize a singleton.");
} }

View File

@ -3,17 +3,28 @@
namespace Khofmann\Database; namespace Khofmann\Database;
use PDO; use PDO;
use Config\Config; use Khofmann\Config\Config;
use Exception;
class Database extends PDO class Database extends PDO
{ {
private static array $instances = []; private static array $instances = [];
protected function __construct(string $dsn, string $username = null, string $password = null, array $options = null) private function __construct(string $dsn, string $username = null, string $password = null, array $options = null)
{ {
parent::__construct($dsn, $username, $password, $options); parent::__construct($dsn, $username, $password, $options);
} }
private function __clone()
{
throw new Exception("Cannot clone a singleton.");
}
private function __wakeup()
{
throw new Exception("Cannot unserialize a singleton.");
}
public static function getInstance(): Database public static function getInstance(): Database
{ {
$cls = static::class; $cls = static::class;

View File

@ -4,6 +4,10 @@ namespace Khofmann\GUID;
class GUID class GUID
{ {
private function __construct()
{
}
public static function v4($data = null): string public static function v4($data = null): string
{ {
// Generate 16 bytes (128 bits) of random data or use the data passed into the function. // Generate 16 bytes (128 bits) of random data or use the data passed into the function.

View File

@ -6,6 +6,10 @@ use Khofmann\Request\Request;
class Input class Input
{ {
private function __construct()
{
}
public static function post(string $index, $defaultValue = null) public static function post(string $index, $defaultValue = null)
{ {
$value = Request::request()->getInputHandler()->post($index, $defaultValue); $value = Request::request()->getInputHandler()->post($index, $defaultValue);

View File

@ -21,7 +21,7 @@ class Post implements JsonSerializable
private string $content; private string $content;
private DateTime $postedAt; private DateTime $postedAt;
public function __construct(int $id, ?User $user, ?string $name, string $content, string $postedAt) private function __construct(int $id, ?User $user, ?string $name, string $content, string $postedAt)
{ {
$this->id = $id; $this->id = $id;
$this->user = $user; $this->user = $user;

View File

@ -6,7 +6,7 @@ use Exception;
use PDO; use PDO;
use DateTime; use DateTime;
use Khofmann\Database\Database; use Khofmann\Database\Database;
use Config\Config; use Khofmann\Config\Config;
use JsonSerializable; use JsonSerializable;
use Khofmann\ApiError\ApiError; use Khofmann\ApiError\ApiError;
use Khofmann\GUID\GUID; use Khofmann\GUID\GUID;
@ -23,7 +23,7 @@ class User implements JsonSerializable
private DateTime $memberSince; private DateTime $memberSince;
private int $postCount; private int $postCount;
protected function __construct(int $id, string $username, int $status, string $email, string $timestamp, ?string $image, bool $isAdmin, int $postCount) private function __construct(int $id, string $username, int $status, string $email, string $timestamp, ?string $image, bool $isAdmin, int $postCount)
{ {
$this->id = $id; $this->id = $id;
$this->username = $username; $this->username = $username;

View File

@ -7,6 +7,10 @@ use Pecee\SimpleRouter\SimpleRouter;
class Request class Request
{ {
private function __construct()
{
}
public static function request(): PRequest public static function request(): PRequest
{ {
return SimpleRouter::request(); return SimpleRouter::request();

View File

@ -7,6 +7,10 @@ use Pecee\Http\Response as PResponse;
class Response class Response
{ {
private function __construct()
{
}
public static function response(): PResponse public static function response(): PResponse
{ {
return SimpleRouter::response(); return SimpleRouter::response();