Skip to content

Commit

Permalink
Merge branch "1.x" into "master".
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloKowalczyk committed Sep 22, 2018
2 parents e304bcb + 428d025 commit 56c81dd
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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)

Expand Down
1 change: 0 additions & 1 deletion composer.json
Expand Up @@ -26,7 +26,6 @@
},
"require": {
"php": "^7.1.3",
"ext-curl": "*",
"jeremeamia/superclosure": "^2.2",
"monolog/monolog": "^1.19",
"mtdowling/cron-expression": "^1.1",
Expand Down
23 changes: 23 additions & 0 deletions config/services.xml
Expand Up @@ -196,9 +196,32 @@
<argument type="service" id="Symfony\Component\Console\Style\SymfonyStyle" />
</service>

<service
class="Crunz\HttpClient\StreamHttpClient"
id="Crunz\HttpClient\StreamHttpClient"
public="false"
/>

<service
class="Crunz\HttpClient\FallbackHttpClient"
id="Crunz\HttpClient\FallbackHttpClient"
public="false"
>
<argument id="Crunz\HttpClient\StreamHttpClient" type="service"/>
<argument id="Crunz\HttpClient\CurlHttpClient" type="service"/>
<argument type="service" id="Crunz\Logger\ConsoleLoggerInterface"/>
</service>

<service
class="Crunz\HttpClient\CurlHttpClient"
id="Crunz\HttpClient\CurlHttpClient"
public="false"
/>

<service
alias="Crunz\HttpClient\FallbackHttpClient"
id="Crunz\HttpClient\HttpClientInterface"
public="false"
/>

<service
Expand Down
80 changes: 80 additions & 0 deletions src/HttpClient/FallbackHttpClient.php
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Crunz\HttpClient;

use Crunz\Logger\ConsoleLoggerInterface;

final class FallbackHttpClient implements HttpClientInterface
{
/** @var StreamHttpClient */
private $streamHttpClient;
/** @var CurlHttpClient */
private $curlHttpClient;
/** @var HttpClientInterface|null */
private $httpClient;
/** @var ConsoleLoggerInterface */
private $consoleLogger;

public function __construct(
StreamHttpClient $streamHttpClient,
CurlHttpClient $curlHttpClient,
ConsoleLoggerInterface $consoleLogger
) {
$this->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 <info>CurlHttpClient</info>.');

return $this->httpClient;
}

if ('1' === \ini_get('allow_url_fopen')) {
$this->httpClient = $this->streamHttpClient;

$this->consoleLogger
->debug("'allow_url_fopen' enabled, use <info>StreamHttpClient</info>");

return $this->httpClient;
}

$this->consoleLogger
->debug('<error>Choosing HttpClient implementation failed.</error>');

throw new HttpClientException(
"Unable to choose HttpClient. Enable cURL extension (preffered) or turn on 'allow_url_fopen' in php.ini."
);
}
}
42 changes: 42 additions & 0 deletions src/HttpClient/StreamHttpClient.php
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Crunz\HttpClient;

final class StreamHttpClient implements HttpClientInterface
{
/**
* @param string $url
*
* @throws HttpClientException
*/
public function ping($url)
{
$context = \stream_context_create(
[
'http' => [
'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);
}
}
}
24 changes: 24 additions & 0 deletions tests/Unit/HttpClient/StreamHttpClientTest.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Crunz\Tests\Unit\HttpClient;

use Crunz\HttpClient\HttpClientException;
use Crunz\HttpClient\StreamHttpClient;
use PHPUnit\Framework\TestCase;

final class StreamHttpClientTest extends TestCase
{
/** @test */
public function pingFailWithInvalidAddress()
{
$this->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');
}
}

0 comments on commit 56c81dd

Please sign in to comment.