diff --git a/.env.dist b/.env.dist index 6abe87b..aa29ca1 100644 --- a/.env.dist +++ b/.env.dist @@ -11,6 +11,7 @@ # PROD / DEV APP_MODE=DEV APP_NODES_FILE=application/config/app.php +APPCLI_NODES_FILE=application/config/app-cli.php APP_COMPONENTS_FILE=application/config/components.php CONTAINER_INI=application/config/container.php CONTAINER_CACHE=var/cache/container diff --git a/README.md b/README.md index 32dcf51..adb55b5 100644 --- a/README.md +++ b/README.md @@ -9,5 +9,5 @@ composer create-project rmphp/skeleton project-name ``` ```bash -composer create-project rmphp/skeleton:"^4.6" project-name +composer create-project rmphp/skeleton:"^4.7" project-name ``` diff --git a/application/base/Controllers/AbstractHandler.php b/application/base/Controllers/AbstractHandler.php new file mode 100644 index 0000000..de22a58 --- /dev/null +++ b/application/base/Controllers/AbstractHandler.php @@ -0,0 +1,72 @@ +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); + } + +} diff --git a/application/bin/cli b/application/bin/cli index ff6d2f0..4e9e09c 100644 --- a/application/bin/cli +++ b/application/bin/cli @@ -1,3 +1,3 @@ #!/usr/bin/php usePutenv()->loadEnv(dirname(__DIR__,2).'/.env'); - -/** @var LoggerInterface $logger */ -$logger = require_once dirname(__DIR__,2).'/application/config/components/loggerFactory.php'; -/** @var Container $container */ -$container = require_once dirname(__DIR__,2).'/application/config/components/containerFactory.php'; -$container->set(LoggerInterface::class, $logger); - -if(str_contains($argv[1], ':')) { - list($className, $method) = explode(':', $argv[1]); - if(class_exists($className)) { - try { - $controllers = $container->get($className); - if(method_exists($controllers, $method)) { - $response = $controllers->$method(); - } - if(isset($response)) { - if($response instanceof ResponseInterface) (new ResponseEmitter())->emit($response); - elseif(!is_bool($response)) echo $response; - } - } catch(Throwable $throwable){ - $logger->error($throwable->getMessage()." on ".$throwable->getFile().":".$throwable->getLine()); - } - } - else echo "Class $className does not exist"; -} diff --git a/application/bin/console b/application/bin/console new file mode 100644 index 0000000..4e9e09c --- /dev/null +++ b/application/bin/console @@ -0,0 +1,3 @@ +#!/usr/bin/php +usePutenv()->loadEnv(dirname(__DIR__,2).'/.env'); + +$app = new AppCli(); +$response = $app->handler(ServerRequestFactory::fromGlobals(), new Response()); +(new ResponseEmitter())->emit($response); diff --git a/application/config/app-cli.php b/application/config/app-cli.php new file mode 100644 index 0000000..52483da --- /dev/null +++ b/application/config/app-cli.php @@ -0,0 +1,19 @@ +'/', "action"=>"App\\Main\\Controllers\\IndexController", "method"=>"index"], +# ['key'=>'', 'router'=>'application/config/routes-cli/routes.php'], +# ['key'=>'/', 'router'=>[]], + +return [ + ['key'=>'', 'router'=>[ + [ + 'key'=>'index', + 'routes' => [ + [ + 'action' => "App\Infrastructure\Handlers\IndexHandler", + 'method' => "index" + ] + ] + ] + ]], +]; diff --git a/composer.json b/composer.json index 743b379..780b373 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,9 @@ "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", + "rmphp/content": "^4.0", + "rmphp/kernel": "^6.0", + "rmphp/router": "^2.0", "rmphp/session": "^1.1", "rmphp/redis": "^1.0", "rmphp/storage": "^6.0", diff --git a/src/Infrastructure/Handlers/IndexHandler.php b/src/Infrastructure/Handlers/IndexHandler.php new file mode 100644 index 0000000..3db3211 --- /dev/null +++ b/src/Infrastructure/Handlers/IndexHandler.php @@ -0,0 +1,22 @@ +textResponse((new \DateTime())->format('Y-m-d H:i:s')); + + } + +}