This commit is contained in:
2022-02-15 07:33:00 +01:00
commit 50638af575
52 changed files with 7293 additions and 0 deletions

33
app/Bootstrap.php Executable file
View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace App;
use Nette\Bootstrap\Configurator;
class Bootstrap
{
public static function boot(): Configurator
{
$configurator = new Configurator;
$appDir = dirname(__DIR__);
$configurator->setDebugMode($_SERVER['HTTP_HOST'] === 'localhost');
$configurator->enableTracy($appDir . '/log');
$configurator->setTimeZone('Europe/Prague');
$configurator->setTempDirectory($appDir . '/temp');
$configurator->createRobotLoader()
->addDirectory(__DIR__)
->register();
$configurator->addConfig($appDir . '/config/common.neon');
$configurator->addConfig($appDir . '/config/services.neon');
$configurator->addConfig($appDir . '/config/local.neon');
return $configurator;
}
}

21
app/Latte/AssetFilter.php Executable file
View File

@@ -0,0 +1,21 @@
<?php
namespace App\Latte;
use Nette\Utils\JsonException;
use Vite;
class AssetFilter
{
public function __construct(
private Vite $vite,
) {}
/**
* @throws JsonException
*/
public function __invoke(string $path): string
{
return $this->vite->getAsset($path);
}
}

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\Presenters;
use Nette;
use Vite;
/**
* Base presenter for all application presenters.
*/
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
public function __construct(
private Vite $vite,
) {}
public function beforeRender(): void
{
$this->template->vite = $this->vite;
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Presenters;
use Nette;
final class Error4xxPresenter extends Nette\Application\UI\Presenter
{
public function startup(): void
{
parent::startup();
if (!$this->getRequest()->isMethod(Nette\Application\Request::FORWARD)) {
$this->error();
}
}
public function renderDefault(Nette\Application\BadRequestException $exception): void
{
// load template 403.latte or 404.latte or ... 4xx.latte
$file = __DIR__ . "/templates/Error/{$exception->getCode()}.latte";
$this->template->setFile(is_file($file) ? $file : __DIR__ . '/templates/Error/4xx.latte');
}
}

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace App\Presenters;
use Nette;
use Nette\Application\Responses;
use Nette\Http;
use Tracy\ILogger;
final class ErrorPresenter implements Nette\Application\IPresenter
{
use Nette\SmartObject;
/** @var ILogger */
private $logger;
public function __construct(ILogger $logger)
{
$this->logger = $logger;
}
public function run(Nette\Application\Request $request): Nette\Application\Response
{
$exception = $request->getParameter('exception');
if ($exception instanceof Nette\Application\BadRequestException) {
[$module, , $sep] = Nette\Application\Helpers::splitName($request->getPresenterName());
return new Responses\ForwardResponse($request->setPresenterName($module . $sep . 'Error4xx'));
}
$this->logger->log($exception, ILogger::EXCEPTION);
return new Responses\CallbackResponse(function (Http\IRequest $httpRequest, Http\IResponse $httpResponse): void {
if (preg_match('#^text/html(?:;|$)#', (string) $httpResponse->getHeader('Content-Type'))) {
require __DIR__ . '/templates/Error/500.phtml';
}
});
}
}

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace App\Presenters;
use Nette;
final class HomepagePresenter extends BasePresenter
{
}

View File

@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>{ifset title}{include title|stripHtml} | {/ifset}Nette Web</title>
{if $vite->isEnabled()}
<script type="module" src="{='@vite/client'|asset}"></script>
{else}
{foreach $vite->getCssAssets('src/scripts/main.js') as $path}
<link rel="stylesheet" href="{$path}">
{/foreach}
{/if}
<script src="{='src/scripts/main.js'|asset}" type="module"></script>
</head>
<body data-controller="body" class="bg-blue-100">
<div n:foreach="$flashes as $flash" n:class="flash, $flash->type">{$flash->message}</div>
{include content}
{block scripts}
{/block}
</body>
</html>

View File

@@ -0,0 +1,7 @@
{block content}
<h1 n:block=title>Access Denied</h1>
<p>You do not have permission to view this page. Please try contact the web
site administrator if you believe you should be able to view this page.</p>
<p><small>error 403</small></p>

View File

@@ -0,0 +1,8 @@
{block content}
<h1 n:block=title>Page Not Found</h1>
<p>The page you requested could not be found. It is possible that the address is
incorrect, or that the page no longer exists. Please use a search engine to find
what you are looking for.</p>
<p><small>error 404</small></p>

View File

@@ -0,0 +1,6 @@
{block content}
<h1 n:block=title>Method Not Allowed</h1>
<p>The requested method is not allowed for the URL.</p>
<p><small>error 405</small></p>

View File

@@ -0,0 +1,6 @@
{block content}
<h1 n:block=title>Page Not Found</h1>
<p>The page you requested has been taken off the site. We apologize for the inconvenience.</p>
<p><small>error 410</small></p>

View File

@@ -0,0 +1,4 @@
{block content}
<h1 n:block=title>Oops...</h1>
<p>Your browser sent a request that this server could not understand or process.</p>

View File

@@ -0,0 +1,27 @@
<!DOCTYPE html><!-- "' --></textarea></script></style></pre></xmp></a></audio></button></canvas></datalist></details></dialog></iframe></listing></meter></noembed></noframes></noscript></optgroup></option></progress></rp></select></table></template></title></video>
<meta charset="utf-8">
<meta name="robots" content="noindex">
<title>Server Error</title>
<style>
#nette-error { all: initial; position: absolute; top: 0; left: 0; right: 0; height: 70vh; min-height: 400px; display: flex; align-items: center; justify-content: center; z-index: 1000 }
#nette-error div { all: initial; max-width: 550px; background: white; color: #333; display: block }
#nette-error h1 { all: initial; font: bold 50px/1.1 sans-serif; display: block; margin: 40px }
#nette-error p { all: initial; font: 20px/1.4 sans-serif; margin: 40px; display: block }
#nette-error small { color: gray }
</style>
<div id=nette-error>
<div>
<h1>Server Error</h1>
<p>We're sorry! The server encountered an internal error and
was unable to complete your request. Please try again later.</p>
<p><small>error 500</small></p>
</div>
</div>
<script>
document.body.insertBefore(document.getElementById('nette-error'), document.body.firstChild);
</script>

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
header('HTTP/1.1 503 Service Unavailable');
header('Retry-After: 300'); // 5 minutes in seconds
?>
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="robots" content="noindex">
<meta name="generator" content="Nette Framework">
<style>
body { color: #333; background: white; width: 500px; margin: 100px auto }
h1 { font: bold 47px/1.5 sans-serif; margin: .6em 0 }
p { font: 21px/1.5 Georgia,serif; margin: 1.5em 0 }
</style>
<title>Site is temporarily down for maintenance</title>
<h1>We're Sorry</h1>
<p>The site is temporarily down for maintenance. Please try again in a few minutes.</p>

View File

@@ -0,0 +1,5 @@
{* This is the welcome page, you can delete it *}
{block content}
<div id="app">
</div>

21
app/Router/RouterFactory.php Executable file
View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Router;
use Nette;
use Nette\Application\Routers\RouteList;
final class RouterFactory
{
use Nette\StaticClass;
public static function createRouter(): RouteList
{
$router = new RouteList;
$router->addRoute('<presenter>/<action>[/<id>]', 'Homepage:default');
return $router;
}
}

91
app/Services/Vite.php Executable file
View File

@@ -0,0 +1,91 @@
<?php
use Nette\Utils\FileSystem;
use Nette\Utils\Html;
use Nette\Utils\Json;
use Nette\Http\Request;
class Vite
{
public function __construct(
private string $viteServer,
private string $manifestFile,
private bool $productionMode,
private Request $httpRequest,
){}
/**
* @throws \Nette\Utils\JsonException
*/
public function getAsset(string $entrypoint): string
{
$asset = '';
$baseUrl = '/';
if (!$this->isEnabled()) {
if (file_exists($this->manifestFile)) {
$manifest = Json::decode(FileSystem::read($this->manifestFile), Json::FORCE_ARRAY);
$asset = $manifest[$entrypoint]['file'];
} else {
trigger_error('Missing manifest file: ' . $this->manifestFile, E_USER_WARNING);
}
} else {
$baseUrl = $this->viteServer . '/';
$asset = $entrypoint;
}
return $baseUrl . $asset;
}
/**
* @throws \Nette\Utils\JsonException
*/
public function getCssAssets(string $entrypoint): array
{
$assets = [];
if (!$this->isEnabled()) {
if (file_exists($this->manifestFile)) {
$manifest = Json::decode(FileSystem::read($this->manifestFile), Json::FORCE_ARRAY);
$assets = $manifest[$entrypoint]['css'] ?? [];
} else {
trigger_error('Missing manifest file: ' . $this->manifestFile, E_USER_WARNING);
}
}
return $assets;
}
public function isEnabled(): bool
{
if (!$this->productionMode && $this->httpRequest->getCookie('netteVite') === 'true') {
return true;
} else {
return false;
}
}
/**
* @throws \Nette\Utils\JsonException
*/
public function printTags(string $entrypoint): void
{
$scripts = [$this->getAsset($entrypoint)];
$styles = $this->getCssAssets($entrypoint);
if ($this->isEnabled()) {
echo Html::el('script')->type('module')->src($this->viteServer . '/' . '@vite/client');
}
foreach ($styles as $path) {
echo Html::el('link')->rel('stylesheet')->href($path);
}
foreach ($scripts as $path) {
echo Html::el('script')->type('module')->src($path);
}
}
}

49
app/Tracy/Vite/Vite.html Executable file
View File

@@ -0,0 +1,49 @@
<span title="Toggle Vite" data-action="netteVite">
<svg width="410" height="404" viewBox="0 0 410 404" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M399.641 59.5246L215.643 388.545C211.844 395.338 202.084 395.378 198.228 388.618L10.5817 59.5563C6.38087 52.1896 12.6802 43.2665 21.0281 44.7586L205.223 77.6824C206.398 77.8924 207.601 77.8904 208.776 77.6763L389.119 44.8058C397.439 43.2894 403.768 52.1434 399.641 59.5246Z" fill="url(#paint0_linear)"/>
<path d="M292.965 1.5744L156.801 28.2552C154.563 28.6937 152.906 30.5903 152.771 32.8664L144.395 174.33C144.198 177.662 147.258 180.248 150.51 179.498L188.42 170.749C191.967 169.931 195.172 173.055 194.443 176.622L183.18 231.775C182.422 235.487 185.907 238.661 189.532 237.56L212.947 230.446C216.577 229.344 220.065 232.527 219.297 236.242L201.398 322.875C200.278 328.294 207.486 331.249 210.492 326.603L212.5 323.5L323.454 102.072C325.312 98.3645 322.108 94.137 318.036 94.9228L279.014 102.454C275.347 103.161 272.227 99.746 273.262 96.1583L298.731 7.86689C299.767 4.27314 296.636 0.855181 292.965 1.5744Z" fill="url(#paint1_linear)"/>
<defs>
<linearGradient id="paint0_linear" x1="6.00017" y1="32.9999" x2="235" y2="344" gradientUnits="userSpaceOnUse">
<stop stop-color="#41D1FF"/>
<stop offset="1" stop-color="#BD34FE"/>
</linearGradient>
<linearGradient id="paint1_linear" x1="194.651" y1="8.81818" x2="236.076" y2="292.989" gradientUnits="userSpaceOnUse">
<stop stop-color="#FFEA83"/>
<stop offset="0.0833333" stop-color="#FFDD35"/>
<stop offset="1" stop-color="#FFA800"/>
</linearGradient>
</defs>
</svg>
</span>
<style>
#tracy-debug [data-action="netteVite"] {
cursor: pointer;
padding: 0 .4em;
margin: 0 -.4em;
display: block;
}
#tracy-debug [data-action="netteVite"]:hover {
background: #c3c1b8;
}
</style>
<script>
function getCookie(n, a = `; ${document.cookie}`.match(`;\\s*${n}=([^;]+)`)) {
return a ? JSON.parse(a[1]) : false;
}
const element = document.querySelector('#tracy-debug [data-action="netteVite"]')
console.log(getCookie('netteVite'))
if (!getCookie('netteVite')) {
element.style.opacity = '40%'
}
element.addEventListener('click', () => {
document.cookie = getCookie('netteVite') ? 'netteVite=false; path=/;' : 'netteVite=true; path=/;'
getCookie('netteVite') ? (element.style.opacity = '') : (element.style.opacity = '40%')
document.location.reload()
})
</script>

14
app/Tracy/Vite/Vite.php Executable file
View File

@@ -0,0 +1,14 @@
<?php
class VitePanel implements Tracy\IBarPanel
{
public function getTab()
{
return file_get_contents(__DIR__ . '/Vite.html');
}
public function getPanel()
{
return '';
}
}