Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/GoogleApi/Auth/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author Chris Chabot <chabotc@google.com>
*
*/
abstract class apiAuth {
abstract class Auth {
abstract public function authenticate($service);
abstract public function sign(HttpRequest $request);
abstract public function createAuthUrl($scope);
Expand Down
6 changes: 3 additions & 3 deletions src/GoogleApi/Auth/AuthNone.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace GoogleApi\Auth;

use GoogleApi\Io\HttpRequest;
use GoogleApi\Config;

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

public function __construct() {
global $apiConfig;
if (!empty($apiConfig['developer_key'])) {
$this->setDeveloperKey($apiConfig['developer_key']);
if (Config::has('developer_key')) {
$this->setDeveloperKey(Config::get('developer_key'));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/GoogleApi/Auth/OAuth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use GoogleApi\Client;
use GoogleApi\Io\HttpRequest;
use GoogleApi\Service\Utils;
use GoogleApi\Config;

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

if (! empty($apiConfig['developer_key'])) {
$this->developerKey = $apiConfig['developer_key'];
Expand Down
5 changes: 3 additions & 2 deletions src/GoogleApi/Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
namespace GoogleApi\Cache;

use GoogleApi\Config;

/*
* This class implements a basic on disk storage. While that does
* work quite well it's not the most elegant and scalable solution.
Expand All @@ -29,8 +31,7 @@ class FileCache extends Cache {
private $path;

public function __construct() {
global $apiConfig;
$this->path = $apiConfig['ioFileCache_directory'];
$this->path = Config::get('ioFileCache_directory');
}

private function isLocked($storageFile) {
Expand Down
7 changes: 4 additions & 3 deletions src/GoogleApi/Cache/MemcacheCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
namespace GoogleApi\Cache;

use GoogleApi\Config;

/**
* A persistent storage class based on the memcache, which is not
* really very persistent, as soon as you restart your memcache daemon
Expand All @@ -28,12 +30,11 @@ class MemcacheCache extends Cache {
private $connection = false;

public function __construct() {
global $apiConfig;
if (! function_exists('memcache_connect')) {
throw new Exception("Memcache functions not available");
}
$this->host = $apiConfig['ioMemCacheCache_host'];
$this->port = $apiConfig['ioMemCacheCache_port'];
$this->host = Config::get('ioMemCacheCache_host');
$this->port = Config::get('ioMemCacheCache_port');
if (empty($this->host) || empty($this->port)) {
throw new Exception("You need to supply a valid memcache host and port");
}
Expand Down
25 changes: 11 additions & 14 deletions src/GoogleApi/Client.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright 2010 Google Inc.
*
Expand All @@ -19,6 +20,7 @@
use GoogleApi\Auth;
use GoogleApi\Io;
use GoogleApi\Cache;
use GoogleApi;

/**
* The Google API Client
Expand Down Expand Up @@ -64,7 +66,7 @@ class Client {
private $authenticated = false;

public function __construct($config = array()) {
global $apiConfig;
$apiConfig = Config::getAll();
$apiConfig = array_merge($apiConfig, $config);
self::$cache = new $apiConfig['cacheClass']();
self::$auth = new $apiConfig['authClass']();
Expand All @@ -75,9 +77,9 @@ public function __construct($config = array()) {
* Add a service
*/
public function addService($service, $version = false) {
global $apiConfig;
$apiConfig = Config::getAll();
if ($this->authenticated) {
throw new apiException('Cant add services after having authenticated');
throw new \GoogleApi\Exception('Cant add services after having authenticated');
}
$this->services[$service] = array();
if (isset($apiConfig['services'][$service])) {
Expand Down Expand Up @@ -204,17 +206,15 @@ public function setApprovalPrompt($approvalPrompt) {
* @param string $applicationName
*/
public function setApplicationName($applicationName) {
global $apiConfig;
$apiConfig['application_name'] = $applicationName;
Config::set('application_name', $applicationName);
}

/**
* Set the OAuth 2.0 Client ID.
* @param string $clientId
*/
public function setClientId($clientId) {
global $apiConfig;
$apiConfig['oauth2_client_id'] = $clientId;
Config::set('oauth2_client_id', $clientId);
self::$auth->clientId = $clientId;
}

Expand All @@ -223,8 +223,7 @@ public function setClientId($clientId) {
* @param string $clientSecret
*/
public function setClientSecret($clientSecret) {
global $apiConfig;
$apiConfig['oauth2_client_secret'] = $clientSecret;
Config::set('oauth2_client_secret', $clientSecret);
self::$auth->clientSecret = $clientSecret;
}

Expand All @@ -233,8 +232,7 @@ public function setClientSecret($clientSecret) {
* @param string $redirectUri
*/
public function setRedirectUri($redirectUri) {
global $apiConfig;
$apiConfig['oauth2_redirect_uri'] = $redirectUri;
Config::set('oauth2_redirect_uri', $redirectUri);
self::$auth->redirectUri = $redirectUri;
}

Expand Down Expand Up @@ -296,8 +294,7 @@ public function setScopes($scopes) {
* @experimental
*/
public function setUseObjects($useObjects) {
global $apiConfig;
$apiConfig['use_objects'] = $useObjects;
Config::set('use_objects', $useObjects);
}

/**
Expand Down Expand Up @@ -327,7 +324,7 @@ public static function getIo() {
}

/**
* @return Cache\Cache the implementation of Cache\Cache.
* @return \GoogleApi\Cache\Cache the implementation of Cache\Cache.
*/
public function getCache() {
return Client::$cache;
Expand Down
158 changes: 158 additions & 0 deletions src/GoogleApi/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace GoogleApi;

/**
* This class stores all the configuration information that is needed for running an GoogleApi/Client.
* Since this project was shifted to PSR-0 standard, Its required to stop using "global" and use an OO method.
*
* @author Asaf David <asafdav@gmail.com>
*
*/
class Config {
protected static $apiConfig = array();

/**
* Initializes $apiConfig,
* Php can't parse non-trivial expressions in data-members initializers, therefore a workaround is needed.
* This method will be called to initialize $apiConfig with the default values.
*
* @static
*/
static function init() {
self::$apiConfig = array(
// True if objects should be returned by the service classes.
// False if associative arrays should be returned (default behavior).
'use_objects' => false,

// The application_name is included in the User-Agent HTTP header.
'application_name' => '',

// OAuth2 Settings, you can get these keys at https://code.google.com/apis/console
'oauth2_client_id' => '',
'oauth2_client_secret' => '',
'oauth2_redirect_uri' => '',

// The developer key, you get this at https://code.google.com/apis/console
'developer_key' => '',

// OAuth1 Settings.
// If you're using the apiOAuth auth class, it will use these values for the oauth consumer key and secret.
// See http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html for info on how to obtain those
'oauth_consumer_key' => 'anonymous',
'oauth_consumer_secret' => 'anonymous',

// Site name to show in the Google's OAuth 1 authentication screen.
'site_name' => 'www.example.org',

// Which Authentication, Storage and HTTP IO classes to use.
'authClass' => 'GoogleApi\Auth\OAuth2',
'ioClass' => 'GoogleApi\Io\CurlIO',
'cacheClass' => 'GoogleApi\Cache\FileCache',

// If you want to run the test suite (by running # phpunit AllTests.php in the tests/ directory), fill in the settings below
'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}'
'oauth_test_user' => '', // and the user ID to use, this can either be a vanity name 'testuser' or a numberic ID '123456'

// Don't change these unless you're working against a special development or testing environment.
'basePath' => 'https://www.googleapis.com',

// IO Class dependent configuration, you only have to configure the values for the class that was configured as the ioClass above
'ioFileCache_directory' =>
(function_exists('sys_get_temp_dir') ?
sys_get_temp_dir() . '/apiClient' :
'/tmp/apiClient'),
'ioMemCacheStorage_host' => '127.0.0.1',
'ioMemcacheStorage_port' => '11211',

// Definition of service specific values like scopes, oauth token URLs, etc
'services' => array(
'analytics' => array('scope' => 'https://www.googleapis.com/auth/analytics.readonly'),
'calendar' => array(
'scope' => array(
"https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/calendar.readonly",
)
),
'books' => array('scope' => 'https://www.googleapis.com/auth/books'),
'latitude' => array(
'scope' => array(
'https://www.googleapis.com/auth/latitude.all.best',
'https://www.googleapis.com/auth/latitude.all.city',
)
),
'moderator' => array('scope' => 'https://www.googleapis.com/auth/moderator'),
'oauth2' => array(
'scope' => array(
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
)
),
'plus' => array('scope' => 'https://www.googleapis.com/auth/plus.me'),
'siteVerification' => array('scope' => 'https://www.googleapis.com/auth/siteverification'),
'tasks' => array('scope' => 'https://www.googleapis.com/auth/tasks'),
'urlshortener' => array('scope' => 'https://www.googleapis.com/auth/urlshortener')
)
);
}


/**
* Returns an array which contains all of the variables that were declared.
* @static
* @return array
*/
public static function getAll() {
return self::$apiConfig;
}

/**
* Returns the value of the wanted variable, If it doesn't exist $default will be returned.
*
* @static
* @param $variable
* @param null $default
* @return string|array
*/
public static function get($variable, $default = null) {
return isset(self::$apiConfig[$variable]) ? self::$apiConfig[$variable] : $default;
}

/**
* Checks whether a variable exists
* @static
* @param $variable
* @return bool
*/
public static function has($variable) {
return isset(self::$apiConfig[$variable]);
}

/**
* Sets the value of the wanted variable
* @static
* @param $variable
* @param $value
*/
public static function set($variable, $value)
{
self::$apiConfig[$variable] = $value;
}

}
Config::init(); // Workaround to initialize the static data-member
Loading