Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a620798b3 | ||
|
|
7658fc88e6 | ||
|
|
5c79d63d6c | ||
|
|
d43d4751a7 | ||
|
|
c681f88de4 | ||
|
|
d5a9d0fc10 | ||
|
|
407529f092 | ||
|
|
1f902c6b70 |
@@ -9,13 +9,7 @@ Stable version
|
||||
```bash
|
||||
composer require rmphp/kernel
|
||||
```
|
||||
```bash
|
||||
composer require rmphp/kernel:"^1.0"
|
||||
```
|
||||
|
||||
|
||||
Dev version contains the latest changes
|
||||
|
||||
```bash
|
||||
composer require rmphp/kernel:"1.0.x-dev"
|
||||
composer require rmphp/kernel:"^2.0"
|
||||
```
|
||||
|
||||
34
src/App.php
34
src/App.php
@@ -4,6 +4,7 @@ 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;
|
||||
@@ -34,6 +35,8 @@ class App extends Main {
|
||||
public function handler(ServerRequestInterface $request, ResponseInterface $response) : ResponseInterface {
|
||||
try{
|
||||
$this->init($request, $response);
|
||||
$this->syslogger()->dump("Request", $request);
|
||||
$this->syslogger()->dump("Router", $this->router);
|
||||
$this->syslogger()->dump("routes", $this->appRoutes);
|
||||
|
||||
foreach ($this->appRoutes as $appRouteKey => $appHandler){
|
||||
@@ -46,8 +49,8 @@ class App extends Main {
|
||||
$this->syslogger()->log("handlers", "Err - Class ".$appHandler->className." is not exists");
|
||||
continue;
|
||||
}
|
||||
$controllers[$appRouteKey] = new $appHandler->className;
|
||||
$log = "OK - Class ".$appHandler->className;
|
||||
$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)) {
|
||||
@@ -55,9 +58,9 @@ class App extends Main {
|
||||
continue;
|
||||
}
|
||||
$response = (!empty($appHandler->params)) ? $controllers[$appRouteKey]->{$appHandler->methodName}(...$appHandler->params) : $controllers[$appRouteKey]->{$appHandler->methodName}();
|
||||
$log = "OK - Method ".$appHandler->className."/".$appHandler->methodName;
|
||||
$log = "Method ".$appHandler->className."/".$appHandler->methodName;
|
||||
}
|
||||
$this->syslogger()->log("handlers", $log);
|
||||
$this->syslogger()->log("handlers", "OK - ".$log);
|
||||
/**
|
||||
* 1. Если на этапе итерации уже получен ответ ResponseInterface - досрочно отдаем результат в эмиттер
|
||||
*/
|
||||
@@ -71,10 +74,8 @@ class App extends Main {
|
||||
* 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);
|
||||
$this->globals()->response()->getBody()->write($this->template()->getResponse());
|
||||
return$this->globals()->response();
|
||||
}
|
||||
/**
|
||||
* 3. Отдаем пустой результат если не определен шаблонизатор
|
||||
@@ -85,7 +86,7 @@ class App extends Main {
|
||||
if($this->logger()) $this->logger()->warning($appException->getMessage()." on ".$appException->getFile().":".$appException->getLine());
|
||||
$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());
|
||||
$this->syslogger()->warning("Exception: ".$exception->getMessage()." : ".$exception->getFile()." : ".$exception->getLine());
|
||||
}
|
||||
@@ -109,10 +110,7 @@ class App extends Main {
|
||||
*/
|
||||
private function defaultPage(int $code) : ResponseInterface{
|
||||
if(is_file($this->baseDir.'/'.getenv("PAGE".$code))){
|
||||
$body = $this->globals()->response()->getBody();
|
||||
$body->write(file_get_contents($this->baseDir.'/'.getenv("PAGE".$code)));
|
||||
$body->rewind();
|
||||
return $this->globals()->response()->withBody($body)->withStatus($code);
|
||||
$this->globals()->response()->getBody()->write(file_get_contents($this->baseDir.'/'.getenv("PAGE".$code)));
|
||||
}
|
||||
return $this->globals()->response()->withStatus($code);
|
||||
}
|
||||
@@ -155,11 +153,6 @@ class App extends Main {
|
||||
if(is_file($this->baseDir."/".getenv("APP_NODES_FILE"))){
|
||||
$nodes = include_once $this->baseDir."/".getenv("APP_NODES_FILE");
|
||||
}
|
||||
for ($appNodeNum = 1; getenv("APP_NODE".$appNodeNum); $appNodeNum++){
|
||||
$nodesCollection[] = json_decode(getenv("APP_NODE".$appNodeNum), true);
|
||||
}
|
||||
if(isset($nodesCollection)) $nodes = $nodesCollection;
|
||||
|
||||
if(empty($nodes) || !is_array($nodes)) throw AppException::emptyAppNodes();
|
||||
$this->getActions($nodes);
|
||||
}
|
||||
@@ -170,7 +163,7 @@ class App extends Main {
|
||||
private function getActions(array $appNodes) : void {
|
||||
foreach ($appNodes as $appNode){
|
||||
|
||||
// по умолчанию точка монтирования ровна корню
|
||||
// по умолчанию точка монтирования от корня
|
||||
$mountKey = (!empty($appNode['key'])) ? $appNode['key'] : '/';
|
||||
|
||||
// если url начинается не с точки монтирования смотрим далее
|
||||
@@ -190,9 +183,6 @@ class App extends Main {
|
||||
if(pathinfo($this->baseDir."/".$appNode['router'])['extension'] == "php") {
|
||||
$this->router->withRules(include_once $this->baseDir."/".$appNode['router']);
|
||||
}
|
||||
elseif(pathinfo($this->baseDir."/".$appNode['router'])['extension'] == "json") {
|
||||
$this->router->withRules(json_decode(file_get_contents($this->baseDir."/".$appNode['router']), true));
|
||||
}
|
||||
elseif(pathinfo($this->baseDir."/".$appNode['router'])['extension'] == "yaml") {
|
||||
$this->router->withRules(yaml_parse_file($this->baseDir."/".$appNode['router']));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user