1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 10:03:27 +01:00

Autoloading functions.php and exposing uriTemplate as namespaced function

This commit is contained in:
Michael Dowling 2013-09-28 15:50:49 -07:00
parent 54003a2ae9
commit 1a950970a2
3 changed files with 32 additions and 34 deletions

View File

@ -22,7 +22,8 @@
"autoload": {
"psr-0": {
"Guzzle": "src/"
}
},
"files": ["src/Guzzle/functions.php"]
},
"require-dev": {

View File

@ -39,9 +39,6 @@ class Client implements ClientInterface
/** @var Collection Parameter object holding configuration data */
private $config;
/** @var UriTemplate */
private static $uriTemplate;
/**
* 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
*
@ -249,20 +225,20 @@ class Client implements ClientInterface
*/
private function buildUrl($url)
{
if (is_array($url)) {
list($url, $templateVars) = $url;
} else {
$templateVars = [];
if (!is_array($url)) {
// Use absolute URLs as is
return substr($url, 0, 4) === 'http'
? (string) $url
: (string) Url::fromString($this->getBaseUrl())->combine($url);
}
list($url, $templateVars) = $url;
if (substr($url, 0, 4) === 'http') {
// Use absolute URLs as-is
return $this->expandTemplate($url, $templateVars);
return \Guzzle\uriTemplate($url, $templateVars);
}
return (string) Url::fromString(
$this->getBaseUrl())->combine($this->expandTemplate($url, $templateVars)
);
return (string) Url::fromString($this->getBaseUrl())
->combine(\Guzzle\uriTemplate($url, $templateVars));
}
/**

View File

@ -4,6 +4,7 @@ namespace Guzzle;
use Guzzle\Http\Client;
use Guzzle\Http\Message\ResponseInterface;
use Guzzle\Url\UriTemplate;
/**
* Send a custom request
@ -126,3 +127,23 @@ function options($url, array $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);
}