Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cc61749c7 | ||
|
|
6bd9e68d91 | ||
|
|
f5f51ef99c | ||
|
|
97f6629907 | ||
|
|
9fb52d116d | ||
|
|
1ffad8d84a | ||
|
|
1d37f96799 | ||
|
|
e500d67171 | ||
|
|
cf621dbaef | ||
|
|
5eaf931847 | ||
|
|
efe3b0ad5a | ||
|
|
d9a3deef90 | ||
|
|
0c121af290 | ||
|
|
7b03cf952e | ||
|
|
33e3fba104 | ||
|
|
2a620798b3 | ||
|
|
7658fc88e6 | ||
|
|
5c79d63d6c |
@@ -9,6 +9,7 @@ Stable version
|
|||||||
```bash
|
```bash
|
||||||
composer require rmphp/kernel
|
composer require rmphp/kernel
|
||||||
```
|
```
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/kernel:"^1.0"
|
composer require rmphp/kernel:"^6.0"
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
"ext-yaml": "*",
|
"ext-yaml": "*",
|
||||||
"psr/log": "^3.0.0",
|
"psr/log": "^3.0.0",
|
||||||
"psr/container": "^1.0",
|
"psr/container": "^1.0",
|
||||||
"rmphp/foundation": "^1.0"
|
"rmphp/foundation": "^3.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|||||||
73
src/App.php
73
src/App.php
@@ -4,6 +4,7 @@ declare(strict_types = 1);
|
|||||||
|
|
||||||
namespace Rmphp\Kernel;
|
namespace Rmphp\Kernel;
|
||||||
|
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
@@ -33,7 +34,8 @@ class App extends Main {
|
|||||||
*/
|
*/
|
||||||
public function handler(ServerRequestInterface $request, ResponseInterface $response) : ResponseInterface {
|
public function handler(ServerRequestInterface $request, ResponseInterface $response) : ResponseInterface {
|
||||||
try{
|
try{
|
||||||
$this->init($request, $response);
|
$this->setGlobals($request, $response);
|
||||||
|
$this->init();
|
||||||
$this->syslogger()->dump("Request", $request);
|
$this->syslogger()->dump("Request", $request);
|
||||||
$this->syslogger()->dump("Router", $this->router);
|
$this->syslogger()->dump("Router", $this->router);
|
||||||
$this->syslogger()->dump("routes", $this->appRoutes);
|
$this->syslogger()->dump("routes", $this->appRoutes);
|
||||||
@@ -41,14 +43,14 @@ class App extends Main {
|
|||||||
foreach ($this->appRoutes as $appRouteKey => $appHandler){
|
foreach ($this->appRoutes as $appRouteKey => $appHandler){
|
||||||
|
|
||||||
if(!$appHandler instanceof MatchObject) continue;
|
if(!$appHandler instanceof MatchObject) continue;
|
||||||
$response = null;
|
$handlerResponse = null;
|
||||||
|
|
||||||
if(!empty($appHandler->className)){
|
if(!empty($appHandler->className)){
|
||||||
if(!class_exists($appHandler->className)) {
|
if(!class_exists($appHandler->className)) {
|
||||||
$this->syslogger()->log("handlers", "Err - Class ".$appHandler->className." is not exists");
|
$this->syslogger()->log("handlers", "Err - Class ".$appHandler->className." is not exists");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$controllers[$appRouteKey] = new $appHandler->className;
|
$controllers[$appRouteKey] = ($this->container() instanceof ContainerInterface) ? $this->container()->get($appHandler->className) : new $appHandler->className;
|
||||||
$log = "Class ".$appHandler->className;
|
$log = "Class ".$appHandler->className;
|
||||||
|
|
||||||
if(!empty($appHandler->methodName)){
|
if(!empty($appHandler->methodName)){
|
||||||
@@ -56,30 +58,19 @@ class App extends Main {
|
|||||||
$this->syslogger()->log("handlers", "Err - Method ".$appHandler->className."/".$appHandler->methodName." is not exists");
|
$this->syslogger()->log("handlers", "Err - Method ".$appHandler->className."/".$appHandler->methodName." is not exists");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$response = (!empty($appHandler->params)) ? $controllers[$appRouteKey]->{$appHandler->methodName}(...$appHandler->params) : $controllers[$appRouteKey]->{$appHandler->methodName}();
|
$handlerResponse = (!empty($appHandler->params)) ? $controllers[$appRouteKey]->{$appHandler->methodName}(...$appHandler->params) : $controllers[$appRouteKey]->{$appHandler->methodName}();
|
||||||
$log = "Method ".$appHandler->className."/".$appHandler->methodName;
|
$log = "Method ".$appHandler->className."/".$appHandler->methodName;
|
||||||
}
|
}
|
||||||
$this->syslogger()->log("handlers", "OK - ".$log);
|
$this->syslogger()->log("handlers", "OK - ".$log);
|
||||||
/**
|
|
||||||
* 1. Если на этапе итерации уже получен ответ ResponseInterface - досрочно отдаем результат в эмиттер
|
if($handlerResponse instanceof ResponseInterface) {
|
||||||
*/
|
return $handlerResponse;
|
||||||
if($response instanceof ResponseInterface) {
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
elseif($response === false) break;
|
elseif($handlerResponse === false) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 2. Если итерации закончились и задан обьект Content им создаем результат для эмиттера
|
* Отдаем пустой результат
|
||||||
*/
|
|
||||||
if($this->template() && !empty($this->template()->getResponse())){
|
|
||||||
$body = $this->globals()->response()->getBody();
|
|
||||||
$body->write($this->template()->getResponse());
|
|
||||||
$body->rewind();
|
|
||||||
return $this->globals()->response()->withBody($body);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 3. Отдаем пустой результат если не определен шаблонизатор
|
|
||||||
*/
|
*/
|
||||||
return $this->defaultPage(404);
|
return $this->defaultPage(404);
|
||||||
}
|
}
|
||||||
@@ -87,12 +78,12 @@ class App extends Main {
|
|||||||
if($this->logger()) $this->logger()->warning($appException->getMessage()." on ".$appException->getFile().":".$appException->getLine());
|
if($this->logger()) $this->logger()->warning($appException->getMessage()." on ".$appException->getFile().":".$appException->getLine());
|
||||||
$this->syslogger()->warning("AppException: ".$appException->getMessage());
|
$this->syslogger()->warning("AppException: ".$appException->getMessage());
|
||||||
}
|
}
|
||||||
catch (\Exception $exception) {
|
catch (\Exception|ContainerExceptionInterface $exception) {
|
||||||
if($this->logger()) $this->logger()->warning($exception->getMessage()." on ".$exception->getFile().":".$exception->getLine());
|
if($this->logger()) $this->logger()->warning($exception->getMessage()." on ".$exception->getFile().":".$exception->getLine());
|
||||||
$this->syslogger()->warning("Exception: ".$exception->getMessage()." : ".$exception->getFile()." : ".$exception->getLine());
|
$this->syslogger()->warning("Exception: ".$exception->getMessage()." : ".$exception->getFile()." : ".$exception->getLine());
|
||||||
}
|
}
|
||||||
catch (AppError $appError){
|
catch (AppError $appError){
|
||||||
if($this->logger()) $this->logger()->warning($appError->getMessage()." on ".$appError->getFile().":".$appError->getLine());
|
if($this->logger()) $this->logger()->error($appError->getMessage()." on ".$appError->getFile().":".$appError->getLine());
|
||||||
$this->syslogger()->error("Error: ".$appError->getMessage()." : ".$appError->getFile()." : ".$appError->getLine());
|
$this->syslogger()->error("Error: ".$appError->getMessage()." : ".$appError->getFile()." : ".$appError->getLine());
|
||||||
}
|
}
|
||||||
catch (\Error $error) {
|
catch (\Error $error) {
|
||||||
@@ -100,7 +91,7 @@ class App extends Main {
|
|||||||
$this->syslogger()->error("Error: ".$error->getMessage()." : ".$error->getFile()." : ".$error->getLine());
|
$this->syslogger()->error("Error: ".$error->getMessage()." : ".$error->getFile()." : ".$error->getLine());
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 4. Отдаем ошибку без шаблона
|
* Отдаем после ошибки
|
||||||
*/
|
*/
|
||||||
return $this->defaultPage(501);
|
return $this->defaultPage(501);
|
||||||
}
|
}
|
||||||
@@ -111,24 +102,16 @@ class App extends Main {
|
|||||||
*/
|
*/
|
||||||
private function defaultPage(int $code) : ResponseInterface{
|
private function defaultPage(int $code) : ResponseInterface{
|
||||||
if(is_file($this->baseDir.'/'.getenv("PAGE".$code))){
|
if(is_file($this->baseDir.'/'.getenv("PAGE".$code))){
|
||||||
$body = $this->globals()->response()->getBody();
|
$this->globals()->response()->getBody()->write(file_get_contents($this->baseDir.'/'.getenv("PAGE".$code)));
|
||||||
$body->write(file_get_contents($this->baseDir.'/'.getenv("PAGE".$code)));
|
|
||||||
$body->rewind();
|
|
||||||
return $this->globals()->response()->withBody($body)->withStatus($code);
|
|
||||||
}
|
}
|
||||||
return $this->globals()->response()->withStatus($code);
|
return $this->globals()->response()->withStatus($code);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ServerRequestInterface $request
|
|
||||||
* @param ResponseInterface $response
|
|
||||||
* @return void
|
* @return void
|
||||||
* @throws AppException
|
* @throws AppException
|
||||||
*/
|
*/
|
||||||
private function init(ServerRequestInterface $request, ResponseInterface $response) : void {
|
private function init() : void {
|
||||||
|
|
||||||
$this->setGlobals($request, $response);
|
|
||||||
|
|
||||||
// init factories
|
// init factories
|
||||||
if(is_file($this->baseDir."/".getenv("APP_COMPONENTS_FILE"))){
|
if(is_file($this->baseDir."/".getenv("APP_COMPONENTS_FILE"))){
|
||||||
$components = include_once $this->baseDir."/".getenv("APP_COMPONENTS_FILE");
|
$components = include_once $this->baseDir."/".getenv("APP_COMPONENTS_FILE");
|
||||||
@@ -167,7 +150,7 @@ class App extends Main {
|
|||||||
private function getActions(array $appNodes) : void {
|
private function getActions(array $appNodes) : void {
|
||||||
foreach ($appNodes as $appNode){
|
foreach ($appNodes as $appNode){
|
||||||
|
|
||||||
// по умолчанию точка монтирования ровна корню
|
// по умолчанию точка монтирования от корня
|
||||||
$mountKey = (!empty($appNode['key'])) ? $appNode['key'] : '/';
|
$mountKey = (!empty($appNode['key'])) ? $appNode['key'] : '/';
|
||||||
|
|
||||||
// если url начинается не с точки монтирования смотрим далее
|
// если url начинается не с точки монтирования смотрим далее
|
||||||
@@ -176,20 +159,24 @@ class App extends Main {
|
|||||||
if(!empty($appNode['action'])){
|
if(!empty($appNode['action'])){
|
||||||
$className = $appNode['action'];
|
$className = $appNode['action'];
|
||||||
$methodName = $appNode['method'];
|
$methodName = $appNode['method'];
|
||||||
$params = (!empty($appNode['params']) && is_string($appNode['params'])) ? explode(",",str_replace(" ", "", $appNode['params'])) : [];
|
$this->appRoutes[] = new MatchObject($className, $methodName);
|
||||||
$this->appRoutes[] = new MatchObject($className, $methodName, $params);
|
}
|
||||||
|
elseif(!empty($appNode['router']) && is_array($appNode['router'])){
|
||||||
|
if(empty($this->router)) throw AppError::invalidRequiredObject("Application config without router");
|
||||||
|
$this->router->setStartPoint($mountKey);
|
||||||
|
$this->router->withRules($appNode['router']);
|
||||||
|
|
||||||
|
$routes = $this->router->match($this->globals()->request()) ?? [];
|
||||||
|
foreach ($routes as $route){
|
||||||
|
$this->appRoutes[] = $route;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
elseif(!empty($appNode['router']) && file_exists($this->baseDir."/".$appNode['router'])){
|
elseif(!empty($appNode['router']) && file_exists($this->baseDir."/".$appNode['router'])){
|
||||||
|
|
||||||
if(empty($this->router)) throw AppError::invalidRequiredObject("Application config without router");
|
if(empty($this->router)) throw AppError::invalidRequiredObject("Application config without router");
|
||||||
$this->router->setStartPoint($mountKey);
|
$this->router->setStartPoint($mountKey);
|
||||||
|
ob_start(); $routes = include_once $this->baseDir."/".$appNode['router']; ob_end_clean();
|
||||||
if(pathinfo($this->baseDir."/".$appNode['router'])['extension'] == "php") {
|
if(is_array($routes)) $this->router->withRules($routes);
|
||||||
$this->router->withRules(include_once $this->baseDir."/".$appNode['router']);
|
|
||||||
}
|
|
||||||
elseif(pathinfo($this->baseDir."/".$appNode['router'])['extension'] == "yaml") {
|
|
||||||
$this->router->withRules(yaml_parse_file($this->baseDir."/".$appNode['router']));
|
|
||||||
}
|
|
||||||
|
|
||||||
$routes = $this->router->match($this->globals()->request()) ?? [];
|
$routes = $this->router->match($this->globals()->request()) ?? [];
|
||||||
foreach ($routes as $route){
|
foreach ($routes as $route){
|
||||||
|
|||||||
186
src/AppCli.php
Normal file
186
src/AppCli.php
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace Rmphp\Kernel;
|
||||||
|
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Rmphp\Foundation\Exceptions\AppError;
|
||||||
|
use Rmphp\Foundation\Exceptions\AppException;
|
||||||
|
use Rmphp\Foundation\RouterInterface;
|
||||||
|
use Rmphp\Foundation\MatchObject;
|
||||||
|
|
||||||
|
|
||||||
|
class AppCli extends Main {
|
||||||
|
|
||||||
|
private string $baseDir;
|
||||||
|
private array $appRoutes = [];
|
||||||
|
private RouterInterface $router;
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$this->baseDir = dirname(__DIR__, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ServerRequestInterface $request
|
||||||
|
* @param ResponseInterface $response
|
||||||
|
* @return ResponseInterface
|
||||||
|
*/
|
||||||
|
public function handler(ServerRequestInterface $request, ResponseInterface $response) : ResponseInterface {
|
||||||
|
try{
|
||||||
|
$this->setGlobals($request, $response);
|
||||||
|
$this->init();
|
||||||
|
$this->syslogger()->dump("Request", $request);
|
||||||
|
$this->syslogger()->dump("Router", $this->router);
|
||||||
|
$this->syslogger()->dump("routes", $this->appRoutes);
|
||||||
|
|
||||||
|
foreach ($this->appRoutes as $appRouteKey => $appHandler){
|
||||||
|
|
||||||
|
if(!$appHandler instanceof MatchObject) continue;
|
||||||
|
$handlerResponse = null;
|
||||||
|
|
||||||
|
if(!empty($appHandler->className)){
|
||||||
|
if(!class_exists($appHandler->className)) {
|
||||||
|
$logs[] = "Err - Class ".$appHandler->className." is not exists";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$controllers[$appRouteKey] = ($this->container() instanceof ContainerInterface) ? $this->container()->get($appHandler->className) : new $appHandler->className;
|
||||||
|
$log = "Class ".$appHandler->className;
|
||||||
|
|
||||||
|
if(!empty($appHandler->methodName)){
|
||||||
|
if(!method_exists($appHandler->className, $appHandler->methodName)) {
|
||||||
|
$logs[] = "Err - Method ".$appHandler->className."/".$appHandler->methodName." is not exists";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$handlerResponse = (!empty($appHandler->params)) ? $controllers[$appRouteKey]->{$appHandler->methodName}(...$appHandler->params) : $controllers[$appRouteKey]->{$appHandler->methodName}();
|
||||||
|
$log = "Method ".$appHandler->className."/".$appHandler->methodName;
|
||||||
|
}
|
||||||
|
$logs[] = "OK - ".$log;
|
||||||
|
|
||||||
|
if($handlerResponse instanceof ResponseInterface) {
|
||||||
|
return $handlerResponse;
|
||||||
|
}
|
||||||
|
elseif($handlerResponse === false) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!isset($handlerResponse)) {
|
||||||
|
return (isset($logs)) ? $this->defaultPage(implode(PHP_EOL, $logs)) : $this->defaultPage("Скрипт не найден");
|
||||||
|
}
|
||||||
|
return $this->globals()->response();
|
||||||
|
}
|
||||||
|
catch (AppException $appException){
|
||||||
|
if($this->logger()) $this->logger()->warning($appException->getMessage()." on ".$appException->getFile().":".$appException->getLine());
|
||||||
|
$error = "AppException: ".$appException->getMessage()." on ".$appException->getFile().":".$appException->getLine();
|
||||||
|
}
|
||||||
|
catch (\Exception|ContainerExceptionInterface $exception) {
|
||||||
|
if($this->logger()) $this->logger()->warning($exception->getMessage()." on ".$exception->getFile().":".$exception->getLine());
|
||||||
|
$error = "Exception: ".$exception->getMessage()." on ".$exception->getFile().":".$exception->getLine();
|
||||||
|
}
|
||||||
|
catch (AppError $appError){
|
||||||
|
if($this->logger()) $this->logger()->error($appError->getMessage()." on ".$appError->getFile().":".$appError->getLine());
|
||||||
|
$error = "AppError: ".$appError->getMessage()." on ".$appError->getFile().":".$appError->getLine();
|
||||||
|
}
|
||||||
|
catch (\Error $error) {
|
||||||
|
if($this->logger()) $this->logger()->error($error->getMessage()." on ".$error->getFile().":".$error->getLine());
|
||||||
|
$error = "Error: ".$error->getMessage()." on ".$error->getFile().":".$error->getLine();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Отдаем после ошибки
|
||||||
|
*/
|
||||||
|
return $this->defaultPage('Ошибка при выполнении. '.$error ?? "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $error
|
||||||
|
* @return ResponseInterface
|
||||||
|
*/
|
||||||
|
private function defaultPage(string $error) : ResponseInterface{
|
||||||
|
$this->globals()->response()->getBody()->write($error);
|
||||||
|
return $this->globals()->response();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws AppException
|
||||||
|
*/
|
||||||
|
private function init() : void {
|
||||||
|
// init factories
|
||||||
|
if(is_file($this->baseDir."/".getenv("APP_COMPONENTS_FILE"))){
|
||||||
|
$components = include_once $this->baseDir."/".getenv("APP_COMPONENTS_FILE");
|
||||||
|
if(!empty($components) && is_array($components)){
|
||||||
|
foreach ($components as $componentName => $componentValue){
|
||||||
|
if(empty($componentValue)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
elseif(is_object($componentValue)){
|
||||||
|
$componentObject = $componentValue;
|
||||||
|
}
|
||||||
|
elseif(!file_exists($this->baseDir.'/'.$componentValue) || !is_object($componentObject = require $this->baseDir.'/'.$componentValue)){
|
||||||
|
throw AppException::invalidObject($componentValue);
|
||||||
|
}
|
||||||
|
switch (true){
|
||||||
|
case ($componentObject instanceof ContainerInterface): $this->setContainer($componentObject); break;
|
||||||
|
case ($componentObject instanceof LoggerInterface): $this->setLogger($componentObject); break;
|
||||||
|
case ($componentObject instanceof RouterInterface): $this->router = $componentObject; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// app nodes
|
||||||
|
if(is_file($this->baseDir."/".getenv("CLI_NODES_FILE"))){
|
||||||
|
$nodes = include_once $this->baseDir."/".getenv("CLI_NODES_FILE");
|
||||||
|
}
|
||||||
|
elseif(is_file($this->baseDir."/".getenv("APP_NODES_FILE"))){
|
||||||
|
$nodes = include_once $this->baseDir."/".getenv("APP_NODES_FILE");
|
||||||
|
}
|
||||||
|
if(empty($nodes) || !is_array($nodes)) throw AppException::emptyAppNodes();
|
||||||
|
$this->getActions($nodes);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $appNodes
|
||||||
|
*/
|
||||||
|
private function getActions(array $appNodes) : void {
|
||||||
|
|
||||||
|
foreach ($appNodes as $appNode){
|
||||||
|
|
||||||
|
// по умолчанию точка монтирования от корня
|
||||||
|
$mountKey = (!empty($appNode['key'])) ? $appNode['key'] : "";
|
||||||
|
|
||||||
|
// если url начинается не с точки монтирования смотрим далее
|
||||||
|
if (0 !== (strpos($this->globals()->request()->getServerParams()['argv'][1], $mountKey))) continue;
|
||||||
|
|
||||||
|
if(!empty($appNode['action'])){
|
||||||
|
$className = $appNode['action'];
|
||||||
|
$methodName = $appNode['method'];
|
||||||
|
$this->appRoutes[] = new MatchObject($className, $methodName);
|
||||||
|
}
|
||||||
|
elseif(!empty($appNode['router']) && is_array($appNode['router'])){
|
||||||
|
if(empty($this->router)) throw AppError::invalidRequiredObject("Application config without router");
|
||||||
|
$this->router->setStartPoint($mountKey);
|
||||||
|
$this->router->withSet($appNode['router']);
|
||||||
|
|
||||||
|
$routes = $this->router->matchByArgv($this->globals()->request()) ?? [];
|
||||||
|
foreach ($routes as $route){
|
||||||
|
$this->appRoutes[] = $route;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif(!empty($appNode['router']) && file_exists($this->baseDir."/".$appNode['router'])){
|
||||||
|
if(empty($this->router)) throw AppError::invalidRequiredObject("Application config without router");
|
||||||
|
$this->router->setStartPoint($mountKey);
|
||||||
|
ob_start(); $routes = include_once $this->baseDir."/".$appNode['router']; ob_end_clean();
|
||||||
|
if(is_array($routes)) $this->router->withSet($routes);
|
||||||
|
|
||||||
|
$routes = $this->router->matchByArgv($this->globals()->request()) ?? [];
|
||||||
|
foreach ($routes as $route){
|
||||||
|
$this->appRoutes[] = $route;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,12 +4,12 @@ namespace Rmphp\Kernel;
|
|||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
|
|
||||||
class Globals {
|
class Globals {
|
||||||
|
|
||||||
private ServerRequestInterface $request;
|
private ServerRequestInterface $request;
|
||||||
private ResponseInterface $response;
|
private ResponseInterface $response;
|
||||||
private Session $session;
|
|
||||||
|
|
||||||
const INT = "INT";
|
const INT = "INT";
|
||||||
const STRING = "STRING";
|
const STRING = "STRING";
|
||||||
@@ -83,16 +83,6 @@ class Globals {
|
|||||||
return (!empty($name)) ? isset($this->request->getCookieParams()[$name]) : !empty($this->request->getCookieParams());
|
return (!empty($name)) ? isset($this->request->getCookieParams()[$name]) : !empty($this->request->getCookieParams());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function isSession(string $name = "") : bool {
|
|
||||||
if(!class_exists(Session::class)) return false;
|
|
||||||
if(!isset($this->session)) $this->session = new Session();
|
|
||||||
return (!empty($name)) ? isset($this->session->getSession()[$name]) : !empty($this->session->getSession());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return bool
|
* @return bool
|
||||||
@@ -139,21 +129,17 @@ class Globals {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $type
|
* @return array|UploadedFileInterface|null
|
||||||
* @return array|int|string
|
|
||||||
*/
|
*/
|
||||||
public function session(string $name = "", string $type = "") {
|
public function files(string $name = ""): array|UploadedFileInterface|null {
|
||||||
if(!class_exists(Session::class)) return null;
|
$name = strtolower($name);
|
||||||
if(!isset($this->session)) $this->session = new Session();
|
$var = $this->request->getUploadedFiles();
|
||||||
return $this->onGlobal($this->session->getSession(), $name, $type);
|
if (!empty($name))
|
||||||
}
|
{
|
||||||
|
if (!isset($var[$name])) return null;
|
||||||
/**
|
return $var[$name];
|
||||||
* @param string $name
|
}
|
||||||
* @return array|int|string
|
return $var;
|
||||||
*/
|
|
||||||
public function files(string $name = "") {
|
|
||||||
return $this->onGlobal($this->request->getUploadedFiles(), $name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -174,18 +160,6 @@ class Globals {
|
|||||||
$this->setResponse($this->response->withAddedHeader($name, $value));
|
$this->setResponse($this->response->withAddedHeader($name, $value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param $value
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setSession(string $name, $value = null) : void {
|
|
||||||
if(class_exists(Session::class)) {
|
|
||||||
if(!isset($this->session)) $this->session = new Session();
|
|
||||||
$this->session->setSession($name, $value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $value
|
* @param string $value
|
||||||
@@ -209,17 +183,6 @@ class Globals {
|
|||||||
$this->addHeader("Set-Cookie", implode("; ", $cookie));
|
$this->addHeader("Set-Cookie", implode("; ", $cookie));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string|null $name
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function clearSession(string $name = null) : void{
|
|
||||||
if(class_exists(Session::class)) {
|
|
||||||
if(!isset($this->session)) $this->session = new Session();
|
|
||||||
$this->session->clearSession($name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $path
|
* @param string $path
|
||||||
@@ -232,14 +195,13 @@ class Globals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $var
|
* @param array $var
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @return array|int|string
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private function onGlobal(array $var, string $name, string $type = "") {
|
private function onGlobal(array $var, string $name, string $type = ""): mixed {
|
||||||
$name = strtolower($name);
|
$name = strtolower($name);
|
||||||
if (!empty($name))
|
if (!empty($name))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Kernel;
|
|
||||||
|
|
||||||
|
|
||||||
class Session {
|
|
||||||
|
|
||||||
public function __construct(string $name = "usi") {
|
|
||||||
if(session_status() == PHP_SESSION_NONE) {
|
|
||||||
session_name($name);
|
|
||||||
session_start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getSession() : array {
|
|
||||||
return $_SESSION;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param $value
|
|
||||||
*/
|
|
||||||
public function setSession(string $name, $value = null) : void {
|
|
||||||
$_SESSION[$name] = $value;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string|null $name
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function clearSession(string $name = null) : void {
|
|
||||||
if (isset($name)) unset($_SESSION[$name]);
|
|
||||||
else $_SESSION = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user