mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-24 10:03:27 +01:00
Cleaning up client privates and configuration checks
This commit is contained in:
parent
19a2823517
commit
24d1aac0ed
@ -2,7 +2,6 @@
|
||||
|
||||
namespace Guzzle\Http;
|
||||
|
||||
use Guzzle\Common\Collection;
|
||||
use Guzzle\Common\HasDispatcherTrait;
|
||||
use Guzzle\Http\Adapter\FakeBatchAdapter;
|
||||
use Guzzle\Http\Event\RequestEvents;
|
||||
@ -37,7 +36,7 @@ class Client implements ClientInterface
|
||||
/** @var string Base URL of the client */
|
||||
private $baseUrl;
|
||||
|
||||
/** @var Collection Parameter object holding configuration data */
|
||||
/** @var array Configuration data */
|
||||
private $config;
|
||||
|
||||
/**
|
||||
@ -70,34 +69,11 @@ class Client implements ClientInterface
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
// Add default request options
|
||||
if (!isset($config['defaults'])) {
|
||||
$config['defaults'] = $this->getDefaultOptions();
|
||||
} else {
|
||||
$config['defaults'] = array_replace($this->getDefaultOptions(), $config['defaults']);
|
||||
}
|
||||
|
||||
$this->configureBaseUrl($config);
|
||||
$this->configureDefaults($config);
|
||||
$this->messageFactory = isset($config['message_factory']) ? $config['message_factory'] : new MessageFactory();
|
||||
$this->baseUrl = isset($config['base_url']) ? $this->buildUrl($config['base_url']) : '';
|
||||
$this->adapter = isset($config['adapter']) ? $config['adapter'] : $this->getDefaultAdapter();
|
||||
|
||||
// If no batch adapter was explicitly provided and one was not defaulted
|
||||
// when creating the default adapter, then create one now
|
||||
if (isset($config['batch_adapter'])) {
|
||||
$this->batchAdapter = $config['batch_adapter'];
|
||||
} elseif (!$this->batchAdapter) {
|
||||
$this->batchAdapter = $this->getDefaultBatchAdapter();
|
||||
}
|
||||
|
||||
// Add the default user-agent header
|
||||
if (!isset($config['defaults']['headers'])) {
|
||||
$config['defaults']['headers'] = ['User-Agent' => static::getDefaultUserAgent()];
|
||||
} elseif (!isset(array_change_key_case($config['defaults']['headers'])['user-agent'])) {
|
||||
// Add the User-Agent header if one was not already set
|
||||
$config['defaults']['headers']['User-Agent'] = static::getDefaultUserAgent();
|
||||
}
|
||||
|
||||
$this->config = new Collection($config);
|
||||
$this->configureAdapter($config);
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -110,36 +86,32 @@ class Client implements ClientInterface
|
||||
return 'Guzzle/' . Version::VERSION . ' curl/' . curl_version()['version'] . ' PHP/' . PHP_VERSION;
|
||||
}
|
||||
|
||||
public function getConfig($key)
|
||||
public function getBaseUrl()
|
||||
{
|
||||
return $this->config->getPath($key);
|
||||
return isset($this->config['base_url']) ? (string) $this->baseUrl : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a default request option from the client
|
||||
* Returns default client configuration values
|
||||
*
|
||||
* @param string $keyOrPath request.options key (e.g. allow_redirects) or path to a nested key (e.g. headers/foo)
|
||||
*
|
||||
* @return mixed|null
|
||||
* @return array
|
||||
*/
|
||||
public function getDefaultOption($keyOrPath)
|
||||
public function getDefaults()
|
||||
{
|
||||
return $this->config->getPath("defaults/{$keyOrPath}");
|
||||
return $this->config['defaults'];
|
||||
}
|
||||
|
||||
public function createRequest($method, $url = null, array $headers = [], $body = null, array $options = [])
|
||||
{
|
||||
// Merge in default options
|
||||
if ($default = $this->config->get('defaults')) {
|
||||
$options = array_replace_recursive($default, $options);
|
||||
}
|
||||
$options = array_replace_recursive($this->config['defaults'], $options);
|
||||
|
||||
// Use a clone of the client's event dispatcher
|
||||
$options['constructor_options'] = ['event_dispatcher' => clone $this->getEventDispatcher()];
|
||||
|
||||
$request = $this->messageFactory->createRequest(
|
||||
$method,
|
||||
$url ? (string) $this->buildUrl($url) : (string) $this->getBaseUrl(),
|
||||
$url ? (string) $this->buildUrl($url) : (string) $this->baseUrl,
|
||||
$headers,
|
||||
$body,
|
||||
$options
|
||||
@ -148,11 +120,6 @@ class Client implements ClientInterface
|
||||
return $request;
|
||||
}
|
||||
|
||||
public function getBaseUrl()
|
||||
{
|
||||
return $this->baseUrl;
|
||||
}
|
||||
|
||||
public function get($url = null, array $headers = [], $options = [])
|
||||
{
|
||||
return $this->send($this->createRequest('GET', $url, $headers, null, $options));
|
||||
@ -242,11 +209,12 @@ class Client implements ClientInterface
|
||||
*/
|
||||
private function buildUrl($url)
|
||||
{
|
||||
$baseUrl = clone $this->baseUrl;
|
||||
if (!is_array($url)) {
|
||||
// Use absolute URLs as is
|
||||
return substr($url, 0, 4) === 'http'
|
||||
? (string) $url
|
||||
: (string) Url::fromString($this->getBaseUrl())->combine($url);
|
||||
: (string) $baseUrl->combine($url);
|
||||
}
|
||||
|
||||
list($url, $templateVars) = $url;
|
||||
@ -254,8 +222,9 @@ class Client implements ClientInterface
|
||||
return \Guzzle\uriTemplate($url, $templateVars);
|
||||
}
|
||||
|
||||
return (string) Url::fromString($this->getBaseUrl())
|
||||
->combine(\Guzzle\uriTemplate($url, $templateVars));
|
||||
return (string) $baseUrl->combine(
|
||||
\Guzzle\uriTemplate($url, $templateVars)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -296,4 +265,44 @@ class Client implements ClientInterface
|
||||
throw new \RuntimeException('The curl extension must be installed or you must set allow_url_fopen to true');
|
||||
}
|
||||
}
|
||||
|
||||
private function configureBaseUrl($config)
|
||||
{
|
||||
if (!isset($config['base_url'])) {
|
||||
$this->baseUrl = new Url('', '');
|
||||
} elseif (is_array($config['base_url'])) {
|
||||
$this->baseUrl = \Guzzle\uriTemplate($config['base_url'][0], $config['base_url'][1]);
|
||||
} else {
|
||||
$this->baseUrl = Url::fromString($config['base_url']);
|
||||
}
|
||||
}
|
||||
|
||||
private function configureDefaults(&$config)
|
||||
{
|
||||
if (isset($config['defaults'])) {
|
||||
$config['defaults'] = array_replace($this->getDefaultOptions(), $config['defaults']);
|
||||
} else {
|
||||
$config['defaults'] = $this->getDefaultOptions();
|
||||
}
|
||||
|
||||
// Add the default user-agent header
|
||||
if (!isset($config['defaults']['headers'])) {
|
||||
$config['defaults']['headers'] = ['User-Agent' => static::getDefaultUserAgent()];
|
||||
} elseif (!isset(array_change_key_case($config['defaults']['headers'])['user-agent'])) {
|
||||
// Add the User-Agent header if one was not already set
|
||||
$config['defaults']['headers']['User-Agent'] = static::getDefaultUserAgent();
|
||||
}
|
||||
}
|
||||
|
||||
private function configureAdapter(&$config)
|
||||
{
|
||||
$this->adapter = isset($config['adapter']) ? $config['adapter'] : $this->getDefaultAdapter();
|
||||
// If no batch adapter was explicitly provided and one was not defaulted
|
||||
// when creating the default adapter, then create one now
|
||||
if (isset($config['batch_adapter'])) {
|
||||
$this->batchAdapter = $config['batch_adapter'];
|
||||
} elseif (!$this->batchAdapter) {
|
||||
$this->batchAdapter = $this->getDefaultBatchAdapter();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -129,7 +129,7 @@ interface ClientInterface extends HasDispatcherInterface
|
||||
public function sendAll($requests, array $options = []);
|
||||
|
||||
/**
|
||||
* Get the client's base URL
|
||||
* Get the client's base URL (if any is specified)
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
|
@ -23,9 +23,9 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
public function testUsesDefaultDefaultOptions()
|
||||
{
|
||||
$client = new Client();
|
||||
$this->assertTrue($client->getDefaultOption('allow_redirects'));
|
||||
$this->assertTrue($client->getDefaultOption('exceptions'));
|
||||
$this->assertContains('cacert.pem', $client->getDefaultOption('verify'));
|
||||
$this->assertTrue($client->getDefaults()['allow_redirects']);
|
||||
$this->assertTrue($client->getDefaults()['exceptions']);
|
||||
$this->assertContains('cacert.pem', $client->getDefaults()['verify']);
|
||||
}
|
||||
|
||||
public function testUsesProvidedDefaultOptions()
|
||||
@ -36,10 +36,10 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
'query' => ['foo' => 'bar']
|
||||
]
|
||||
]);
|
||||
$this->assertFalse($client->getDefaultOption('allow_redirects'));
|
||||
$this->assertTrue($client->getDefaultOption('exceptions'));
|
||||
$this->assertContains('cacert.pem', $client->getDefaultOption('verify'));
|
||||
$this->assertEquals(['foo' => 'bar'], $client->getDefaultOption('query'));
|
||||
$this->assertFalse($client->getDefaults()['allow_redirects']);
|
||||
$this->assertTrue($client->getDefaults()['exceptions']);
|
||||
$this->assertContains('cacert.pem', $client->getDefaults()['verify']);
|
||||
$this->assertEquals(['foo' => 'bar'], $client->getDefaults()['query']);
|
||||
}
|
||||
|
||||
public function testCanSpecifyBaseUrl()
|
||||
@ -105,20 +105,14 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddsDefaultUserAgentHeaderWithDefaultOptions()
|
||||
{
|
||||
$client = new Client(['defaults' => ['allow_redirects' => false]]);
|
||||
$this->assertFalse($client->getDefaultOption('allow_redirects'));
|
||||
$this->assertEquals(['User-Agent' => Client::getDefaultUserAgent()], $client->getDefaultOption('headers'));
|
||||
$this->assertFalse($client->getDefaults()['allow_redirects']);
|
||||
$this->assertEquals(['User-Agent' => Client::getDefaultUserAgent()], $client->getDefaults()['headers']);
|
||||
}
|
||||
|
||||
public function testAddsDefaultUserAgentHeaderWithoutDefaultOptions()
|
||||
{
|
||||
$client = new Client();
|
||||
$this->assertEquals(['User-Agent' => Client::getDefaultUserAgent()], $client->getDefaultOption('headers'));
|
||||
}
|
||||
|
||||
public function testProvidesConfigPathValues()
|
||||
{
|
||||
$client = new Client(['foo' => ['baz' => 'bar']]);
|
||||
$this->assertEquals('bar', $client->getConfig('foo/baz'));
|
||||
$this->assertEquals(['User-Agent' => Client::getDefaultUserAgent()], $client->getDefaults()['headers']);
|
||||
}
|
||||
|
||||
private function getRequestClient()
|
||||
|
@ -103,7 +103,7 @@ class Server
|
||||
}
|
||||
|
||||
try {
|
||||
$this->client->get('guzzle-server/perf', [], ['timeout' => 5]);
|
||||
$this->client->get('guzzle-server/perf', [], ['timeout' => 1]);
|
||||
return $this->running = true;
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user