mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-24 18:13:00 +01:00
Autoloading functions.php and exposing uriTemplate as namespaced function
This commit is contained in:
parent
54003a2ae9
commit
1a950970a2
@ -22,7 +22,8 @@
|
|||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-0": {
|
"psr-0": {
|
||||||
"Guzzle": "src/"
|
"Guzzle": "src/"
|
||||||
}
|
},
|
||||||
|
"files": ["src/Guzzle/functions.php"]
|
||||||
},
|
},
|
||||||
|
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
@ -39,9 +39,6 @@ class Client implements ClientInterface
|
|||||||
/** @var Collection Parameter object holding configuration data */
|
/** @var Collection Parameter object holding configuration data */
|
||||||
private $config;
|
private $config;
|
||||||
|
|
||||||
/** @var UriTemplate */
|
|
||||||
private static $uriTemplate;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clients accept an array of constructor parameters.
|
* Clients accept an array of constructor parameters.
|
||||||
*
|
*
|
||||||
@ -219,27 +216,6 @@ class Client implements ClientInterface
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Expand a URI template
|
|
||||||
*
|
|
||||||
* @param string $template Template to expand
|
|
||||||
* @param array $variables Variables to inject
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function expandTemplate($template, array $variables)
|
|
||||||
{
|
|
||||||
if (function_exists('uri_template')) {
|
|
||||||
return uri_template($template, $variables);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!self::$uriTemplate) {
|
|
||||||
self::$uriTemplate = new UriTemplate();
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$uriTemplate->expand($template, $variables);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand a URI template and inherit from the base URL if it's relative
|
* Expand a URI template and inherit from the base URL if it's relative
|
||||||
*
|
*
|
||||||
@ -249,20 +225,20 @@ class Client implements ClientInterface
|
|||||||
*/
|
*/
|
||||||
private function buildUrl($url)
|
private function buildUrl($url)
|
||||||
{
|
{
|
||||||
if (is_array($url)) {
|
if (!is_array($url)) {
|
||||||
list($url, $templateVars) = $url;
|
// Use absolute URLs as is
|
||||||
} else {
|
return substr($url, 0, 4) === 'http'
|
||||||
$templateVars = [];
|
? (string) $url
|
||||||
|
: (string) Url::fromString($this->getBaseUrl())->combine($url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
list($url, $templateVars) = $url;
|
||||||
if (substr($url, 0, 4) === 'http') {
|
if (substr($url, 0, 4) === 'http') {
|
||||||
// Use absolute URLs as-is
|
return \Guzzle\uriTemplate($url, $templateVars);
|
||||||
return $this->expandTemplate($url, $templateVars);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (string) Url::fromString(
|
return (string) Url::fromString($this->getBaseUrl())
|
||||||
$this->getBaseUrl())->combine($this->expandTemplate($url, $templateVars)
|
->combine(\Guzzle\uriTemplate($url, $templateVars));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,6 +4,7 @@ namespace Guzzle;
|
|||||||
|
|
||||||
use Guzzle\Http\Client;
|
use Guzzle\Http\Client;
|
||||||
use Guzzle\Http\Message\ResponseInterface;
|
use Guzzle\Http\Message\ResponseInterface;
|
||||||
|
use Guzzle\Url\UriTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a custom request
|
* Send a custom request
|
||||||
@ -126,3 +127,23 @@ function options($url, array $headers = [], $options = [])
|
|||||||
{
|
{
|
||||||
return request('OPTIONS', $url, $headers, $options);
|
return request('OPTIONS', $url, $headers, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expands a URI template
|
||||||
|
*
|
||||||
|
* @param string $template URI template
|
||||||
|
* @param array $variables Template variables
|
||||||
|
*/
|
||||||
|
function uriTemplate($template, array $variables)
|
||||||
|
{
|
||||||
|
if (function_exists('uri_template')) {
|
||||||
|
return uri_template($template, $variables);
|
||||||
|
}
|
||||||
|
|
||||||
|
static $uriTemplate;
|
||||||
|
if (!$uriTemplate) {
|
||||||
|
$uriTemplate = new UriTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $uriTemplate->expand($template, $variables);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user