Skip to content

Commit

Permalink
Add Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
VennDev authored Jul 30, 2023
1 parent d93a46c commit cd67798
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/vennv/vapm/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@

use Throwable;
use function file_get_contents;
use function curl_init;
use function curl_multi_init;
use function curl_multi_add_handle;
use function curl_multi_exec;
use function curl_multi_remove_handle;
use function curl_multi_close;
use function curl_multi_getcontent;
use const CURLOPT_RETURNTRANSFER;
use const CURLM_OK;

interface SystemInterface {

Expand Down Expand Up @@ -67,6 +76,15 @@ public static function clearInterval(SampleMacro $sampleMacro) : void;
*/
public static function fetch(string $url, array $options = []) : Promise;

/**
* @param array<int, string> $curls
* @return Promise
* @throws Throwable
*
* Use this to curl multiple addresses at once
*/
public static function fetchAll(array $curls) : Promise;

/**
* @throws Throwable
*
Expand Down Expand Up @@ -154,6 +172,52 @@ public static function fetch(string $url, array $options = []) : Promise {
});
}

/**
* @param array<int, string> $curls
* @return Promise
* @throws Throwable
*
* Use this to curl multiple addresses at once
*/
public static function fetchAll(array $curls) : Promise {
return new Promise(function ($resolve, $reject) use ($curls) : void {
$multiHandle = curl_multi_init();
$handles = [];

foreach ($curls as $url) {
$handle = curl_init($url);

curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);

Check failure on line 190 in src/vennv/vapm/System.php

View workflow job for this annotation

GitHub Actions / Jobs (8.2)

Parameter #1 $handle of function curl_setopt expects CurlHandle, CurlHandle|false given.
curl_multi_add_handle($multiHandle, $handle);

Check failure on line 191 in src/vennv/vapm/System.php

View workflow job for this annotation

GitHub Actions / Jobs (8.2)

Parameter #2 $handle of function curl_multi_add_handle expects CurlHandle, CurlHandle|false given.

$handles[] = $handle;
}

$running = null;

do {
$status = curl_multi_exec($multiHandle, $running);

if ($status !== CURLM_OK) {
$reject(Error::FAILED_IN_FETCHING_DATA);
}

FiberManager::wait();
} while ($running > 0);

$results = [];

foreach ($handles as $handle) {
$results[] = curl_multi_getcontent($handle);

Check failure on line 211 in src/vennv/vapm/System.php

View workflow job for this annotation

GitHub Actions / Jobs (8.2)

Parameter #1 $handle of function curl_multi_getcontent expects CurlHandle, CurlHandle|false given.
curl_multi_remove_handle($multiHandle, $handle);

Check failure on line 212 in src/vennv/vapm/System.php

View workflow job for this annotation

GitHub Actions / Jobs (8.2)

Parameter #2 $handle of function curl_multi_remove_handle expects CurlHandle, CurlHandle|false given.
}

curl_multi_close($multiHandle);

$resolve($results);
});
}

/**
* @throws Throwable
*/
Expand Down

0 comments on commit cd67798

Please sign in to comment.