Skip to content

Commit

Permalink
Merge pull request #245 from bowphp/5.x-refactoring-for-perf
Browse files Browse the repository at this point in the history
[5.x] Many bugs fixes
  • Loading branch information
papac authored May 27, 2023
2 parents 7589576 + 1791494 commit a2ef07f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 31 deletions.
49 changes: 49 additions & 0 deletions src/Application/Exception/BaseErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Bow\Application\Exception;

use PDOException;
use Bow\View\View;
use Bow\Http\Exception\HttpException;
use Bow\Validation\Exception\ValidationException;

class BaseErrorHandler
{
Expand All @@ -19,4 +22,50 @@ protected function render($view, $data = []): string
{
return View::parse($view, $data)->getContent();
}

/**
* Send the json as response
*
* @param string $data
* @param mixed $code
* @return mixed
*/
protected function json($exception, $code = null)
{
if (is_null($code)) {
if (method_exists($exception, 'getStatus')) {
$code = $exception->getStatus();
} else {
$code = 'INTERNAL_SERVER_ERROR';
}
}

if (app_env("APP_ENV") == "production" && $exception instanceof PDOException) {
$message = 'An SQL error occurs. For security, we did not display the message.';
} else {
$message = $exception->getMessage();
}

$response = [
'message' => $message,
'code' => $code,
'time' => date('Y-m-d H:i:s')
];

$status = 500;

if ($exception instanceof HttpException) {
$status = $exception->getStatusCode();
$response = array_merge($response, compact('status'));
if ($exception instanceof ValidationException) {
$response["errors"] = $exception->getErrors();
}
}

if (app_env("APP_ENV") != "production") {
$response["trace"] = $exception->getTrace();
}

return response()->json($response, $status);
}
}
26 changes: 17 additions & 9 deletions src/Cache/Adapter/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public function __construct(array $config)
/**
* @inheritDoc
*/
public function add(string $key, mixed $data, ?int $time = null): bool
public function add(string $key, mixed $data, ?int $time = 60): bool
{
if (is_callable($data)) {
$content = $data();
} else {
$content = $data;
}

$meta['__bow_meta'] = ['expire_at' => $time == null ? '+' : $time];
$meta['__bow_meta'] = ['expire_at' => $time == null ? '+' : time() + $time];

$meta['content'] = $content;

Expand Down Expand Up @@ -142,6 +142,14 @@ public function get(string $key, mixed $default = null): mixed

$cache = unserialize(file_get_contents($this->makeHashFilename($key)));

$expire_at = $cache['__bow_meta']['expire_at'];

if ($expire_at != '+') {
if (time() > $expire_at) {
return null;
}
}

if (!$this->with_meta) {
unset($cache['__bow_meta']);

Expand All @@ -167,11 +175,11 @@ public function addTime(string $key, int $time): bool
}

if ($cache['__bow_meta']['expire_at'] == '+') {
$cache['__bow_meta']['expire_at'] = time();
$cache['__bow_meta']['expire_at'] = time() + $time;
} else {
$cache['__bow_meta']['expire_at'] += $time;
}

$cache['__bow_meta']['expire_at'] += $time;

return (bool) file_put_contents(
$this->makeHashFilename($key),
serialize($cache)
Expand All @@ -193,7 +201,7 @@ public function timeOf(string $key): int|bool|string
return false;
}

return $cache['__bow_meta']['expire_at'];
return (int) $cache['__bow_meta']['expire_at'];
}

/**
Expand Down Expand Up @@ -243,11 +251,11 @@ public function expired(string $key): bool
return false;
}

$expire_at = $cache['__bow_meta']['expire_at'];

$this->with_meta = false;

return $cache['__bow_meta']['expire_at'] == '+'
? false
: (time() > $cache['__bow_meta']['expire_at']);
return $expire_at == '+' ? false : (time() > $expire_at);
}

/**
Expand Down
21 changes: 18 additions & 3 deletions src/Http/Client/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,13 @@ public function delete(string $url, array $data = []): Response
* Attach new file
*
* @param string $attach
* @return array
* @return HttpClient
*/
public function addAttach(string|array $attach): array
public function addAttach(string|array $attach): HttpClient
{
return $this->attach = (array) $attach;
$this->attach = (array) $attach;

return $this;
}

/**
Expand All @@ -185,6 +187,19 @@ public function addHeaders(array $headers): HttpClient
return $this;
}

/**
* Set the user agent
*
* @param string $user_agent
* @return HttpClient
*/
public function setUserAgent(string $user_agent): HttpClient
{
curl_setopt($this->ch, CURLOPT_USERAGENT, $user_agent);

return $this;
}

/**
* Reset alway connection
*
Expand Down
20 changes: 10 additions & 10 deletions src/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class Response
*
* @var string
*/
private ?string $error = null;
private ?string $error_message = null;

/**
* The error number
*
* @var int
*/
private int $errno;
private int $errer_number;

/**
* The headers
Expand All @@ -44,8 +44,8 @@ class Response
*/
public function __construct(CurlHandle &$ch, ?string $content = null)
{
$this->error = curl_error($ch);
$this->errno = curl_errno($ch);
$this->error_message = curl_error($ch);
$this->errer_number = curl_errno($ch);
$this->headers = curl_getinfo($ch);
$this->content = $content;
}
Expand All @@ -63,9 +63,9 @@ public function getContent(): ?string
/**
* Get response content as json
*
* @return bool|string
* @return object|array
*/
public function toJson(): bool|string
public function toJson(): object|array
{
$content = $this->getContent();

Expand All @@ -75,9 +75,9 @@ public function toJson(): bool|string
/**
* Get response content as json
*
* @return bool|string
* @return array
*/
public function toArray(): bool|string
public function toArray(): array
{
$content = $this->getContent();

Expand Down Expand Up @@ -181,7 +181,7 @@ public function getDownloadSpeed(): ?float
*/
public function getErrorMessage(): string
{
return $this->error;
return $this->error_message ?? curl_strerror($this->errer_number);
}

/**
Expand All @@ -191,7 +191,7 @@ public function getErrorMessage(): string
*/
public function getErrorNumber(): int
{
return $this->errno;
return $this->errer_number;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,11 +856,11 @@ function validator(array $inputs, array $rules): \Bow\Validation\Validate
* Get Route by name
*
* @param string $name
* @param array $data
* @param bool|array $data
* @param bool $absolute
* @return string
*/
function route(string $name, array $data = [], $absolute = false)
function route(string $name, bool|array $data = [], bool $absolute = false)
{
$routes = config('app.routes');

Expand All @@ -881,12 +881,12 @@ function route(string $name, array $data = [], $absolute = false)
if (preg_match_all('/(?::([a-zA-Z0-9_-]+\??))/', $url, $matches)) {
$keys = end($matches);
foreach ($keys as $key) {
if (preg_match("/?$/", $key)) {
if (preg_match("/\?$/", $key)) {
$valide_key = trim($key, "?");
$value = $data[$valide_key] ?? "";
unset($data[$valide_key]);
} else {
if (isset($data[$key])) {
if (!isset($data[$key])) {
throw new InvalidArgumentException(
"The $key key is not provide"
);
Expand Down
10 changes: 5 additions & 5 deletions tests/Cache/CacheFilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ public function test_get_cache()
$this->assertEquals(Cache::get('name'), 'Dakia');
}

public function test_AddWithCallbackCache()
public function test_add_with_callback_cache()
{
$result = Cache::add('lastname', fn () => 'Franck');
$result = $result && Cache::add('age', fn () => 25, 20000);

$this->assertEquals($result, true);
}

public function test_GetCallbackCache()
public function test_get_callback_cache()
{
$this->assertEquals(Cache::get('lastname'), 'Franck');

$this->assertEquals(Cache::get('age'), 25);
}

public function test_AddArrayCache()
public function test_add_array_cache()
{
$result = Cache::add('address', [
'tel' => "49929598",
Expand All @@ -53,7 +53,7 @@ public function test_AddArrayCache()
$this->assertEquals($result, true);
}

public function test_GetArrayCache()
public function test_get_array_cache()
{
$result = Cache::get('address');

Expand Down Expand Up @@ -94,7 +94,7 @@ public function test_time_of_empty()
{
$result = Cache::timeOf('lastname');

$this->assertEquals('+', $result);
$this->assertTrue(is_numeric($result));
}

public function test_time_of_empty_2()
Expand Down

0 comments on commit a2ef07f

Please sign in to comment.