NEW: Adding Guzzle dependency.

This commit is contained in:
Frank Mullenger 2019-02-15 11:30:51 +13:00
parent f6652c2b37
commit e323674adf
2 changed files with 48 additions and 1 deletions

View File

@ -16,7 +16,8 @@
],
"require": {
"silverstripe/framework": "^4.0",
"silverstripe/versioned": "^1.0"
"silverstripe/versioned": "^1.0",
"guzzlehttp/guzzle": "^6.3.3"
},
"require-dev": {
"phpunit/phpunit": "^5.7",

46
src/Traits/Fetcher.php Normal file
View File

@ -0,0 +1,46 @@
<?php
namespace SilverStripe\EnvironmentCheck\Traits;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
/**
* Simple helper for fetching responses using Guzzle client.
*
* @package environmentcheck
*/
trait Fetcher
{
/**
* Configuration for the Guzzle client
*
* @var array
*/
protected $clientConfig = [];
/**
* Merges configuration arrays and returns the result
*
* @param array $extraConfig
* @return array
*/
private function getClientConfig(array $extraConfig = []) : array
{
return array_merge($this->clientConfig, $extraConfig);
}
/**
* Fetch a response for a URL using Guzzle client.
*
* @param string $url
* @param array|null $extraConfig Extra configuration
* @return ResponseInterface
*/
public function fetchResponse(string $url, ? array $extraConfig = []) : ? ResponseInterface
{
$config = $this->getClientConfig($extraConfig);
$client = new Client($config);
return $client->get($url);
}
}