Skip to content

Commit

Permalink
Merge pull request #227 from bowphp/5.x-refactoring-for-perf
Browse files Browse the repository at this point in the history
Fixes http client and TestCase service
  • Loading branch information
papac authored May 16, 2023
2 parents 15e87d6 + 33e8240 commit f8c12e4
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 71 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 2 additions & 15 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Application/Exception/BaseErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
64 changes: 30 additions & 34 deletions src/Configuration/LoggerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Database/Barry/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Barry/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Http/Client/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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));
}
}
}
2 changes: 1 addition & 1 deletion src/Http/Client/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 8 additions & 11 deletions src/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions tests/Application/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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()
Expand Down

0 comments on commit f8c12e4

Please sign in to comment.