Skip to content

Commit

Permalink
Add fallback support for when cURL multi is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed May 19, 2019
1 parent 2a85079 commit 610ec1f
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/FasterImage/FasterImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ class FasterImage
*/
public function batch(array $urls)
{
$has_curl_multi = (
function_exists( 'curl_multi_init' )
&&
function_exists( 'curl_multi_exec' )
&&
function_exists( 'curl_multi_add_handle' )
&&
function_exists( 'curl_multi_select' )
&&
defined( 'CURLM_OK' )
&&
defined( 'CURLM_CALL_MULTI_PERFORM' )
);
$has_curl_multi = false; // TEMP for unit testing.
if ( ! $has_curl_multi ) {
return $this->batchSynchronously($urls);
}

$multi = curl_multi_init();
$results = array();
Expand Down Expand Up @@ -112,6 +129,33 @@ public function batch(array $urls)
return $results;
}

/**
* Get the size of each of the urls in a list, using synchronous method
*
* @param array $urls
*
* @return array
* @throws \Exception
*/
protected function batchSynchronously(array $urls) {
$results = [];
foreach ( array_values($urls) as $count => $uri ) {
$results[$uri] = [];

$ch = $this->handle($uri, $results[$uri]);

curl_exec($ch);

// We can't check return value because the buffer size is too small and curl_error() will always be "Failed writing body".
if ( empty($results[$uri]) ) {
throw new \Exception("Curl handle for $uri could not be executed");
}

curl_close($ch);
}
return $results;
}

/**
* @param int $seconds
*/
Expand Down Expand Up @@ -271,7 +315,7 @@ protected function handle($url, & $result)
*/
//
// hey curl! this is an error. But really we just are stopping cause
// we already have what we wwant
// we already have what we want
return -1;
});

Expand Down

0 comments on commit 610ec1f

Please sign in to comment.