diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e8e9e0c..24189853 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ Release for 5.0.2 - Fix action dependency injector - Add the base error handler -## [Unreleased] +## 5.0.0 - 2023-05-10 - [Add] Convert the project from PHP7 to PHP8 - [Add] Multiconnection for storage system diff --git a/src/Application/Application.php b/src/Application/Application.php index 05987e3c..4e56b378 100644 --- a/src/Application/Application.php +++ b/src/Application/Application.php @@ -195,30 +195,17 @@ public function send(): ?bool // the routing collection $routes = $this->getRoutes(); - if (!isset($routes[$method])) { - // We verify and call function associate by 404 code - $this->response->status(404); - - if (empty($this->error_code)) { - $this->response->send( - sprintf('Cannot %s %s 404', $method, $this->request->path()) - ); - } - - return false; - } - $response = null; $resolved = false; - foreach ($routes[$method] as $route) { + foreach ($routes[$method] ?? [] as $route) { // The route must be an instance of Route if (!($route instanceof Route)) { continue; } // We launch the search of the method that arrived in the query - // then start checking the url of the request + // then start checking the url of the request if (!$route->match($this->request->path())) { continue; } diff --git a/src/Application/Exception/BaseErrorHandler.php b/src/Application/Exception/BaseErrorHandler.php index e2b0031c..4c1aae42 100644 --- a/src/Application/Exception/BaseErrorHandler.php +++ b/src/Application/Exception/BaseErrorHandler.php @@ -6,16 +6,16 @@ use Bow\View\View; -class BaseErrorHandler extends \Exception +class BaseErrorHandler { /** * Render view as response * * @param string $view * @param array $data - * @return mixed + * @return string */ - protected function render($view, $data = []) + protected function render($view, $data = []): string { return View::parse($view, $data)->getContent(); } diff --git a/src/Configuration/LoggerConfiguration.php b/src/Configuration/LoggerConfiguration.php index b4590c7c..912fa13d 100644 --- a/src/Configuration/LoggerConfiguration.php +++ b/src/Configuration/LoggerConfiguration.php @@ -6,14 +6,16 @@ use Bow\View\View; use Monolog\Logger; -use Bow\Http\Redirect; use Bow\Support\Collection; use Bow\Configuration\Loader; use Bow\Database\Barry\Model; use Monolog\Handler\StreamHandler; use Monolog\Handler\FirePHPHandler; +use Whoops\Handler\CallbackHandler; use Bow\Configuration\Configuration; use Bow\Contracts\ResponseInterface; +use Whoops\Handler\PrettyPageHandler; +use Whoops\Handler\Handler; class LoggerConfiguration extends Configuration { @@ -54,44 +56,38 @@ private function loadFrontLogger(Logger $monolog, $error_handler) { $whoops = new \Whoops\Run(); - if (app_env('APP_ENV') == 'development') { - $whoops->pushHandler( - new \Whoops\Handler\PrettyPageHandler() - ); + if (app_env('APP_ENV') != 'production') { + $whoops->pushHandler(new PrettyPageHandler()); + $whoops->register(); + return; } - if (class_exists($error_handler)) { - $handler = new \Whoops\Handler\CallbackHandler( - function ($exception, $inspector, $run) use ($monolog, $error_handler) { - $monolog->error($exception->getMessage(), $exception->getTrace()); - - $result = call_user_func_array( - [new $error_handler(), 'handle'], - [$exception] - ); - - switch (true) { - case $result instanceof View: - return $result->getContent(); - case $result instanceof ResponseInterface || $result instanceof Redirect: - $result->sendContent(); - break; - case $result instanceof Model || $result instanceof Collection: - return $result->toArray(); - case is_null($result): - case is_string($result): - case is_array($result): - case is_object($result): - case $result instanceof \Iterable: - return $result; - } - exit(1); + $handler = new CallbackHandler( + function ($exception, $inspector, $run) use ($monolog, $error_handler) { + $monolog->error($exception->getMessage(), $exception->getTrace()); + + $result = call_user_func_array([new $error_handler(), 'handle'], [$exception]); + + if ($result instanceof View) { + echo $result->getContent(); + } elseif ($result instanceof ResponseInterface) { + $result->sendContent(); + } elseif ( + is_null($result) + || $result instanceof Model || $result instanceof Collection + || is_string($result) + || is_array($result) + || is_object($result) + || $result instanceof \Iterable + ) { + echo json_encode($result); } - ); - $whoops->pushHandler($handler); - } + return Handler::QUIT; + } + ); + $whoops->pushHandler($handler); $whoops->register(); } diff --git a/src/Database/Barry/Relations/BelongsTo.php b/src/Database/Barry/Relations/BelongsTo.php index 68f92bfe..9911bd6a 100644 --- a/src/Database/Barry/Relations/BelongsTo.php +++ b/src/Database/Barry/Relations/BelongsTo.php @@ -55,7 +55,7 @@ public function getResults(): ?Model $cache = Cache::cache('file')->get($key); if (!is_null($cache)) { - $related = new $this->related; + $related = new $this->related(); $related->setAttributes($cache); return $related; } diff --git a/src/Database/Barry/Relations/HasOne.php b/src/Database/Barry/Relations/HasOne.php index ec3f452f..5eb0c985 100644 --- a/src/Database/Barry/Relations/HasOne.php +++ b/src/Database/Barry/Relations/HasOne.php @@ -52,7 +52,7 @@ public function getResults(): ?Model $cache = Cache::cache('file')->get($key); if (!is_null($cache)) { - $related = new $this->related; + $related = new $this->related(); $related->setAttributes($cache); return $related; } diff --git a/src/Http/Client/HttpClient.php b/src/Http/Client/HttpClient.php index 5c74ab8a..0f52f42c 100644 --- a/src/Http/Client/HttpClient.php +++ b/src/Http/Client/HttpClient.php @@ -57,6 +57,8 @@ public function get(string $url, array $data = []): Parser $this->addFields($data); + curl_setopt($this->ch, CURLOPT_HTTPGET, true); + return new Parser($this->ch); } @@ -81,6 +83,7 @@ public function post(string $url, array $data = []): Parser $data = array_merge($this->attach, $data); } + curl_setopt($this->ch, CURLOPT_POST, true); $this->addFields($data); return new Parser($this->ch); @@ -101,6 +104,8 @@ public function put(string $url, array $data = []): Parser $this->addFields($data); } + curl_setopt($this->ch, CURLOPT_PUT, true); + return new Parser($this->ch); } @@ -145,6 +150,7 @@ public function addHeaders(array $headers): HttpClient private function initCurl(string $url): void { $url = $this->base_url . "/" . trim($url, "/"); + $this->ch = curl_init($url); } @@ -156,6 +162,8 @@ private function initCurl(string $url): void */ private function addFields(array $data): void { - curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data)); + if (count($data) > 0) { + curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data)); + } } } diff --git a/src/Http/Client/Parser.php b/src/Http/Client/Parser.php index 73a4d28a..ec7d72f8 100644 --- a/src/Http/Client/Parser.php +++ b/src/Http/Client/Parser.php @@ -153,7 +153,7 @@ private function returnTransfert() private function returnTransfertToRaw() { if ($this->returnTransfert()) { - if (!curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, true)) { + if (!curl_setopt($this->ch, CURLOPT_HTTPGET, true)) { $this->close(); return false; diff --git a/src/Testing/TestCase.php b/src/Testing/TestCase.php index d4d8c415..074d6351 100644 --- a/src/Testing/TestCase.php +++ b/src/Testing/TestCase.php @@ -31,20 +31,17 @@ class TestCase extends PHPUnitTestCase private array $headers = []; /** - * Format url + * Get the base url * - * @param $url * @return string */ - private function formatUrl(string $url): string + private function getBaseUrl(): string { - if (!$this->url) { - $this->url = app_env('APP_URL', 'http://127.0.0.1:5000'); + if (is_null($this->url)) { + return rtrim(app_env('APP_URL', 'http://127.0.0.1:5000')); } - $url = rtrim($this->url, '/') . $url; - - return trim($url, '/'); + return $this->url ?? 'http://127.0.0.1:5000'; } /** @@ -82,7 +79,7 @@ public function withHeader(array $headers): TestCase */ public function get(string $url, array $param = []): Response { - $http = new HttpClient($this->formatUrl($url)); + $http = new HttpClient($this->getBaseUrl()); $http->addHeaders($this->headers); @@ -98,7 +95,7 @@ public function get(string $url, array $param = []): Response */ public function post(string $url, array $param = []): Response { - $http = new HttpClient($this->formatUrl($url)); + $http = new HttpClient($this->getBaseUrl()); if (!empty($this->attach)) { $http->addAttach($this->attach); @@ -118,7 +115,7 @@ public function post(string $url, array $param = []): Response */ public function put(string $url, array $param = []): Response { - $http = new HttpClient($this->formatUrl($url)); + $http = new HttpClient($this->getBaseUrl()); $http->addHeaders($this->headers); diff --git a/tests/Application/ApplicationTest.php b/tests/Application/ApplicationTest.php index d1605e22..7d6a2e3d 100644 --- a/tests/Application/ApplicationTest.php +++ b/tests/Application/ApplicationTest.php @@ -57,13 +57,14 @@ public function test_one_time_application_boot() public function test_send_application_with_404_status() { + $this->expectException(RouterException::class); + $response = Mockery::mock(Response::class); $request = Mockery::mock(Request::class); // Response mock method $response->allows()->addHeader('X-Powered-By', 'Bow Framework'); $response->allows()->status(404); - $response->allows()->send('Cannot GET / 404'); // Request mock method $request->allows()->method()->andReturns("GET"); @@ -80,8 +81,7 @@ public function test_send_application_with_404_status() $app = new Application($request, $response); $app->bind($config); - - $this->assertFalse($app->send()); + $app->send(); } public function test_send_application_with_matched_route()