Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mamor committed May 13, 2013
1 parent 9860cb4 commit 26e6bfb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 49 deletions.
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,26 @@ https://code.google.com/p/google-api-php-client/
## Install
### Setup to fuel/packages/gdata
* Use composer https://packagist.org/packages/mp-php/fuel-packages-gdata
* git submodule add
* git submodule
* Download zip

## Usage
### 1: Configuration
1. Copy packages/gdata/config/gdata.php to under app/config directory.
2. Edit gdata.php that copied.
### Configuration

### 2: Enable Gdata package.
##### In app/config/config.php
##### One
In app/config/config.php

'always_load' => array('packages' => array(
'gdata',
...

or

##### In your code
or in your code

Package::load('gdata');

### 3: Forge Gdata
##### Two
Copy packages/gdata/config/gdata.php to under app/config directory and edit

### Usage

$gdata = Gdata::forge(
$service_name,
Expand Down
108 changes: 76 additions & 32 deletions classes/gdata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,41 @@

namespace Gdata;

/**
* FuelPHP Gdata package
*
* @author Mamoru Otsuka http://madroom-project.blogspot.jp/
* @copyright 2013 Mamoru Otsuka
* @license MIT License http://www.opensource.org/licenses/mit-license.php
*/
class Gdata
{
/**
* @var Gdata
*/
protected static $_instance;
protected static $_instances = array();

public $client;
public $service;
/**
* @var array
*/
protected static $_instances = array();

/*
* Initialize
*/
public static function _init()
{
\Config::load('gdata', true);
}


public function __construct($service, $name, $config)
{
require_once PKGPATH."gdata/vendor/google-api-php-client/src/Google_Client.php";

$service_class = 'Google_'.ucfirst(strtolower($service)).'Service';
require_once PKGPATH."gdata/vendor/google-api-php-client/src/contrib/{$service_class}.php";

if( ! class_exists($service_class))
{
throw new \FuelException('Could not find Gdata service class: '.$service_class);
}

$config = array_merge(\Config::get('gdata'), $config);

$client = new \Google_Client();
$client->setApplicationName($config['application_name']);
$client->setClientId($config['client_id']);
$client->setClientSecret($config['client_secret']);
$client->setRedirectUri($config['redirect_uri']);
$client->setDeveloperKey($config['api_key']);
$client->setScopes($config['scopes']);
$client->setAccessType($config['access_type']);

$this->client = $client;
$this->service = new $service_class($client);
}

/**
* Forge
*
* @param string $service
* @param string $name
* @param array $config
* @return Gdata
*/
public static function forge($service = null, $name = 'default', array $config = array())
{

Expand All @@ -52,7 +46,7 @@ public static function forge($service = null, $name = 'default', array $config =
return $exists;
}

static::$_instances[$name] = new static($service, $name, $config);
static::$_instances[$name] = new static($service, $config);

if ($name == 'default')
{
Expand All @@ -62,6 +56,12 @@ public static function forge($service = null, $name = 'default', array $config =
return static::$_instances[$name];
}

/**
* Get instance
*
* @param string $instance
* @return mixed
*/
public static function instance($instance = null)
{
if ($instance !== null)
Expand All @@ -82,6 +82,50 @@ public static function instance($instance = null)
return static::$_instance;
}

/**
* Google \Google_Client
*/
public $client;

/**
* Google Service Class
*/
public $service;

/**
* Constructor
*
* @param string $service
* @param array $config
* @throws \FuelException
*/
public function __construct($service, array $config)
{
require_once PKGPATH."gdata/vendor/google-api-php-client/src/Google_Client.php";

$service_class = 'Google_'.ucfirst(strtolower($service)).'Service';
require_once PKGPATH."gdata/vendor/google-api-php-client/src/contrib/{$service_class}.php";

if( ! class_exists($service_class))
{
throw new \FuelException('Could not find Gdata service class: '.$service_class);
}

$config = array_merge(\Config::get('gdata'), $config);

$client = new \Google_Client();
$client->setApplicationName($config['application_name']);
$client->setClientId($config['client_id']);
$client->setClientSecret($config['client_secret']);
$client->setRedirectUri($config['redirect_uri']);
$client->setDeveloperKey($config['api_key']);
$client->setScopes($config['scopes']);
$client->setAccessType($config['access_type']);

$this->client = $client;
$this->service = new $service_class($client);
}

}

/* end of file gdata.php */
13 changes: 7 additions & 6 deletions config/gdata.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

return array(
'application_name' => '',
'client_id' => '',
'client_secret' => '',
'redirect_uri' => '',
'api_key' => '',
'scopes' => array(),
'access_type' => 'offline',
'client_id' => '',
'client_secret' => '',
'redirect_uri' => '',
'api_key' => '',
'scopes' => array(),
'access_type' => 'offline',
);

0 comments on commit 26e6bfb

Please sign in to comment.