21 Commits
4.2.1 ... 5.x

Author SHA1 Message Date
User
1106b4af47 20250807#1 2025-08-07 01:31:59 +03:00
User
a2650ab53f 20250706#2 2025-07-06 20:12:09 +03:00
User
0558bb121d 20250706#1 2025-07-06 20:11:27 +03:00
User
b5ecb1fc86 20250622#1 2025-06-22 13:54:05 +03:00
User
618d0029fa 20250419#3 2025-04-19 19:02:53 +03:00
User
4f38812468 20250419#2 2025-04-19 19:00:46 +03:00
User
b263351289 20250419#1 2025-04-19 19:00:02 +03:00
User
2657310000 20250330#1 2025-03-30 19:23:57 +03:00
User
7d5538ad35 20250327#1 2025-03-27 03:15:13 +03:00
User
c2a14307ae 20250321#1 2025-03-21 21:50:55 +03:00
User
ddb82ee395 20250303#4 2025-03-03 15:25:46 +03:00
User
a095b76e8d 20250303#3 2025-03-03 14:30:02 +03:00
User
70810412f4 20250303#2 2025-03-03 14:25:44 +03:00
User
fee7f1466c 20250303#1 2025-03-03 00:41:34 +03:00
User
a05a8c8dd1 20250302#1 2025-03-02 23:46:57 +03:00
User
133371a908 20250228#2 2025-02-28 13:39:44 +03:00
User
49ae47e43e 20250228#1 2025-02-28 13:11:14 +03:00
User
8fc94aaeb8 20250224#1 2025-02-24 19:45:12 +03:00
User
aba089db5e 20250218#1 2025-02-18 02:39:16 +03:00
User
43e529dcad 20250128#2 2025-01-28 14:21:33 +03:00
User
5bef4f577e 20250128#1 2025-01-28 13:23:44 +03:00
26 changed files with 174 additions and 95 deletions

View File

@@ -8,11 +8,12 @@
#
# Real environment variables win over .env files.
# PROD / DEV
# DEBUG / DEV / PROD
APP_MODE=DEV
APP_NODES_FILE=application/config/app.php
APP_COMPONENTS_FILE=application/config/components.php
CONTAINER_INI=application/config/container.php
APP_NODES_FILE=config/app.php
CLI_NODES_FILE=config/app-cli.php
APP_COMPONENTS_FILE=config/components.php
CONTAINER_INI=config/container.php
CONTAINER_CACHE=var/cache/container
# Default page
@@ -20,5 +21,5 @@ PAGE404="/templates/error/404.tpl"
PAGE501="/templates/error/501.tpl"
# Users environment
MYSQL_PARAM='{"host":"host.docker.internal", "user":"***user***", "pass":"***password***","base":"***basename***", "logsEnable":true}'
MYSQL_PARAM='{"host":"host.docker.internal", "user":"***user***", "pass":"***password***","base":"***basename***", "charset":"utf8mb4", "logsEnable":true}'
REDIS_PARAM='{"host":"127.0.0.1","port":6379,"connectTimeout":2.5,"backoff":{"algorithm":1,"base":500,"cap":750}, "database":0, "defaultExpire":300}'

View File

@@ -9,5 +9,5 @@ composer create-project rmphp/skeleton project-name
```
```bash
composer create-project rmphp/skeleton:"^4.2" project-name
composer create-project rmphp/skeleton:"^5.1" project-name
```

View File

@@ -1,27 +0,0 @@
<?php
namespace Base\Application;
use ReflectionClass;
abstract class AbstractDTO {
/**
* @param array $data
* @return static
*/
static function fromArray(array $data) : static {
$class = new ReflectionClass(static::class);
$self = new static();
foreach ($class->getProperties() as $property) {
$propertyNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
// data[propertyName] ?? data[property_name] ?? null
$value = $data[$property->getName()] ?? $data[$propertyNameSnakeCase] ?? null;
// если есть внутренний метод (приоритетная обработка)
if($class->hasMethod('set'.ucfirst($property->getName()))) $self->{'set'.ucfirst($property->getName())}($value);
// прямое присовение по умолчанию
elseif(isset($value)) $self->{$property->getName()} = $value;
}
return $self;
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace Base\Application;
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;
}
}

View File

@@ -3,33 +3,19 @@
namespace Base\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", "/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();
if($e instanceof DomainException || $e instanceof ApplicationException) return $e->getMessage();
return "Ошибка. Дата и время: ".date("d-m-Y H:i:s");
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Base\Handlers;
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\Response\TextResponse;
use Psr\Http\Message\ResponseInterface;
use Rmphp\Kernel\Main;
use Throwable;
abstract class AbstractHandler 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);
}
}

3
application/bin/cli Normal file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/php
<?php
require_once dirname(__FILE__).'/cli.php';

13
application/bin/cli.php Normal file
View File

@@ -0,0 +1,13 @@
<?php
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use Rmphp\Kernel\AppCli;
use Rmphp\Kernel\ResponseEmitter;
require_once dirname(__DIR__,2).'/vendor/autoload.php';
(new Symfony\Component\Dotenv\Dotenv())->usePutenv()->loadEnv(dirname(__DIR__,2).'/.env');
$app = new AppCli();
$response = $app->handler(ServerRequestFactory::fromGlobals(), new Response());
(new ResponseEmitter())->emit($response);

View File

@@ -1,5 +0,0 @@
<?php
return [
"application/config/container/services.php",
"application/config/container/settings.php",
];

View File

@@ -5,20 +5,20 @@
"minimum-stability": "dev",
"prefer-stable" : true,
"require": {
"php": "^8.1",
"php": "^8.3",
"ext-json": "*",
"ext-yaml": "*",
"laminas/laminas-diactoros": "^2.5",
"monolog/monolog": "^2.3",
"php-di/php-di": "^6.3",
"ramsey/uuid": "^4.7",
"rmphp/content": "^3.1",
"rmphp/kernel": "^5.0",
"rmphp/router": "^1.2",
"laminas/laminas-diactoros": "^2.26",
"monolog/monolog": "^2.10",
"php-di/php-di": "^6.4",
"ramsey/uuid": "^4.9",
"rmphp/content": "^4.1",
"rmphp/kernel": "^6.1",
"rmphp/router": "^2.1",
"rmphp/session": "^1.1",
"rmphp/redis": "^1.0",
"rmphp/storage": "^4.1",
"symfony/dotenv": "^6.2"
"rmphp/storage": "^12.0",
"symfony/dotenv": "^6.4"
},
"autoload": {
"psr-4": {
@@ -34,7 +34,7 @@
"process-timeout":0
},
"require-dev": {
"symfony/var-dumper": "^5.3",
"rmphp/var-damper": "^1.0"
"symfony/var-dumper": "^5.4",
"rmphp/var-damper": "^1.1"
}
}

19
config/app-cli.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
# Example:
# ['key'=>'/', "action"=>"App\\Main\\Controllers\\IndexController", "method"=>"index"],
# ['key'=>'', 'router'=>'config/routes-cli/routes.php'],
# ['key'=>'/', 'router'=>[]],
return [
['key'=>'', 'router'=>[
[
'key'=>'index',
'routes' => [
[
'action' => "App\Infrastructure\Handlers\IndexHandler",
'method' => "index"
]
]
]
]],
];

View File

@@ -5,9 +5,9 @@
# Example:
# ['key'=>'/', "action"=>"App\\Main\\Controllers\\IndexController", "method"=>"index"],
# ['key'=>'/', 'router'=>'application/config/routes/main/routes.php'],
# ['key'=>'/', 'router'=>'config/routes/main/routes.php'],
# ['key'=>'/', 'router'=>[]],
return [
['key'=>'/', 'router'=>'application/config/routes/routes.php'],
['key'=>'/', 'router'=>'config/routes/routes.php'],
];

View File

@@ -4,17 +4,17 @@ return [
/**
* Путь к файлу фабрики возвращающий реализацию RouterInterface или сам экземпляр класса
*/
\Rmphp\Foundation\RouterInterface::class => 'application/config/components/routerFactory.php',
\Rmphp\Foundation\RouterInterface::class => 'config/components/routerFactory.php',
/**
* Путь к файлу фабрики возвращающий реализацию TemplateInterface или сам экземпляр класса
*/
\Rmphp\Foundation\TemplateInterface::class => 'application/config/components/templateFactory.php',
\Rmphp\Foundation\TemplateInterface::class => 'config/components/templateFactory.php',
/**
* Путь к файлу фабрики возвращающий реализацию PSR-3 LoggerInterface или сам экземпляр класса
*/
\Psr\Log\LoggerInterface::class => 'application/config/components/loggerFactory.php',
\Psr\Log\LoggerInterface::class => 'config/components/loggerFactory.php',
/**
* Путь к файлу фабрики возвращающий реализацию PSR-11 ContainerInterface или сам экземпляр класса
*/
\Psr\Container\ContainerInterface::class => 'application/config/components/containerFactory.php',
\Psr\Container\ContainerInterface::class => 'config/components/containerFactory.php',
];

View File

@@ -2,18 +2,18 @@
use DI\ContainerBuilder;
$containerIni = (getenv("CONTAINER_INI")) ?: "application/config/container.php";
$containerIni = (getenv("CONTAINER_INI")) ?: "config/container.php";
$containerCache = (getenv("CONTAINER_CACHE")) ?: "var/cache/container";
$dependencies = require dirname(__DIR__,3).'/'.$containerIni;
$dependencies = require dirname(__DIR__,2).'/'.$containerIni;
$dependenciesCollection = array_map(function ($dependenciesFile){
return require dirname(__DIR__,3)."/".$dependenciesFile;
return require dirname(__DIR__,2)."/".$dependenciesFile;
}, $dependencies);
try {
$builder = new ContainerBuilder();
if(getenv("APP_MODE") == "PROD") $builder->enableCompilation(dirname(__DIR__,3)."/".$containerCache);
if(getenv("APP_MODE") == "PROD") $builder->enableCompilation(dirname(__DIR__,2)."/".$containerCache);
$builder->addDefinitions(array_replace_recursive(...$dependenciesCollection));
return $builder->build();
} catch (Exception $e) {echo $e->getMessage();}

View File

@@ -1,2 +1,2 @@
<?php
return (new \Monolog\Logger('system'))->pushHandler(new \Monolog\Handler\StreamHandler(dirname(__DIR__, 3).'/var/logs/log'.date('Ymd').'.log'));
return (new \Monolog\Logger('system'))->pushHandler(new \Monolog\Handler\StreamHandler(dirname(__DIR__, 2).'/var/logs/log'.date('Ymd').'.log'));

6
config/container.php Normal file
View File

@@ -0,0 +1,6 @@
<?php
return [
"config/container/params.php",
"config/container/services.php",
"config/container/settings.php",
];

View File

@@ -0,0 +1,6 @@
<?php
return [
'MYSQL_PARAM' => json_decode(getenv("MYSQL_PARAM"), true),
'REDIS_PARAM' => json_decode(getenv("REDIS_PARAM"), true),
];

View File

@@ -4,7 +4,7 @@ use Rmphp\Storage\Mysql\MysqlStorage;
use Rmphp\Storage\Mysql\MysqlStorageInterface;
return [
MysqlStorageInterface::class => DI\create(MysqlStorage::class)->constructor(json_decode(getenv("MYSQL_PARAM"), true)),
MysqlStorageInterface::class => DI\create(MysqlStorage::class)->constructor(DI\get("MYSQL_PARAM")),
'App\Domain\Repository\*RepositoryInterface' => DI\autowire('App\Infrastructure\Repository\*Repository'),
'App\*\Domain\Repository\*RepositoryInterface' => DI\autowire('App\*\Infrastructure\Repository\*Repository'),
];

View File

@@ -1,6 +1,6 @@
<?php
$cashFile = preg_replace("'.application.*$'",'', __DIR__).'/var/routes/'.md5(__FILE__);
$cashFile = dirname(__DIR__,2).'/var/routes/'.md5(__FILE__);
if(getenv("APP_MODE") == "PROD" && file_exists($cashFile)){
return unserialize(file_get_contents($cashFile));

View File

@@ -23,7 +23,7 @@ $app = new App();
$response = $app->handler($request, new Response());
(new ResponseEmitter())->emit($response);
if(($response->getStatusCode() !== 200 && getenv("APP_MODE") == 'DEV') || in_array("Dev", $response->getHeader("App-Mode"))){
if(($response->getStatusCode() !== 200 && in_array(getenv("APP_MODE"), ['DEBUG', 'DEV'])) || in_array("Dev", $response->getHeader("App-Mode"))){
$app->syslogger()->dump("Response", $response);
addShutdownInfo($app->syslogger()->getLogs());
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: Zuev Yuri
* Date: 02.03.2025
* Time: 22:02
*/
namespace App\Infrastructure\Handlers;
use Base\Handlers\AbstractHandler;
use Psr\Http\Message\ResponseInterface;
class IndexHandler extends AbstractHandler {
public function index() : bool|ResponseInterface {
return $this->textResponse((new \DateTime())->format('Y-m-d H:i:s'));
}
}