Skip to content

Commit

Permalink
Merge pull request #248 from bowphp/5.x-refactoring-for-perf
Browse files Browse the repository at this point in the history
[5.x] Refactoring For Performance
  • Loading branch information
papac authored Jun 1, 2023
2 parents a2ef07f + 627a232 commit 8fa0223
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/Database/Migration/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class Migration
*
* @var AbstractConnection
*/
private $adapter;
private AbstractConnection $adapter;

/**
* Migration constructor
Expand Down Expand Up @@ -58,6 +58,16 @@ final public function connection(string $name): Migration
return $this;
}

/**
* Get adapter name
*
* @return string
*/
public function getAdapterName(): string
{
return $this->adapter->getName();
}

/**
* Drop table action
*
Expand Down
39 changes: 33 additions & 6 deletions src/Http/Client/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ class HttpClient
*
* @var array
*/
private $attach = [];
private array $attach = [];

/**
* Define the accept json header
*
* @var boolean
*/
private bool $accept_json = false;

/**
* The headers collection
Expand Down Expand Up @@ -74,8 +81,7 @@ public function setBaseUrl(string $url): void
public function get(string $url, array $data = []): Response
{
if (count($data) > 0) {
$params = http_build_query($data);
$url . "?" . $params;
$url = $url . "?" . http_build_query($data);
}

$this->init($url);
Expand Down Expand Up @@ -200,6 +206,20 @@ public function setUserAgent(string $user_agent): HttpClient
return $this;
}

/**
* Set the json accept prop to format the sent content in json
*
* @return HttpClient
*/
public function acceptJson(): HttpClient
{
$this->accept_json = true;

$this->addHeaders(["Content-Type" => "application/json"]);

return $this;
}

/**
* Reset alway connection
*
Expand All @@ -223,9 +243,13 @@ private function init(string $url): void
*/
private function addFields(array $data): void
{
if (count($data) > 0) {
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));
if ($this->accept_json) {
$payload = json_encode($data);
} else {
$payload = http_build_query($data);
}

curl_setopt($this->ch, CURLOPT_POSTFIELDS, $payload);
}

/**
Expand Down Expand Up @@ -256,7 +280,10 @@ private function execute(): string
$this->close();

if ($content === false) {
throw new \Exception(curl_strerror($errno));
throw new HttpClientException(
curl_strerror($errno),
$errno
);
}

return $content;
Expand Down
11 changes: 11 additions & 0 deletions src/Http/Client/HttpClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Bow\Http\Client;

use Exception;

class HttpClientException extends Exception
{
}
27 changes: 27 additions & 0 deletions src/Http/Exception/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class HttpException extends Exception
*/
protected $status = 'OK';

/**
* Define the errors bags
*
* @var array
*/
protected array $error_bags = [];

/**
* HttpException constructor
*
Expand Down Expand Up @@ -47,4 +54,24 @@ public function getStatusCode()
{
return $this->getCode();
}

/**
* Set the errors bags
*
* @param array $errors
*/
public function setErrorBags(array $errors)
{
$this->error_bags = $errors;
}

/**
* Get the errors bags
*
* @return array
*/
public function getErrorBags(): array
{
return $this->error_bags;
}
}
2 changes: 1 addition & 1 deletion src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private function __construct()
$this->id = "req_" . sha1(uniqid() . time());

if ($this->getHeader('content-type') == 'application/json') {
$data = json_decode(file_get_contents("php://input"), true);
$data = json_decode(file_get_contents("php://input"), true, 1024, JSON_THROW_ON_ERROR);
$this->input = array_merge((array) $data, $_GET);
} else {
$data = $_POST ?? [];
Expand Down
13 changes: 11 additions & 2 deletions src/Support/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Bow\Support;

use Bow\Application\Exception\ApplicationException;

class Env
{
/**
Expand Down Expand Up @@ -52,9 +54,16 @@ public static function load(string $filename)
// Get the env file content
$content = file_get_contents($filename);

static::$envs = json_decode(trim($content), true);
$envs = json_decode(trim($content), true, 1024);

if (json_last_error()) {
throw new ApplicationException(
json_last_error_msg() . ": check your env json and synthax please."
);
}

static::$envs = static::bindVariables(static::$envs);
static::$envs = $envs;
static::$envs = static::bindVariables($envs);

foreach (static::$envs as $key => $value) {
$key = Str::upper(trim($key));
Expand Down
6 changes: 3 additions & 3 deletions src/Validation/Rules/RegexRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ trait RegexRule
* Check that the contents of the field with a regular expression
*
* @param string $key
* @param string $masque
* @param string|int|float $masque
* @return void
*/
protected function compileRegex(string $key, string $masque): void
protected function compileRegex(string $key, string|int|float $masque): void
{
if (!preg_match("/^regex:(.+)+$/", $masque, $match)) {
if (!preg_match("/^regex:(.+)+$/", (string) $masque, $match)) {
return;
}

Expand Down

0 comments on commit 8fa0223

Please sign in to comment.