diff --git a/CHANGELOG.md b/CHANGELOG.md index 8270475e..eb29a9d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Removed `Crunz\Output\VerbosityAwareOutput` class - PR [#103](https://github.com/lavary/crunz/pull/103), [@PabloKowalczyk](https://github.com/PabloKowalczyk) +## 1.10.1 - 2018-09-22 + +### Fixed + +- Incompatibility for users without cURL extension but with enabled `allow_url_fopen` - PR [#139](https://github.com/lavary/crunz/pull/139) +by [@PabloKowalczyk](https://github.com/PabloKowalczyk) + ## 1.10.0 - 2018-09-22 ### Fixed diff --git a/README.md b/README.md index 5ce7b6fd..233e4f58 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Crunz is capable of executing any kind of executable command as well as PHP clos |Version|Linux build|Windows build| |---|---|---| -|stable (v1.10.0)|[![Build Status](https://img.shields.io/travis/lavary/crunz/v1.10.0.svg?style=flat-square)](https://travis-ci.org/lavary/crunz)|*Tag build not supported* +|stable (v1.10.1)|[![Build Status](https://img.shields.io/travis/lavary/crunz/v1.10.1.svg?style=flat-square)](https://travis-ci.org/lavary/crunz)|*Tag build not supported* |v2 (master/v2.x-dev)|[![Build Status](https://img.shields.io/travis/lavary/crunz/master.svg?style=flat-square)](https://travis-ci.org/lavary/crunz)|[![AppVeyor branch](https://img.shields.io/appveyor/ci/lavary/crunz/master.svg?style=flat-square)](https://ci.appveyor.com/project/lavary/crunz) |v1 (v1.x-dev)|[![Build Status](https://img.shields.io/travis/lavary/crunz/1.x.svg?style=flat-square)](https://travis-ci.org/lavary/crunz)|[![AppVeyor branch](https://img.shields.io/appveyor/ci/lavary/crunz/1.x.svg?style=flat-square)](https://ci.appveyor.com/project/lavary/crunz) diff --git a/composer.json b/composer.json index 55918b62..12399428 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,6 @@ }, "require": { "php": "^7.1.3", - "ext-curl": "*", "jeremeamia/superclosure": "^2.2", "monolog/monolog": "^1.19", "mtdowling/cron-expression": "^1.1", diff --git a/config/services.xml b/config/services.xml index 635a74e1..f4d9c218 100644 --- a/config/services.xml +++ b/config/services.xml @@ -196,9 +196,32 @@ + + + + + + + + + + streamHttpClient = $streamHttpClient; + $this->curlHttpClient = $curlHttpClient; + $this->consoleLogger = $consoleLogger; + } + + /** + * @param string $url + * + * @throws HttpClientException + */ + public function ping($url) + { + $httpClient = $this->chooseHttpClient(); + $httpClient->ping($url); + } + + /** + * @return HttpClientInterface + * + * @throws HttpClientException + */ + private function chooseHttpClient() + { + if (null !== $this->httpClient) { + return $this->httpClient; + } + + $this->consoleLogger + ->debug('Choosing HttpClient implementation.'); + + if (\function_exists('curl_exec')) { + $this->httpClient = $this->curlHttpClient; + + $this->consoleLogger + ->debug('cURL available, use CurlHttpClient.'); + + return $this->httpClient; + } + + if ('1' === \ini_get('allow_url_fopen')) { + $this->httpClient = $this->streamHttpClient; + + $this->consoleLogger + ->debug("'allow_url_fopen' enabled, use StreamHttpClient"); + + return $this->httpClient; + } + + $this->consoleLogger + ->debug('Choosing HttpClient implementation failed.'); + + throw new HttpClientException( + "Unable to choose HttpClient. Enable cURL extension (preffered) or turn on 'allow_url_fopen' in php.ini." + ); + } +} diff --git a/src/HttpClient/StreamHttpClient.php b/src/HttpClient/StreamHttpClient.php new file mode 100644 index 00000000..2f17640c --- /dev/null +++ b/src/HttpClient/StreamHttpClient.php @@ -0,0 +1,42 @@ + [ + 'user_agent' => 'Crunz StreamHttpClient', + 'timeout' => 5, + ], + ] + ); + $resource = @\fopen( + $url, + 'rb', + false, + $context + ); + + if (false === $resource) { + $error = \error_get_last(); + $errorMessage = $error['message'] ?? 'Unknown error'; + + throw new HttpClientException("Ping failed with message: \"{$errorMessage}\"."); + } + + if ($resource) { + \fclose($resource); + } + } +} diff --git a/tests/Unit/HttpClient/StreamHttpClientTest.php b/tests/Unit/HttpClient/StreamHttpClientTest.php new file mode 100644 index 00000000..404f04ba --- /dev/null +++ b/tests/Unit/HttpClient/StreamHttpClientTest.php @@ -0,0 +1,24 @@ +expectException(HttpClientException::class); + $this->expectExceptionMessage( + 'Ping failed with message: "fopen(http://www.wrong-address.tld): failed to open stream: php_network_getaddresses: getaddrinfo failed:' + ); + + $client = new StreamHttpClient(); + $client->ping('http://www.wrong-address.tld'); + } +}