Skip to content

Commit ce708e7

Browse files
committed
Merge pull request #2 from asafdav/master
Fixes and changes
2 parents 4149d26 + 0572291 commit ce708e7

40 files changed

+1164
-796
lines changed

src/GoogleApi/Auth/Auth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @author Chris Chabot <chabotc@google.com>
2424
*
2525
*/
26-
abstract class apiAuth {
26+
abstract class Auth {
2727
abstract public function authenticate($service);
2828
abstract public function sign(HttpRequest $request);
2929
abstract public function createAuthUrl($scope);

src/GoogleApi/Auth/AuthNone.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace GoogleApi\Auth;
1818

1919
use GoogleApi\Io\HttpRequest;
20+
use GoogleApi\Config;
2021

2122
/**
2223
* Do-nothing authentication implementation, use this if you want to make un-authenticated calls
@@ -27,9 +28,8 @@ class AuthNone extends Auth {
2728
public $key = null;
2829

2930
public function __construct() {
30-
global $apiConfig;
31-
if (!empty($apiConfig['developer_key'])) {
32-
$this->setDeveloperKey($apiConfig['developer_key']);
31+
if (Config::has('developer_key')) {
32+
$this->setDeveloperKey(Config::get('developer_key'));
3333
}
3434
}
3535

src/GoogleApi/Auth/OAuth2.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use GoogleApi\Client;
2020
use GoogleApi\Io\HttpRequest;
2121
use GoogleApi\Service\Utils;
22+
use GoogleApi\Config;
2223

2324
/**
2425
* Authentication class that deals with the OAuth 2 web-server authentication flow
@@ -53,7 +54,7 @@ class OAuth2 extends Auth {
5354
* to the discretion of the caller (which is done by calling authenticate()).
5455
*/
5556
public function __construct() {
56-
global $apiConfig;
57+
$apiConfig = Config::getAll();
5758

5859
if (! empty($apiConfig['developer_key'])) {
5960
$this->developerKey = $apiConfig['developer_key'];

src/GoogleApi/Cache/FileCache.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
namespace GoogleApi\Cache;
1818

19+
use GoogleApi\Config;
20+
1921
/*
2022
* This class implements a basic on disk storage. While that does
2123
* work quite well it's not the most elegant and scalable solution.
@@ -29,8 +31,7 @@ class FileCache extends Cache {
2931
private $path;
3032

3133
public function __construct() {
32-
global $apiConfig;
33-
$this->path = $apiConfig['ioFileCache_directory'];
34+
$this->path = Config::get('ioFileCache_directory');
3435
}
3536

3637
private function isLocked($storageFile) {

src/GoogleApi/Cache/MemcacheCache.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
namespace GoogleApi\Cache;
1818

19+
use GoogleApi\Config;
20+
1921
/**
2022
* A persistent storage class based on the memcache, which is not
2123
* really very persistent, as soon as you restart your memcache daemon
@@ -28,12 +30,11 @@ class MemcacheCache extends Cache {
2830
private $connection = false;
2931

3032
public function __construct() {
31-
global $apiConfig;
3233
if (! function_exists('memcache_connect')) {
3334
throw new Exception("Memcache functions not available");
3435
}
35-
$this->host = $apiConfig['ioMemCacheCache_host'];
36-
$this->port = $apiConfig['ioMemCacheCache_port'];
36+
$this->host = Config::get('ioMemCacheCache_host');
37+
$this->port = Config::get('ioMemCacheCache_port');
3738
if (empty($this->host) || empty($this->port)) {
3839
throw new Exception("You need to supply a valid memcache host and port");
3940
}

src/GoogleApi/Client.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/*
34
* Copyright 2010 Google Inc.
45
*
@@ -19,6 +20,7 @@
1920
use GoogleApi\Auth;
2021
use GoogleApi\Io;
2122
use GoogleApi\Cache;
23+
use GoogleApi;
2224

2325
/**
2426
* The Google API Client
@@ -64,7 +66,7 @@ class Client {
6466
private $authenticated = false;
6567

6668
public function __construct($config = array()) {
67-
global $apiConfig;
69+
$apiConfig = Config::getAll();
6870
$apiConfig = array_merge($apiConfig, $config);
6971
self::$cache = new $apiConfig['cacheClass']();
7072
self::$auth = new $apiConfig['authClass']();
@@ -75,9 +77,9 @@ public function __construct($config = array()) {
7577
* Add a service
7678
*/
7779
public function addService($service, $version = false) {
78-
global $apiConfig;
80+
$apiConfig = Config::getAll();
7981
if ($this->authenticated) {
80-
throw new apiException('Cant add services after having authenticated');
82+
throw new \GoogleApi\Exception('Cant add services after having authenticated');
8183
}
8284
$this->services[$service] = array();
8385
if (isset($apiConfig['services'][$service])) {
@@ -204,17 +206,15 @@ public function setApprovalPrompt($approvalPrompt) {
204206
* @param string $applicationName
205207
*/
206208
public function setApplicationName($applicationName) {
207-
global $apiConfig;
208-
$apiConfig['application_name'] = $applicationName;
209+
Config::set('application_name', $applicationName);
209210
}
210211

211212
/**
212213
* Set the OAuth 2.0 Client ID.
213214
* @param string $clientId
214215
*/
215216
public function setClientId($clientId) {
216-
global $apiConfig;
217-
$apiConfig['oauth2_client_id'] = $clientId;
217+
Config::set('oauth2_client_id', $clientId);
218218
self::$auth->clientId = $clientId;
219219
}
220220

@@ -223,8 +223,7 @@ public function setClientId($clientId) {
223223
* @param string $clientSecret
224224
*/
225225
public function setClientSecret($clientSecret) {
226-
global $apiConfig;
227-
$apiConfig['oauth2_client_secret'] = $clientSecret;
226+
Config::set('oauth2_client_secret', $clientSecret);
228227
self::$auth->clientSecret = $clientSecret;
229228
}
230229

@@ -233,8 +232,7 @@ public function setClientSecret($clientSecret) {
233232
* @param string $redirectUri
234233
*/
235234
public function setRedirectUri($redirectUri) {
236-
global $apiConfig;
237-
$apiConfig['oauth2_redirect_uri'] = $redirectUri;
235+
Config::set('oauth2_redirect_uri', $redirectUri);
238236
self::$auth->redirectUri = $redirectUri;
239237
}
240238

@@ -296,8 +294,7 @@ public function setScopes($scopes) {
296294
* @experimental
297295
*/
298296
public function setUseObjects($useObjects) {
299-
global $apiConfig;
300-
$apiConfig['use_objects'] = $useObjects;
297+
Config::set('use_objects', $useObjects);
301298
}
302299

303300
/**
@@ -327,7 +324,7 @@ public static function getIo() {
327324
}
328325

329326
/**
330-
* @return Cache\Cache the implementation of Cache\Cache.
327+
* @return \GoogleApi\Cache\Cache the implementation of Cache\Cache.
331328
*/
332329
public function getCache() {
333330
return Client::$cache;

src/GoogleApi/Config.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
/*
3+
* Copyright 2010 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+
namespace GoogleApi;
19+
20+
/**
21+
* This class stores all the configuration information that is needed for running an GoogleApi/Client.
22+
* Since this project was shifted to PSR-0 standard, Its required to stop using "global" and use an OO method.
23+
*
24+
* @author Asaf David <asafdav@gmail.com>
25+
*
26+
*/
27+
class Config {
28+
protected static $apiConfig = array();
29+
30+
/**
31+
* Initializes $apiConfig,
32+
* Php can't parse non-trivial expressions in data-members initializers, therefore a workaround is needed.
33+
* This method will be called to initialize $apiConfig with the default values.
34+
*
35+
* @static
36+
*/
37+
static function init() {
38+
self::$apiConfig = array(
39+
// True if objects should be returned by the service classes.
40+
// False if associative arrays should be returned (default behavior).
41+
'use_objects' => false,
42+
43+
// The application_name is included in the User-Agent HTTP header.
44+
'application_name' => '',
45+
46+
// OAuth2 Settings, you can get these keys at https://code.google.com/apis/console
47+
'oauth2_client_id' => '',
48+
'oauth2_client_secret' => '',
49+
'oauth2_redirect_uri' => '',
50+
51+
// The developer key, you get this at https://code.google.com/apis/console
52+
'developer_key' => '',
53+
54+
// OAuth1 Settings.
55+
// If you're using the apiOAuth auth class, it will use these values for the oauth consumer key and secret.
56+
// See http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html for info on how to obtain those
57+
'oauth_consumer_key' => 'anonymous',
58+
'oauth_consumer_secret' => 'anonymous',
59+
60+
// Site name to show in the Google's OAuth 1 authentication screen.
61+
'site_name' => 'www.example.org',
62+
63+
// Which Authentication, Storage and HTTP IO classes to use.
64+
'authClass' => 'GoogleApi\Auth\OAuth2',
65+
'ioClass' => 'GoogleApi\Io\CurlIO',
66+
'cacheClass' => 'GoogleApi\Cache\FileCache',
67+
68+
// If you want to run the test suite (by running # phpunit AllTests.php in the tests/ directory), fill in the settings below
69+
'oauth_test_token' => '', // the oauth access token to use (which you can get by runing authenticate() as the test user and copying the token value), ie '{"key":"foo","secret":"bar","callback_url":null}'
70+
'oauth_test_user' => '', // and the user ID to use, this can either be a vanity name 'testuser' or a numberic ID '123456'
71+
72+
// Don't change these unless you're working against a special development or testing environment.
73+
'basePath' => 'https://www.googleapis.com',
74+
75+
// IO Class dependent configuration, you only have to configure the values for the class that was configured as the ioClass above
76+
'ioFileCache_directory' =>
77+
(function_exists('sys_get_temp_dir') ?
78+
sys_get_temp_dir() . '/apiClient' :
79+
'/tmp/apiClient'),
80+
'ioMemCacheStorage_host' => '127.0.0.1',
81+
'ioMemcacheStorage_port' => '11211',
82+
83+
// Definition of service specific values like scopes, oauth token URLs, etc
84+
'services' => array(
85+
'analytics' => array('scope' => 'https://www.googleapis.com/auth/analytics.readonly'),
86+
'calendar' => array(
87+
'scope' => array(
88+
"https://www.googleapis.com/auth/calendar",
89+
"https://www.googleapis.com/auth/calendar.readonly",
90+
)
91+
),
92+
'books' => array('scope' => 'https://www.googleapis.com/auth/books'),
93+
'latitude' => array(
94+
'scope' => array(
95+
'https://www.googleapis.com/auth/latitude.all.best',
96+
'https://www.googleapis.com/auth/latitude.all.city',
97+
)
98+
),
99+
'moderator' => array('scope' => 'https://www.googleapis.com/auth/moderator'),
100+
'oauth2' => array(
101+
'scope' => array(
102+
'https://www.googleapis.com/auth/userinfo.profile',
103+
'https://www.googleapis.com/auth/userinfo.email',
104+
)
105+
),
106+
'plus' => array('scope' => 'https://www.googleapis.com/auth/plus.me'),
107+
'siteVerification' => array('scope' => 'https://www.googleapis.com/auth/siteverification'),
108+
'tasks' => array('scope' => 'https://www.googleapis.com/auth/tasks'),
109+
'urlshortener' => array('scope' => 'https://www.googleapis.com/auth/urlshortener')
110+
)
111+
);
112+
}
113+
114+
115+
/**
116+
* Returns an array which contains all of the variables that were declared.
117+
* @static
118+
* @return array
119+
*/
120+
public static function getAll() {
121+
return self::$apiConfig;
122+
}
123+
124+
/**
125+
* Returns the value of the wanted variable, If it doesn't exist $default will be returned.
126+
*
127+
* @static
128+
* @param $variable
129+
* @param null $default
130+
* @return string|array
131+
*/
132+
public static function get($variable, $default = null) {
133+
return isset(self::$apiConfig[$variable]) ? self::$apiConfig[$variable] : $default;
134+
}
135+
136+
/**
137+
* Checks whether a variable exists
138+
* @static
139+
* @param $variable
140+
* @return bool
141+
*/
142+
public static function has($variable) {
143+
return isset(self::$apiConfig[$variable]);
144+
}
145+
146+
/**
147+
* Sets the value of the wanted variable
148+
* @static
149+
* @param $variable
150+
* @param $value
151+
*/
152+
public static function set($variable, $value)
153+
{
154+
self::$apiConfig[$variable] = $value;
155+
}
156+
157+
}
158+
Config::init(); // Workaround to initialize the static data-member

0 commit comments

Comments
 (0)