Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4ad36c6034 | ||
|
|
8346234859 | ||
|
|
cb5408be4a | ||
|
|
041ee17263 | ||
|
|
15ec4a46bc | ||
|
|
da2bab7338 | ||
|
|
5b8ef84efa | ||
|
|
9abb4d4d6b | ||
|
|
171def66b8 | ||
|
|
f6b47693a4 | ||
|
|
36861b6967 | ||
|
|
44250475c1 | ||
|
|
a35a50eb53 | ||
|
|
c2d2ad9177 | ||
|
|
035783e264 | ||
|
|
cdf9a13242 | ||
|
|
7df5be904f | ||
|
|
34de164e72 | ||
|
|
19731cb6fe | ||
|
|
4c087a156b | ||
|
|
4ea833db5a | ||
|
|
19b505a458 |
21
.env.dist
21
.env.dist
@@ -8,24 +8,17 @@
|
||||
#
|
||||
# Real environment variables win over .env files.
|
||||
|
||||
# App mode (DEV, TEST, PROD)
|
||||
# PROD / DEV
|
||||
APP_MODE=DEV
|
||||
|
||||
# Including file with array of componens
|
||||
APP_COMPONENTS_FILE="config/components.php"
|
||||
|
||||
# Including file with array of app nodes
|
||||
APP_NODES_FILE="config/appnodes.php"
|
||||
|
||||
# Define app nodes
|
||||
#APP_NODE1='{"key":"/","action":"App\\Auth\\Controllers\\UserController","method":"index"}'
|
||||
#APP_NODE2='{"key":"/","router":"config/routes/main/collection.php"}'
|
||||
APP_COMPONENTS_FILE=application/config/app.php
|
||||
APP_NODES_FILE=application/config/nodes.php
|
||||
CONTAINER_DIR=application/config/container
|
||||
CONTAINER_CACHE=var/cache/container
|
||||
|
||||
# Default page
|
||||
PAGE404="public/web/404.html"
|
||||
PAGE501="public/web/501.html"
|
||||
PAGE404=templates/error/404.tpl
|
||||
PAGE501=templates/error/501.tpl
|
||||
|
||||
# Users environment
|
||||
|
||||
CONTAINER_DIR="config/container"
|
||||
MYSQL_PARAM='{"host":"host.docker.internal", "user":"***user***", "pass":"***password***","base":"***basename***", "logsEnable":true}'
|
||||
10
README.md
10
README.md
@@ -5,15 +5,9 @@
|
||||
Stable version
|
||||
|
||||
```bash
|
||||
composer create-project rmphp/skeleton
|
||||
```
|
||||
```bash
|
||||
composer create-project rmphp/skeleton:"^1.0"
|
||||
composer create-project rmphp/skeleton project-name
|
||||
```
|
||||
|
||||
|
||||
Dev version contains the latest changes
|
||||
|
||||
```bash
|
||||
composer create-project rmphp/skeleton:"1.0.x-dev"
|
||||
composer create-project rmphp/skeleton:"^4.0" project-name
|
||||
```
|
||||
@@ -4,18 +4,18 @@ return [
|
||||
/**
|
||||
* Путь к файлу фабрики возвращающий реализацию RouterInterface или сам экземпляр класса
|
||||
*/
|
||||
\Rmphp\Foundation\RouterInterface::class => 'config/factories/routerFactory.php',
|
||||
\Rmphp\Foundation\RouterInterface::class => 'application/config/factories/routerFactory.php',
|
||||
/**
|
||||
* Путь к файлу фабрики возвращающий реализацию TemplateInterface или сам экземпляр класса
|
||||
*/
|
||||
\Rmphp\Foundation\TemplateInterface::class => 'config/factories/templateFactory.php',
|
||||
\Rmphp\Foundation\TemplateInterface::class => 'application/config/factories/templateFactory.php',
|
||||
/**
|
||||
* Путь к файлу фабрики возвращающий реализацию PSR-3 LoggerInterface или сам экземпляр класса
|
||||
*/
|
||||
\Psr\Log\LoggerInterface::class => 'config/factories/loggerFactory.php',
|
||||
\Psr\Log\LoggerInterface::class => 'application/config/factories/loggerFactory.php',
|
||||
/**
|
||||
* Путь к файлу фабрики возвращающий реализацию PSR-11 ContainerInterface или сам экземпляр класса
|
||||
*/
|
||||
\Psr\Container\ContainerInterface::class => 'config/factories/containerFactory.php',
|
||||
\Psr\Container\ContainerInterface::class => 'application/config/factories/containerFactory.php',
|
||||
];
|
||||
|
||||
15
application/config/container/services.php
Normal file
15
application/config/container/services.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
use Rmphp\Cache\Cache;
|
||||
use Rmphp\Cache\CacheInterface;
|
||||
use Rmphp\Session\Session;
|
||||
use Rmphp\Session\SessionInterface;
|
||||
use Rmphp\Storage\Mysql\MysqlStorage;
|
||||
use Rmphp\Storage\Mysql\MysqlStorageInterface;
|
||||
|
||||
return [
|
||||
MysqlStorageInterface::class => DI\create(MysqlStorage::class)->constructor(json_decode(getenv("MYSQL_PARAM"), true)),
|
||||
SessionInterface::class => DI\create(Session::class)->constructor(),
|
||||
CacheInterface::class => DI\create(Cache::class)->constructor("../var/cache"),
|
||||
'App\*\Domain\Repository\*Repository\*RepositoryInterface' => DI\autowire('App\*\Repository\Mysql\*Repository'),
|
||||
];
|
||||
@@ -2,17 +2,17 @@
|
||||
|
||||
use DI\ContainerBuilder;
|
||||
|
||||
$containerDir = (getenv("CONTAINER_DIR"))?:"config/container/";
|
||||
|
||||
$dependencies = glob(dirname(__DIR__,2)."/".$containerDir."/*.php");
|
||||
$containerDir = (getenv("CONTAINER_DIR")) ?: "application/config/container";
|
||||
$containerCache = (getenv("CONTAINER_CACHE"))?:"var/cache/container";
|
||||
|
||||
$dependencies = glob(dirname(__DIR__,3)."/".$containerDir."/*.php");
|
||||
$dependenciesCollection = array_map(function ($dependenciesFile){
|
||||
return require $dependenciesFile;
|
||||
}, $dependencies);
|
||||
|
||||
try {
|
||||
$builder = new ContainerBuilder();
|
||||
$builder->enableCompilation(__DIR__ . '/../../var/cache/container');
|
||||
if(getenv("APP_MODE") == "PROD") $builder->enableCompilation(dirname(__DIR__,3)."/".$containerCache);
|
||||
$builder->addDefinitions(array_replace_recursive(...$dependenciesCollection));
|
||||
return $builder->build();
|
||||
} catch (Exception $e) {echo $e->getMessage();}
|
||||
2
application/config/factories/loggerFactory.php
Normal file
2
application/config/factories/loggerFactory.php
Normal file
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
return (new \Monolog\Logger('system'))->pushHandler(new \Monolog\Handler\StreamHandler(dirname(__DIR__, 3).'/var/logs/log'.date('Ymd').'.log'));
|
||||
2
application/config/factories/templateFactory.php
Normal file
2
application/config/factories/templateFactory.php
Normal file
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
return (new \Rmphp\Content\Content('/templates/base.tpl'))->setSubtemplatePath('/templates');
|
||||
14
application/config/nodes.php
Normal file
14
application/config/nodes.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/**
|
||||
* Правила для точек монтирования слоев
|
||||
*/
|
||||
|
||||
# Example:
|
||||
# ['key'=>'/', "action"=>"App\\Main\\Controllers\\IndexController", "method"=>"index"],
|
||||
# ['key'=>'/', 'router'=>'config/routes/main/routes.php'],
|
||||
# ['key'=>'/', 'router'=>[]],
|
||||
|
||||
return [
|
||||
['key'=>'/', 'router'=>'application/config/routes/main/routes.php'],
|
||||
];
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
params: ""
|
||||
|
||||
# Empty
|
||||
- key: "[any]"
|
||||
- key: "<@any>"
|
||||
routes:
|
||||
- action: "App\\Main\\Controllers\\IndexController"
|
||||
method: "emptyAction"
|
||||
149
application/src/Controllers/AbstractController.php
Normal file
149
application/src/Controllers/AbstractController.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace Base\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;
|
||||
}
|
||||
}
|
||||
35
application/src/Controllers/AbstractPageController.php
Normal file
35
application/src/Controllers/AbstractPageController.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Controllers;
|
||||
|
||||
use Base\Repository\RepositoryException;
|
||||
use Base\Services\DTOException;
|
||||
use Base\Services\ServiceException;
|
||||
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 {
|
||||
if($e instanceof DTOException || $e instanceof ServiceException || $e instanceof RepositoryException) return $e->getMessage();
|
||||
($e instanceof Exception) ? $this->logException($e) : $this->logError($e);
|
||||
return "Ошибка. Дата и время: ".date("d-m-Y H:i:s");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Common\Controllers;
|
||||
namespace Base\Controllers;
|
||||
|
||||
class NotFoundException extends \Exception {
|
||||
|
||||
82
application/src/Domain/AbstractObject.php
Normal file
82
application/src/Domain/AbstractObject.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
*/
|
||||
|
||||
namespace Base\Domain;
|
||||
|
||||
abstract class AbstractObject {
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return static
|
||||
*/
|
||||
public static function fromData(array $data) : static {
|
||||
$self = new static();
|
||||
$self->setProperties($data);
|
||||
return $self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return $this
|
||||
*/
|
||||
public function setProperties(array $data) : self {
|
||||
$propArray = array_keys(get_class_vars(get_class($this)));
|
||||
foreach ($propArray as $propName)
|
||||
{
|
||||
$propNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $propName));
|
||||
|
||||
// если в переданном массиве ключ и свойство совпадают optionId = optionId
|
||||
if(isset($data[$propName])) {
|
||||
$this->{$propName} = (method_exists($this, 'set'.ucfirst($propName))) ? $this->{'set'.ucfirst($propName)}($data[$propName]) : $data[$propName];
|
||||
}
|
||||
// если свойство в снэйккесе совподает с ключем в массиве option_id = optionId
|
||||
elseif(isset($data[$propNameSnakeCase])) {
|
||||
$this->{$propName} = (method_exists($this, 'set'.ucfirst($propName))) ? $this->{'set'.ucfirst($propName)}($data[$propNameSnakeCase]) : $data[$propNameSnakeCase];
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable|null $method
|
||||
* @return array
|
||||
*/
|
||||
public function getProperties(callable $method = null) : array {
|
||||
$objectData = get_object_vars($this);
|
||||
foreach ($objectData as $fieldName => $value)
|
||||
{
|
||||
$fieldNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $fieldName));
|
||||
|
||||
// если есть внутренний метод
|
||||
if(method_exists($this, 'get'.ucfirst($fieldName))) {
|
||||
$out[$fieldNameSnakeCase] = $this->{'get'.ucfirst($fieldName)}($value);
|
||||
}
|
||||
// если есть callable функция
|
||||
elseif(isset($method) && !is_array($value)) {
|
||||
$out[$fieldNameSnakeCase] = $method($value);
|
||||
}
|
||||
// если это логическое значение
|
||||
elseif(is_bool($value)){
|
||||
$out[$fieldNameSnakeCase] = (int) $value;
|
||||
}
|
||||
// если это дробное число
|
||||
elseif(is_float($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
// если это целое число
|
||||
elseif(is_int($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
// если это строка
|
||||
elseif(is_string($value)) {
|
||||
$out[$fieldNameSnakeCase] = $value;
|
||||
}
|
||||
|
||||
}
|
||||
return $out ?? [];
|
||||
}
|
||||
|
||||
}
|
||||
35
application/src/Repository/AbstractMysqlRepository.php
Normal file
35
application/src/Repository/AbstractMysqlRepository.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Repository;
|
||||
|
||||
use Base\Domain\AbstractObject;
|
||||
use Rmphp\Storage\Mysql\MysqlStorageInterface;
|
||||
|
||||
abstract class AbstractMysqlRepository {
|
||||
|
||||
public function __construct(
|
||||
public readonly MysqlStorageInterface $mysql
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
public function saveEntity(AbstractObject $object, string $table) : AbstractObject {
|
||||
$in = $object->getProperties(function ($value){
|
||||
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
|
||||
});
|
||||
try {
|
||||
if (!empty($object->id) && !empty($this->mysql->findById($table, $object->id))) {
|
||||
$this->mysql->updateById($table, $in, $object->id);
|
||||
} else {
|
||||
$this->mysql->insert($table, $in);
|
||||
$object->id = $this->mysql->mysql()->insert_id;
|
||||
}
|
||||
} catch (\Throwable $throwable) {throw new RepositoryException($throwable->getMessage());}
|
||||
return $object;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Common\Repository;
|
||||
namespace Base\Repository;
|
||||
|
||||
use Throwable;
|
||||
|
||||
81
application/src/Services/AbstractDTO.php
Normal file
81
application/src/Services/AbstractDTO.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Services;
|
||||
|
||||
use Exception;
|
||||
|
||||
abstract class AbstractDTO {
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return static
|
||||
* @throws Exception
|
||||
*/
|
||||
static function fromArray(array $data) : static {
|
||||
$self = new static();
|
||||
|
||||
$propArray = array_keys(get_class_vars(get_class($self)));
|
||||
|
||||
foreach($propArray as $propName){
|
||||
$propNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $propName));
|
||||
if(method_exists($self, "requireAssert".ucfirst($propName))){
|
||||
if(isset($data[$propName])) {
|
||||
$self->{"requireAssert".ucfirst($propName)}($data[$propName]);
|
||||
}
|
||||
elseif(isset($data[$propNameSnakeCase])) {
|
||||
$self->{"requireAssert".ucfirst($propName)}($data[$propNameSnakeCase]);
|
||||
}
|
||||
else{
|
||||
throw new DTOException("Отсутствует обязательное значение");
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach($data as $key => $item){
|
||||
try {
|
||||
$camelCaseKey = str_replace('_', '', ucwords($key, "_"));
|
||||
if(method_exists($self, "assert".$camelCaseKey)) {
|
||||
$self->{'assert'.$camelCaseKey}($item);
|
||||
} elseif(in_array(lcfirst($camelCaseKey), $propArray)) {
|
||||
$self->{lcfirst($camelCaseKey)} = $item;
|
||||
}
|
||||
}
|
||||
catch (\Error $error) {
|
||||
$errorKey[$key] = $error->getMessage();
|
||||
}
|
||||
if(!empty($errorKey)) throw new Exception(json_encode(["data"=>$data, "error"=>$errorKey]));
|
||||
}
|
||||
return $self;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
public function validateInt($value) : bool {
|
||||
return (is_numeric($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
public function validateFloat($value) : bool {
|
||||
return (is_float((float) $value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
public function validateBoolean($value) : bool {
|
||||
return (is_bool($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
public function validateEmail($value) : bool {
|
||||
return (filter_var($value, FILTER_VALIDATE_EMAIL));
|
||||
}
|
||||
}
|
||||
10
application/src/Services/AbstractService.php
Normal file
10
application/src/Services/AbstractService.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Services;
|
||||
|
||||
use Rmphp\Kernel\Main;
|
||||
|
||||
abstract class AbstractService extends Main {
|
||||
|
||||
|
||||
}
|
||||
16
application/src/Services/DTOException.php
Normal file
16
application/src/Services/DTOException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Base\Services;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class DTOException extends \Exception {
|
||||
|
||||
public array $data;
|
||||
|
||||
public function __construct($message="", $code=0, array $data = [], Throwable $previous=null) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->data = $data;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Common\Services;
|
||||
namespace Base\Services;
|
||||
|
||||
use Throwable;
|
||||
|
||||
@@ -12,14 +12,17 @@
|
||||
"monolog/monolog": "^2.3",
|
||||
"php-di/php-di": "^6.3",
|
||||
"symfony/dotenv": "^6.2",
|
||||
"rmphp/kernel": "^1.0",
|
||||
"rmphp/kernel": "^4.0",
|
||||
"rmphp/router": "^1.0",
|
||||
"rmphp/content": "^1.0",
|
||||
"rmphp/storage": "^1.0"
|
||||
"rmphp/content": "^3.0",
|
||||
"rmphp/storage": "^3.0",
|
||||
"rmphp/session": "^1.0",
|
||||
"rmphp/cache-file": "^1.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "src"
|
||||
"App\\": "src",
|
||||
"Base\\": "application/src"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
@@ -30,6 +33,7 @@
|
||||
"process-timeout":0
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/var-dumper": "^5.3",
|
||||
"rmphp/var-damper": "^1.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Правила для точек монтирования модулей
|
||||
* Каждый массив определяет порядок срабатывания, часть url с которого
|
||||
*/
|
||||
|
||||
# Example:
|
||||
# ['key'=>'/', "action"=>"App\\Main\\Controllers\\IndexController", "method"=>"index"],
|
||||
# ['key'=>'/', 'router'=>'config/routes/main/collection.php'],
|
||||
# ['key'=>'/', 'router'=>'config/routes/main.json']
|
||||
# ['key'=>'/', 'router'=>'config/routes/main.yaml'],
|
||||
|
||||
return [
|
||||
['key'=>'/', 'router'=>'config/routes/main.yaml'],
|
||||
];
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Rmphp\Storage\MysqlStorage;
|
||||
use Rmphp\Storage\MysqlStorageInterface;
|
||||
|
||||
return [
|
||||
MysqlStorageInterface::class => DI\create(MysqlStorage::class)->constructor(json_decode(getenv("MYSQL_PARAM"), true)),
|
||||
'App\*\Domain\Repository\*RepositoryInterface' => DI\autowire('App\*\Repository\Mysql\*Repository'),
|
||||
'App\*\Services\*\*Service' => DI\autowire('App\*\Services\*\*Service'),
|
||||
'App\*\Services\*Service' => DI\autowire('App\*\Services\*Service'),
|
||||
];
|
||||
@@ -1,2 +0,0 @@
|
||||
<?php
|
||||
return (new \Monolog\Logger('system'))->pushHandler(new \Monolog\Handler\StreamHandler(__DIR__.'/../../var/logs/log'.date('Ymd').'.log'));
|
||||
@@ -1,2 +0,0 @@
|
||||
<?php
|
||||
return (new \Rmphp\Content\Content('public/web/main/sheet.html'))->setSubtemplePath('templates');
|
||||
@@ -1,14 +0,0 @@
|
||||
[
|
||||
{
|
||||
"key": "/",
|
||||
"routes": [
|
||||
{"action": "App\\Main\\Controllers\\IndexController", "method": "index", "params": ""}
|
||||
]
|
||||
},
|
||||
{
|
||||
"key": "[any]",
|
||||
"routes": [
|
||||
{"action": "App\\Main\\Controllers\\IndexController", "method": "emptyAction", "params": ""}
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -1,12 +0,0 @@
|
||||
- key: "/"
|
||||
routes:
|
||||
- action: "App\\Main\\Controllers\\IndexController"
|
||||
method: "index"
|
||||
params: ""
|
||||
|
||||
# Empty
|
||||
- key: "[any]"
|
||||
routes:
|
||||
- action: "App\\Main\\Controllers\\IndexController"
|
||||
method: "emptyAction"
|
||||
params: ""
|
||||
40
public/assets/css/style.css
Normal file
40
public/assets/css/style.css
Normal file
@@ -0,0 +1,40 @@
|
||||
body{
|
||||
font-family: Arial, sans-serif;
|
||||
font-size: 15px;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background:#d4e5d8;
|
||||
}
|
||||
* {
|
||||
scrollbar-width: thin;
|
||||
scrollbar-color: #CCC #FFF
|
||||
}
|
||||
*::-webkit-scrollbar {
|
||||
width: 10px;
|
||||
}
|
||||
*::-webkit-scrollbar-track {
|
||||
background: #FFF;
|
||||
}
|
||||
*::-webkit-scrollbar-thumb {
|
||||
background-color: #AAA;
|
||||
border-radius: 20px;
|
||||
border: 3px solid #FFF;
|
||||
}
|
||||
|
||||
h1{padding: 0 30px}
|
||||
|
||||
.main-block{
|
||||
display: flex;
|
||||
align-items: center;
|
||||
height: 50vh;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
color:#888;
|
||||
}
|
||||
.main-block__header{
|
||||
font-size: 70px;
|
||||
}
|
||||
.main-block__text{
|
||||
font-size: 30px;
|
||||
}
|
||||
|
||||
@@ -12,9 +12,6 @@ require_once dirname(__DIR__).'/vendor/autoload.php';
|
||||
(new Symfony\Component\Dotenv\Dotenv())->usePutenv()->loadEnv(dirname(__DIR__).'/.env');
|
||||
|
||||
error_reporting(0); ini_set('display_errors','Off');
|
||||
if(getenv("APP_MODE") == 'DEV'){
|
||||
error_reporting(E_ALL); ini_set('display_errors','On');
|
||||
}
|
||||
|
||||
$request = ServerRequestFactory::fromGlobals();
|
||||
|
||||
@@ -22,18 +19,7 @@ $app = new App();
|
||||
$response = $app->handler($request, (new Response())->withHeader("Content-Type", "text/html; charset=utf-8"));
|
||||
(new ResponseEmitter())->emit($response);
|
||||
|
||||
|
||||
if(getenv("APP_MODE") == 'DEV' && in_array("Dev", $response->getHeader("App-Mode"))){
|
||||
$app->syslogger()->dump("request", $request);
|
||||
$app->syslogger()->dump("response", $response);
|
||||
$app->syslogger()->dump("globals", [
|
||||
"ENV"=>$_ENV,
|
||||
"GET"=>$request->getQueryParams(),
|
||||
"POST"=>$request->getParsedBody(),
|
||||
"COOKIE"=>$request->getCookieParams(),
|
||||
"SESSION"=>$_SESSION ?? [],
|
||||
"SERVER"=>$request->getServerParams()
|
||||
]);
|
||||
$app->syslogger()->dump("kernel", $app);
|
||||
if(($response->getStatusCode() !== 200 && getenv("APP_MODE") == 'DEV') || in_array("Dev", $response->getHeader("App-Mode"))){
|
||||
$app->syslogger()->dump("Response", $response);
|
||||
addShutdownInfo($app->syslogger()->getLogs());
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>404</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Error 404</h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,10 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>500</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Error 500</h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,18 +0,0 @@
|
||||
body{font-family: Arimo, sans-serif; font-size: 15px; margin: 0; padding: 0;}
|
||||
* {
|
||||
scrollbar-width: thin;
|
||||
scrollbar-color: #CCC #FFF
|
||||
}
|
||||
*::-webkit-scrollbar {
|
||||
width: 10px;
|
||||
}
|
||||
*::-webkit-scrollbar-track {
|
||||
background: #FFF;
|
||||
}
|
||||
*::-webkit-scrollbar-thumb {
|
||||
background-color: #AAA;
|
||||
border-radius: 20px;
|
||||
border: 3px solid #FFF;
|
||||
}
|
||||
|
||||
h1{padding: 0 30px}
|
||||
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Common\Controllers;
|
||||
|
||||
use Laminas\Diactoros\Response\HtmlResponse;
|
||||
use Laminas\Diactoros\Response\JsonResponse;
|
||||
use Laminas\Diactoros\Response\RedirectResponse;
|
||||
use Laminas\Diactoros\Response\TextResponse;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Rmphp\Kernel\Main;
|
||||
|
||||
abstract class AbstractController extends Main {
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function containerGet(string $name) {
|
||||
try {
|
||||
return $this->container()->get($name);
|
||||
}
|
||||
catch (NotFoundExceptionInterface $notFoundException){
|
||||
$this->syslogger()->error($notFoundException->getMessage());
|
||||
}
|
||||
catch (ContainerExceptionInterface $containerException){
|
||||
$this->syslogger()->error($containerException->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 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 {
|
||||
$this->container()->set("showDebugLogs", false);
|
||||
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 {
|
||||
$this->container()->set("showDebugLogs", false);
|
||||
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 {
|
||||
$this->container()->set("showDebugLogs", false);
|
||||
return new RedirectResponse($url, $status, array_merge($this->globals()->response()->getHeaders(), $headers));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Common\Controllers;
|
||||
|
||||
abstract class AbstractPageController extends AbstractController {
|
||||
|
||||
/**
|
||||
* @param string $point
|
||||
* @param string $string
|
||||
*/
|
||||
public function templateAddValue(string $point, string $string) : void {
|
||||
$this->template()->addValue($point, $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $point
|
||||
* @param string $subTempl
|
||||
* @param array $resource
|
||||
*/
|
||||
public function templateSetSubtemple(string $point, string $subTempl, array $resource = []) : void {
|
||||
$this->template()->setSubtemple($point, $subTempl, $resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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->templateSetSubtemple("main", "/main/errpage.tpl", [
|
||||
"errorText" => "<span style='color:red'>Error: ".$exception->getMessage()." (".$exception->getCode().")"."</span>"
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Common\Services;
|
||||
|
||||
use Rmphp\Kernel\Main;
|
||||
|
||||
class AbstractService extends Main {
|
||||
|
||||
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace App\Main\Controllers;
|
||||
use App\Common\Controllers\AbstractPageController;
|
||||
use App\Common\Services\ServiceException;
|
||||
use Base\Controllers\AbstractPageController;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
|
||||
@@ -13,13 +12,13 @@ class IndexController extends AbstractPageController {
|
||||
*/
|
||||
public function index() : bool|ResponseInterface {
|
||||
try {
|
||||
$this->addHeader("App-Mode", "Dev");
|
||||
//$this->addHeader("App-Mode", "Dev");
|
||||
$this->template()->setValue("title", "Главная");
|
||||
$this->template()->setSubtemple("main", "main/index.tpl", [
|
||||
$this->template()->setSubtemplate("main", "/main/index.tpl", [
|
||||
"date" => (new \DateTime())->format('Y-m-d H:i:s')
|
||||
]);
|
||||
}
|
||||
catch(ServiceException $exception){}
|
||||
return true;
|
||||
catch(\Throwable $e){$error = $this->checkError($e);}
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,15 +5,11 @@
|
||||
<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="/web/main/css/style.css?1" rel="stylesheet">
|
||||
<!--[if lt IE 9]>
|
||||
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
|
||||
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
|
||||
<![endif]-->
|
||||
<link href="/assets/css/style.css?1" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<?=$this->getPoint('main')?>
|
||||
<script type="text/javascript" src="/web/main/js/script.js?1"></script>
|
||||
<script type="text/javascript" src="/assets/js/script.js?1"></script>
|
||||
<?=$this->getPoint('jsscript')?>
|
||||
</body>
|
||||
</html>
|
||||
38
templates/error/404.tpl
Normal file
38
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
templates/error/501.tpl
Normal file
38
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>
|
||||
@@ -1,5 +1,8 @@
|
||||
<article>
|
||||
<main>
|
||||
<h1>Hellow</h1>
|
||||
<div class="main-block">
|
||||
<div class="main-block__header">Hello</div>
|
||||
<div class="main-block__text">Now is <?=$this->date?></div>
|
||||
</div>
|
||||
</main>
|
||||
</article>
|
||||
Reference in New Issue
Block a user