20240502#1
This commit is contained in:
149
application/src/Presentation/Controllers/AbstractController.php
Normal file
149
application/src/Presentation/Controllers/AbstractController.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Presentation\Controllers;
|
||||
|
||||
use Laminas\Diactoros\Response\HtmlResponse;
|
||||
use Laminas\Diactoros\Response\JsonResponse;
|
||||
use Laminas\Diactoros\Response\RedirectResponse;
|
||||
use Laminas\Diactoros\Response\TextResponse;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Rmphp\Kernel\Main;
|
||||
use Throwable;
|
||||
|
||||
abstract class AbstractController extends Main {
|
||||
|
||||
/**
|
||||
* @param Throwable $throwable
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function logException(Throwable $throwable, array $data = []) : void {
|
||||
$this->logger()->warning($throwable->getMessage()." on ".$throwable->getFile().":".$throwable->getLine(), $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Throwable $throwable
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function logError(Throwable $throwable, array $data = []) : void {
|
||||
$this->logger()->error($throwable->getMessage()." on ".$throwable->getFile().":".$throwable->getLine(), $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @return void
|
||||
*/
|
||||
public function addHeader(string $name, string $value) : void {
|
||||
$this->globals()->addHeader($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $html
|
||||
* @param int $status
|
||||
* @param array $headers
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function htmlResponse($html, int $status = 200, array $headers = []) : ResponseInterface {
|
||||
return new HtmlResponse($html, $status, array_merge($this->globals()->response()->getHeaders(), $headers));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $text
|
||||
* @param int $status
|
||||
* @param array $headers
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function textResponse($text, int $status = 200, array $headers = []) : ResponseInterface {
|
||||
return new TextResponse($text, $status, array_merge($this->globals()->response()->getHeaders(), $headers));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param int $status
|
||||
* @param array $headers
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function jsonResponse(array $array, int $status = 200, array $headers = []) : ResponseInterface {
|
||||
return new JsonResponse($array, $status, array_merge($this->globals()->response()->getHeaders(), $headers), JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param int $status
|
||||
* @param array $headers
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function redirectResponse(string $url, int $status = 302, array $headers = []) : ResponseInterface {
|
||||
return new RedirectResponse($url, $status, array_merge($this->globals()->response()->getHeaders(), $headers));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $point
|
||||
* @param string $subtemplate
|
||||
* @param array $data
|
||||
* @param int $status
|
||||
* @param array $headers
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function renderResponse(string $point, string $subtemplate, array $data = [], int $status = 200, array $headers = []) : ResponseInterface {
|
||||
$this->template()->setSubtemplate($point, $this->getTemplatePath($subtemplate), $data);
|
||||
return new HtmlResponse($this->template()->getResponse(), $status, array_merge($this->globals()->response()->getHeaders(), $headers));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $status
|
||||
* @param array $headers
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function render(int $status = 200, array $headers = []) : ResponseInterface {
|
||||
return new HtmlResponse($this->template()->getResponse(), $status, array_merge($this->globals()->response()->getHeaders(), $headers));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $point
|
||||
* @param string $string
|
||||
* @return void
|
||||
*/
|
||||
public function templSetValue(string $point, string $string) : void {
|
||||
$this->template()->setValue($point, $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $point
|
||||
* @param string $string
|
||||
* @return void
|
||||
*/
|
||||
public function templAddValue(string $point, string $string) : void {
|
||||
$this->template()->addValue($point, $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $point
|
||||
* @param string $subtemplate
|
||||
* @param array $resource
|
||||
* @return void
|
||||
*/
|
||||
public function templSetSubtemplate(string $point, string $subtemplate, array $resource = []) : void {
|
||||
$this->template()->setSubtemplate($point, $this->getTemplatePath($subtemplate), $resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $point
|
||||
* @param string $subtemplate
|
||||
* @param array $resource
|
||||
* @return void
|
||||
*/
|
||||
public function templAddSubtemplate(string $point, string $subtemplate, array $resource = []) : void {
|
||||
$this->template()->addSubtemplate($point, $this->getTemplatePath($subtemplate), $resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public function getTemplatePath(string $path) : string {
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Presentation\Controllers;
|
||||
|
||||
use Base\Application\ApplicationException;
|
||||
use Base\Application\DTOException;
|
||||
use Base\Domain\DomainException;
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
abstract class AbstractPageController extends AbstractController {
|
||||
|
||||
/**
|
||||
* @param Exception $exception
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function exceptionPage(Exception $exception, array $data = []) : void {
|
||||
$this->logException($exception, $data);
|
||||
$this->syslogger()->warning($exception->getMessage()." on ".$exception->getFile().":".$exception->getLine(), $data);
|
||||
$this->template()->setSubtemplate("main", "/templates/error/errpage.tpl", [
|
||||
"errorText" => "<span style='color:red'>Error: ".$exception->getMessage()." (".$exception->getCode().")"."</span>"
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Throwable $e
|
||||
* @return string
|
||||
*/
|
||||
public function checkError(Throwable $e) : string {
|
||||
($e instanceof Exception) ? $this->logException($e) : $this->logError($e);
|
||||
if($e instanceof DTOException || $e instanceof DomainException || $e instanceof ApplicationException) return $e->getMessage();
|
||||
return "Ошибка. Дата и время: ".date("d-m-Y H:i:s");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Presentation\Controllers;
|
||||
|
||||
class NotFoundException extends \Exception {
|
||||
|
||||
}
|
||||
15
application/src/Presentation/templates/base.tpl
Normal file
15
application/src/Presentation/templates/base.tpl
Normal file
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Rmphp - <?=$this->getPoint('title')?></title>
|
||||
<link href="/assets/css/style.css?1" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<?=$this->getPoint('main')?>
|
||||
<script type="text/javascript" src="/assets/js/script.js?1"></script>
|
||||
<?=$this->getPoint('jsscript')?>
|
||||
</body>
|
||||
</html>
|
||||
38
application/src/Presentation/templates/error/404.tpl
Normal file
38
application/src/Presentation/templates/error/404.tpl
Normal file
@@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>404 Page Not Found</title>
|
||||
<style>
|
||||
body{
|
||||
font-family: Arial, sans-serif;
|
||||
font-size: 15px;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background: #c9d1e1;
|
||||
}
|
||||
.err-page{
|
||||
display: flex;
|
||||
align-items: center;
|
||||
height: 50vh;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
color:#888;
|
||||
}
|
||||
.err-page__header{
|
||||
font-size: 70px;
|
||||
}
|
||||
.err-page__text{
|
||||
font-size: 30px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="err-page">
|
||||
<div class="err-page__header">404</div>
|
||||
<div class="err-page__text">Page Not Found</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
38
application/src/Presentation/templates/error/501.tpl
Normal file
38
application/src/Presentation/templates/error/501.tpl
Normal file
@@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>501 Not Implemented</title>
|
||||
<style>
|
||||
body{
|
||||
font-family: Arial, sans-serif;
|
||||
font-size: 15px;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background: #e1c9d1;
|
||||
}
|
||||
.err-page{
|
||||
display: flex;
|
||||
align-items: center;
|
||||
height: 50vh;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
color:#888;
|
||||
}
|
||||
.err-page__header{
|
||||
font-size: 70px;
|
||||
}
|
||||
.err-page__text{
|
||||
font-size: 30px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="err-page">
|
||||
<div class="err-page__header">501</div>
|
||||
<div class="err-page__text">Not Implemented</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
9
application/src/Presentation/templates/error/errpage.tpl
Normal file
9
application/src/Presentation/templates/error/errpage.tpl
Normal file
@@ -0,0 +1,9 @@
|
||||
<main>
|
||||
<div class="main-section">
|
||||
<div class="errBlock">
|
||||
<div class="center">
|
||||
<?=(!empty($this->data->errorText))?$this->data->errorText:""?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
12
application/src/Presentation/templates/inc/header.tpl
Normal file
12
application/src/Presentation/templates/inc/header.tpl
Normal file
@@ -0,0 +1,12 @@
|
||||
<header class="header">
|
||||
<div class="header__logo">
|
||||
<a class="header__logolink" href="/"> </a>
|
||||
</div>
|
||||
<div class="header__freearea"></div>
|
||||
<div class="header__user"><?=$this->user->fio ?? "Гость"?></div>
|
||||
<div class="header__out">
|
||||
<a href="/?logout">
|
||||
<i class="header__outicon fa-solid fa-right-from-bracket"></i>
|
||||
</a>
|
||||
</div>
|
||||
</header>
|
||||
Reference in New Issue
Block a user