Skip to content

Commit

Permalink
Efficient curl resource handling
Browse files Browse the repository at this point in the history
  • Loading branch information
QuintenJustus committed Jun 8, 2022
1 parent 96f49ea commit 73e8359
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protoqol/quo-php",
"version": "0.3.0",
"version": "0.3.1",
"description": "PHP - Quo package.",
"license": "MIT",
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/Http/QuoCurlHandle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Protoqol\Quo\Http;

class QuoCurlHandle
{
/**
* Get curl instance.
*
* @return resource
*/
public static function make()
{
return curl_init();
}

/**
* Destroy curl instance.
*
* @param $handle
*
* @return void
*/
public static function destroy($handle)
{
curl_close($handle);
}
}
4 changes: 2 additions & 2 deletions src/Http/QuoPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ private function getVariableNames()

while (!str_contains($src, 'quo(')) {
$src .= (file(
$backtrace[$backtrack]['file']
))[$backtrace[$backtrack]['line'] - $i] . ($i === 1 ? "," : "");
$backtrace[$backtrack]['file']
))[$backtrace[$backtrack]['line'] - $i] . ($i === 1 ? "," : "");
$i++;
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/Http/QuoRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ class QuoRequest
private $userAgent = "Protoqol/Quo";

/**
* @param $requester
* @param string $hostname
* @param int $port
* @param bool $verbose
*/
public function __construct(string $hostname, int $port, bool $verbose = false)
public function __construct($requester, string $hostname, int $port, bool $verbose = false)
{
$this->hostname = $hostname;
$this->port = $port;
$this->client = curl_init();
$this->client = $requester;
$this->setHeaders();

if ($verbose) {
Expand Down Expand Up @@ -111,8 +112,6 @@ public function send()

$this->captureErrors();

curl_close($this->client);

return $response;
}

Expand Down
12 changes: 9 additions & 3 deletions src/Quo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Protoqol\Quo\Config\QuoConfig;
use Protoqol\Quo\Http\QuoCurlHandle;
use Protoqol\Quo\Http\QuoPayload;
use Protoqol\Quo\Http\QuoRequest;
use Protoqol\Quo\Http\QuoResponse;
Expand All @@ -17,13 +18,14 @@ class Quo
private $request;

/**
* @param $requester
* @param QuoConfig $config
*
* @throws Exception
*/
public function __construct(QuoConfig $config)
public function __construct($requester, QuoConfig $config)
{
$this->request = new QuoRequest($config->getHostname(), $config->getPort());
$this->request = new QuoRequest($requester, $config->getHostname(), $config->getPort());
}

/**
Expand All @@ -46,7 +48,9 @@ public static function make()
return [];
}

$quo = new Quo($config);
$requester = QuoCurlHandle::make();

$quo = new Quo($requester, $config);

foreach ($args as $argument) {
try {
Expand All @@ -64,6 +68,8 @@ public static function make()
}
}

QuoCurlHandle::destroy($requester);

return $args;
}

Expand Down

0 comments on commit 73e8359

Please sign in to comment.