Premier commit

This commit is contained in:
2024-09-09 10:22:45 +02:00
commit bcc2604080
74 changed files with 25819 additions and 0 deletions

51
lib/AuthMiddleware.php Normal file
View File

@@ -0,0 +1,51 @@
<?php
namespace app\lib;
use PDO;
use Piko\ModularApplication;
use HttpSoft\Message\Response;
use app\modules\user\models\User;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
final class AuthMiddleware implements MiddlewareInterface
{
private ModularApplication $application;
public function __construct(ModularApplication $app)
{
$this->application = $app;
$pdo = $this->application->getComponent('PDO');
assert($pdo instanceof PDO);
User::$pdo = $pdo;
}
/**
* {@inheritDoc}
* @see \Psr\Http\Server\MiddlewareInterface::process()
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$user = $this->application->getComponent('Piko\User');
assert($user instanceof \Piko\User);
$router = $this->application->getComponent('Piko\Router');
assert($router instanceof \Piko\Router);
$loginUrl = $router->getUrl('user/default/login');
$params = $request->getServerParams();
if ($user->isGuest() && $params['REQUEST_URI'] != $loginUrl) {
$response= new Response();
return $response->withHeader('Location', $loginUrl);
}
return $handler->handle($request);
}
}

22
lib/CorsMiddleware.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
namespace app\lib;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
final class CorsMiddleware implements MiddlewareInterface
{
/**
* {@inheritDoc}
* @see \Psr\Http\Server\MiddlewareInterface::process()
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
return $response->withHeader('Access-Control-Allow-Origin', '*');
}
}

107
lib/Vite.php Normal file
View File

@@ -0,0 +1,107 @@
<?php
namespace app\lib;
use Piko;
class Vite
{
// For a real-world example check here:
// https://github.com/wp-bond/bond/blob/master/src/Tooling/Vite.php
// https://github.com/wp-bond/boilerplate/tree/master/app/themes/boilerplate
// you might check @vitejs/plugin-legacy if you need to support older browsers
// https://github.com/vitejs/vite/tree/main/packages/plugin-legacy
/**
* Prints all the html entries needed for Vite
*/
public static function vite(string $entry): string
{
return implode("\n", [
static::jsTag($entry),
static::jsPreloadImports($entry),
static::cssTag($entry)
]);
}
// Helpers to print tags
private static function jsTag(string $entry): string
{
if (strpos($entry, '.js') === false) {
return '';
}
$url = getenv('VITE_ENV') === 'dev'
? getenv('VITE_HOST') . Piko::getAlias('@vite_web/' . $entry)
: static::assetUrl($entry);
if (!$url) {
return '';
}
return '<script type="module" crossorigin src="' . $url . '"></script>';
}
private static function jsPreloadImports(string $entry): string
{
if (getenv('VITE_ENV') === 'dev') {
return '';
}
$res = '';
foreach (static::importsUrls($entry) as $url) {
$res .= '<link rel="modulepreload" href="'. $url. '">';
}
return $res;
}
private static function cssTag(string $entry): string
{
// Not needed on dev, it's inject by Vite
if (getenv('VITE_ENV') === 'dev' || strpos($entry, '.css') === false) {
return '';
}
$url = static::assetUrl($entry);
if (!$url) {
return '';
}
return '<link rel="stylesheet" type="text/css" href="' . $url . '">';
}
// Helpers to locate files
private static function getManifest(): array
{
$content = file_get_contents(Piko::getAlias('@webroot/manifest.json'));
return json_decode($content, true);
}
private static function assetUrl(string $entry): string
{
$manifest = static::getManifest();
return isset($manifest[$entry])
? Piko::getAlias('@web/' . $manifest[$entry]['file'])
: '';
}
private static function importsUrls(string $entry): array
{
$urls = [];
$manifest = static::getManifest();
if (!empty($manifest[$entry]['imports'])) {
foreach ($manifest[$entry]['imports'] as $imports) {
$urls[] = Piko::getAlias('@web/' . $manifest[$imports]['file']);
}
}
return $urls;
}
}