@@ -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 ;
@@ -34,6 +35,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 -> init ( $request , $response );
$this -> syslogger () -> dump ( " Request " , $request );
$this -> syslogger () -> dump ( " Router " , $this -> router );
$this -> syslogger () -> dump ( " routes " , $this -> appRoutes );
$this -> syslogger () -> dump ( " routes " , $this -> appRoutes );
foreach ( $this -> appRoutes as $appRouteKey => $appHandler ){
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 " );
$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 = " OK - Class " . $appHandler -> className ;
$log = " Class " . $appHandler -> className ;
if ( ! empty ( $appHandler -> methodName )){
if ( ! empty ( $appHandler -> methodName )){
if ( ! method_exists ( $appHandler -> className , $appHandler -> methodName )) {
if ( ! method_exists ( $appHandler -> className , $appHandler -> methodName )) {
@@ -55,12 +58,10 @@ class App extends Main {
continue ;
continue ;
}
}
$response = ( ! empty ( $appHandler -> params )) ? $controllers [ $appRouteKey ] -> { $appHandler -> methodName }( ... $appHandler -> params ) : $controllers [ $appRouteKey ] -> { $appHandler -> methodName }();
$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 - досрочно отдаем результат в эмиттер
*/
if ( $response instanceof ResponseInterface ) {
if ( $response instanceof ResponseInterface ) {
return $response ;
return $response ;
}
}
@@ -68,16 +69,7 @@ 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 );
}
/**
* 3. Отдаем пустой результат если не определен шаблонизатор
*/
*/
return $this -> defaultPage ( 404 );
return $this -> defaultPage ( 404 );
}
}
@@ -85,12 +77,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 ) {
@@ -98,7 +90,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 );
}
}
@@ -109,10 +101,7 @@ 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 );
}
}
@@ -155,11 +144,6 @@ class App extends Main {
if ( is_file ( $this -> baseDir . " / " . getenv ( " APP_NODES_FILE " ))){
if ( is_file ( $this -> baseDir . " / " . getenv ( " APP_NODES_FILE " ))){
$nodes = include_once $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 ();
if ( empty ( $nodes ) || ! is_array ( $nodes )) throw AppException :: emptyAppNodes ();
$this -> getActions ( $nodes );
$this -> getActions ( $nodes );
}
}
@@ -170,7 +154,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 начинается не с точки монтирования смотрим далее
@@ -182,20 +166,22 @@ class App extends Main {
$params = ( ! empty ( $appNode [ 'params' ]) && is_string ( $appNode [ 'params' ])) ? explode ( " , " , str_replace ( " " , " " , $appNode [ 'params' ])) : [];
$params = ( ! empty ( $appNode [ 'params' ]) && is_string ( $appNode [ 'params' ])) ? explode ( " , " , str_replace ( " " , " " , $appNode [ 'params' ])) : [];
$this -> appRoutes [] = new MatchObject ( $className , $methodName , $params );
$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' ] == " 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' ]));
}
$routes = $this -> router -> match ( $this -> globals () -> request ()) ? ? [];
$routes = $this -> router -> match ( $this -> globals () -> request ()) ? ? [];
foreach ( $routes as $route ){
foreach ( $routes as $route ){