Skip to content

Commit cc62767

Browse files
author
Ian Barber
committed
First pass at stream tidy ups
Adding options for request timeout Adding a null cache class Tests still required
1 parent 8271966 commit cc62767

5 files changed

Lines changed: 94 additions & 10 deletions

File tree

src/Google/Cache/Null.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/*
3+
* Copyright 2014 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
require_once "Google/Cache/Abstract.php";
19+
require_once "Google/Cache/Exception.php";
20+
21+
/**
22+
* A blank storage class, for cases where caching is not
23+
* required.
24+
*/
25+
class Google_Cache_Null extends Google_Cache_Abstract
26+
{
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function get($key, $expiration = false)
31+
{
32+
return false;
33+
}
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public function set($key, $value)
39+
{
40+
// Nop.
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
* @param String $key
46+
*/
47+
public function delete($key)
48+
{
49+
// Nop.
50+
}
51+
}

src/Google/Config.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ public function __construct($ini_file_location = null)
5151

5252
// Definition of class specific values, like file paths and so on.
5353
'classes' => array(
54-
// If you want to pass in OAuth 2.0 settings, they will need to be
55-
// structured like this.
54+
'Google_IO_Abstract' => array(
55+
'request_timeout_seconds' => 100,
56+
),
5657
'Google_Http_Request' => array(
5758
// Disable the use of gzip on calls if set to true. Defaults to false.
5859
'disable_gzip' => self::GZIP_ENABLED,
@@ -63,6 +64,8 @@ public function __construct($ini_file_location = null)
6364
// a production environment.
6465
'enable_gzip_for_uploads' => self::GZIP_UPLOADS_DISABLED,
6566
),
67+
// If you want to pass in OAuth 2.0 settings, they will need to be
68+
// structured like this.
6669
'Google_Auth_OAuth2' => array(
6770
// Keys for OAuth 2.0 access, see the API console at
6871
// https://developers.google.com/console

src/Google/IO/Abstract.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ abstract class Google_IO_Abstract
3737
public function __construct(Google_Client $client)
3838
{
3939
$this->client = $client;
40+
$timeout = $client->getClassConfig('Google_IO_Abstract', 'request_timeout_seconds');
41+
if ($timeout > 0) {
42+
$this->setTimeout($timeout);
43+
}
4044
}
4145

4246
/**
@@ -51,6 +55,12 @@ abstract public function executeRequest(Google_Http_Request $request);
5155
* @param $options
5256
*/
5357
abstract public function setOptions($options);
58+
59+
/**
60+
* Set the maximum request time in seconds.
61+
* @param $timeout in seconds
62+
*/
63+
abstract public function setTimeout($timeout);
5464

5565
/**
5666
* @visible for testing.

src/Google/IO/Curl.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class Google_IO_Curl extends Google_IO_Abstract
3737
public function executeRequest(Google_Http_Request $request)
3838
{
3939
$curl = curl_init();
40-
40+
4141
if ($request->getPostBody()) {
4242
curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody());
4343
}
44-
44+
4545
$requestHeaders = $request->getRequestHeaders();
4646
if ($requestHeaders && is_array($requestHeaders)) {
4747
$curlHeaders = array();
@@ -60,11 +60,11 @@ public function executeRequest(Google_Http_Request $request)
6060
curl_setopt($curl, CURLOPT_HEADER, true);
6161

6262
curl_setopt($curl, CURLOPT_URL, $request->getUrl());
63-
63+
6464
if ($request->canGzip()) {
6565
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
6666
}
67-
67+
6868
foreach ($this->options as $key => $var) {
6969
curl_setopt($curl, $key, $var);
7070
}
@@ -90,6 +90,15 @@ public function executeRequest(Google_Http_Request $request)
9090
*/
9191
public function setOptions($options)
9292
{
93-
$this->options = $options;
93+
$this->options = array_merge($this->options, $options);
94+
}
95+
96+
/**
97+
* Set the maximum request time in seconds.
98+
* @param $timeout in seconds
99+
*/
100+
public function setTimeout($timeout)
101+
{
102+
$this->options[CURLOPT_TIMEOUT] = $timeout;
94103
}
95104
}

src/Google/IO/Stream.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
class Google_IO_Stream extends Google_IO_Abstract
2727
{
28+
const TIMEOUT = "timeout";
2829
const ZLIB = "compress.zlib://";
30+
private $options = array();
2931

3032
private static $DEFAULT_HTTP_CONTEXT = array(
3133
"follow_location" => 0,
@@ -119,7 +121,7 @@ public function executeRequest(Google_Http_Request $request)
119121
}
120122

121123
$responseHeaders = $this->getHttpResponseHeaders($http_response_header);
122-
124+
123125
return array($response_data, $responseHeaders, $respHttpCode);
124126
}
125127

@@ -129,9 +131,18 @@ public function executeRequest(Google_Http_Request $request)
129131
*/
130132
public function setOptions($options)
131133
{
132-
// NO-OP
134+
$this->options = array_merge($this->options, $options);
133135
}
134-
136+
137+
/**
138+
* Set the maximum request time in seconds.
139+
* @param $timeout in seconds
140+
*/
141+
public function setTimeout($timeout)
142+
{
143+
$this->options[self::TIMEOUT] = $timeout;
144+
}
145+
135146
protected function getHttpResponseCode($response_headers)
136147
{
137148
$header_count = count($response_headers);

0 commit comments

Comments
 (0)