Init
This commit is contained in:
44
app/modules/Base/BaseError4xxPresenter.php
Normal file
44
app/modules/Base/BaseError4xxPresenter.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace App\Modules\Base;
|
||||
|
||||
use App\Model\Exception\Runtime\InvalidStateException;
|
||||
use Nette\Application\BadRequestException;
|
||||
use Nette\Application\Request;
|
||||
use Nette\Application\UI\ComponentReflection;
|
||||
|
||||
abstract class BaseError4xxPresenter extends SecuredPresenter
|
||||
{
|
||||
|
||||
/**
|
||||
* Common presenter method
|
||||
*/
|
||||
public function startup(): void
|
||||
{
|
||||
parent::startup();
|
||||
|
||||
if ($this->getRequest() !== null && $this->getRequest()->isMethod(Request::FORWARD)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->error();
|
||||
}
|
||||
|
||||
public function renderDefault(BadRequestException $exception): void
|
||||
{
|
||||
$rf1 = new ComponentReflection(static::class);
|
||||
$fileName = $rf1->getFileName();
|
||||
|
||||
// Validate if class is not in PHP core
|
||||
if ($fileName === false) {
|
||||
throw new InvalidStateException('Class is defined in the PHP core or in a PHP extension');
|
||||
}
|
||||
|
||||
$dir = dirname($fileName);
|
||||
|
||||
// Load template 403.latte or 404.latte or ... 4xx.latte
|
||||
$file = $dir . '/templates/' . $exception->getCode() . '.latte';
|
||||
$this->template->setFile(is_file($file) ? $file : $dir . '/templates/4xx.latte');
|
||||
}
|
||||
|
||||
}
|
||||
57
app/modules/Base/BaseErrorPresenter.php
Normal file
57
app/modules/Base/BaseErrorPresenter.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace App\Modules\Base;
|
||||
|
||||
use Nette\Application\BadRequestException;
|
||||
use Nette\Application\Helpers;
|
||||
use Nette\Application\IResponse as AppResponse;
|
||||
use Nette\Application\Request;
|
||||
use Nette\Application\Responses\CallbackResponse;
|
||||
use Nette\Application\Responses\ForwardResponse;
|
||||
use Nette\Http\IRequest;
|
||||
use Nette\Http\IResponse;
|
||||
use Psr\Log\LogLevel;
|
||||
use Throwable;
|
||||
use Tracy\Debugger;
|
||||
use Tracy\ILogger;
|
||||
|
||||
abstract class BaseErrorPresenter extends SecuredPresenter
|
||||
{
|
||||
|
||||
/**
|
||||
* @return ForwardResponse|CallbackResponse
|
||||
*/
|
||||
public function run(Request $request): AppResponse
|
||||
{
|
||||
$e = $request->getParameter('exception');
|
||||
|
||||
if ($e instanceof Throwable) {
|
||||
$code = $e->getCode();
|
||||
$level = ($code >= 400 && $code <= 499) ? LogLevel::WARNING : LogLevel::ERROR;
|
||||
|
||||
Debugger::log(sprintf(
|
||||
'Code %s: %s in %s:%s',
|
||||
$code,
|
||||
$e->getMessage(),
|
||||
$e->getFile(),
|
||||
$e->getLine()
|
||||
), $level);
|
||||
|
||||
Debugger::log($e, ILogger::EXCEPTION);
|
||||
}
|
||||
|
||||
if ($e instanceof BadRequestException) {
|
||||
[$module, , $sep] = Helpers::splitName($request->getPresenterName());
|
||||
|
||||
return new ForwardResponse($request->setPresenterName($module . $sep . 'Error4xx'));
|
||||
}
|
||||
|
||||
return new CallbackResponse(function (IRequest $httpRequest, IResponse $httpResponse): void {
|
||||
$header = $httpResponse->getHeader('Content-Type');
|
||||
if ($header !== null && preg_match('#^text/html(?:;|$)#', $header)) {
|
||||
require __DIR__ . '/templates/500.phtml';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
23
app/modules/Base/BasePresenter.php
Normal file
23
app/modules/Base/BasePresenter.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace App\Modules\Base;
|
||||
|
||||
use App\Model\Latte\TemplateProperty;
|
||||
use App\Model\Security\SecurityUser;
|
||||
use App\UI\Control\TFlashMessage;
|
||||
use App\UI\Control\TModuleUtils;
|
||||
use Contributte\Application\UI\Presenter\StructuredTemplates;
|
||||
use Nette\Application\UI\Presenter;
|
||||
|
||||
/**
|
||||
* @property-read TemplateProperty $template
|
||||
* @property-read SecurityUser $user
|
||||
*/
|
||||
abstract class BasePresenter extends Presenter
|
||||
{
|
||||
|
||||
use StructuredTemplates;
|
||||
use TFlashMessage;
|
||||
use TModuleUtils;
|
||||
|
||||
}
|
||||
30
app/modules/Base/SecuredPresenter.php
Normal file
30
app/modules/Base/SecuredPresenter.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace App\Modules\Base;
|
||||
|
||||
use App\Model\App;
|
||||
use Nette\Application\UI\ComponentReflection;
|
||||
use Nette\Security\IUserStorage;
|
||||
|
||||
abstract class SecuredPresenter extends BasePresenter
|
||||
{
|
||||
|
||||
/**
|
||||
* @param ComponentReflection|mixed $element
|
||||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
|
||||
*/
|
||||
public function checkRequirements($element): void
|
||||
{
|
||||
if (!$this->user->isLoggedIn()) {
|
||||
if ($this->user->getLogoutReason() === IUserStorage::INACTIVITY) {
|
||||
$this->flashInfo('You have been logged out for inactivity');
|
||||
}
|
||||
|
||||
$this->redirect(
|
||||
App::DESTINATION_SIGN_IN,
|
||||
['backlink' => $this->storeRequest()]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
8
app/modules/Base/UnsecuredPresenter.php
Normal file
8
app/modules/Base/UnsecuredPresenter.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace App\Modules\Base;
|
||||
|
||||
abstract class UnsecuredPresenter extends BasePresenter
|
||||
{
|
||||
|
||||
}
|
||||
27
app/modules/Base/templates/500.phtml
Normal file
27
app/modules/Base/templates/500.phtml
Normal 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>
|
||||
28
app/modules/Base/templates/@layout.latte
Normal file
28
app/modules/Base/templates/@layout.latte
Normal file
@@ -0,0 +1,28 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<link rel="shortcut icon" href="{$basePath}/favicon.ico">
|
||||
|
||||
<!-- Seo -->
|
||||
<title>{block #title|stripHtml|trim}Webapp Skeleton{/} | Contributte</title>
|
||||
|
||||
<!-- Meta -->
|
||||
<meta name="description" n:ifset="#description" content="{include #description}">
|
||||
<meta name="keywords" n:ifset="#keywords" content="{include #keywords}">
|
||||
<meta name="robots" content="index,follow">
|
||||
<meta name="googlebot" content="snippet,archive">
|
||||
<meta name="author" content="f3l1x">
|
||||
|
||||
{block #head}{/}
|
||||
</head>
|
||||
<body>
|
||||
{block #main}
|
||||
<div class="container">
|
||||
{include #content}
|
||||
</div>
|
||||
{/}
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user