Skip to content

Commit

Permalink
Merge pull request #240 from bowphp/5.x-refactoring-for-perf
Browse files Browse the repository at this point in the history
[5.x] Fixes the hasone relationship and Refactoring the http client
  • Loading branch information
papac authored May 23, 2023
2 parents 036834b + 2429916 commit bcfd0c0
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 220 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"aws/aws-sdk-php": "^3.87",
"phpstan/phpstan": "^0.12.87",
"php-amqplib/php-amqplib": "^3.0",
"bowphp/policier": "dev-master",
"bowphp/policier": "^3.0",
"mockery/mockery": "^1.5",
"spatie/phpunit-snapshot-assertions": "^4.2",
"predis/predis": "^2.1"
Expand Down
3 changes: 2 additions & 1 deletion src/Database/Barry/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function __construct(Model $related, Model $parent, string $foreign_key,

$this->local_key = $local_key;
$this->foreign_key = $foreign_key;
$this->query = $this->query->where($this->foreign_key, $this->parent->getKeyValue());

$this->query = $this->query->where($this->foreign_key, $this->parent->$local_key);
}

/**
Expand Down
97 changes: 81 additions & 16 deletions src/Http/Client/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ public function get(string $url, array $data = []): Response
}

$this->init($url);
$this->applyCommonOptions();

curl_setopt($this->ch, CURLOPT_HTTPGET, true);

return new Response($this->ch);
$content = $this->execute();

return new Response($this->ch, $content);
}

/**
Expand All @@ -99,11 +102,14 @@ public function post(string $url, array $data = []): Response
$data = array_merge($this->attach, $data);
}

$this->addFields($data);
$this->applyCommonOptions();

curl_setopt($this->ch, CURLOPT_POST, true);

$this->addFields($data);
$content = $this->execute();

return new Response($this->ch);
return new Response($this->ch, $content);
}

/**
Expand All @@ -116,14 +122,33 @@ public function post(string $url, array $data = []): Response
public function put(string $url, array $data = []): Response
{
$this->init($url);

if (!curl_setopt($this->ch, CURLOPT_PUT, true)) {
$this->addFields($data);
}
$this->addFields($data);
$this->applyCommonOptions();

curl_setopt($this->ch, CURLOPT_PUT, true);

return new Response($this->ch);
$content = $this->execute();

return new Response($this->ch, $content);
}

/**
* Make put requete
*
* @param string $url
* @param array $data
* @return Response
*/
public function delete(string $url, array $data = []): Response
{
$this->init($url);
$this->addFields($data);
$this->applyCommonOptions();

curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$content = $this->execute();

return new Response($this->ch, $content);
}

/**
Expand All @@ -145,16 +170,14 @@ public function addAttach(string|array $attach): array
*/
public function addHeaders(array $headers): HttpClient
{
if (is_resource($this->ch)) {
$data = [];

foreach ($headers as $key => $value) {
$data[] = $key . ': ' . $value;
}
$data = [];

curl_setopt($this->ch, CURLOPT_HTTPHEADER, $data);
foreach ($headers as $key => $value) {
$data[] = $key . ': ' . $value;
}

curl_setopt($this->ch, CURLOPT_HTTPHEADER, $data);

return $this;
}

Expand All @@ -166,7 +189,7 @@ public function addHeaders(array $headers): HttpClient
*/
private function init(string $url): void
{
if (!is_null($this->base_url)) {
if (is_null($this->base_url)) {
$url = $this->base_url . "/" . trim($url, "/");
}

Expand All @@ -185,4 +208,46 @@ private function addFields(array $data): void
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
}

/**
* Close connection
*
* @return void
*/
private function close(): void
{
curl_close($this->ch);
}

/**
* Execute request
*
* @return string
* @throws \Exception
*/
private function execute(): string
{
$content = curl_exec($this->ch);
$errno = curl_errno($this->ch);

$this->close();

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

return $content;
}

/**
* Set Curl CURLOPT_RETURNTRANSFER option
*
* @return bool
*/
private function applyCommonOptions()
{
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);
}
}
Loading

0 comments on commit bcfd0c0

Please sign in to comment.