Skip to content

Commit

Permalink
7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kawax committed May 25, 2024
1 parent fa70b16 commit a595c98
Show file tree
Hide file tree
Showing 22 changed files with 92 additions and 86 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
[![Test Coverage](https://api.codeclimate.com/v1/badges/20fdd1ca8f3737c383df/test_coverage)](https://codeclimate.com/github/kawax/laravel-google-sheets/test_coverage)

## Requirements
- PHP >= 8.1
- Laravel >= 10.0
- PHP >= 8.2
- Laravel >= 11.0

## Versioning
- Basic : semver
Expand Down Expand Up @@ -60,6 +60,13 @@ Another Google API Series.
- https://github.com/kawax/laravel-google-photos
- https://github.com/kawax/laravel-google-searchconsole

## Select auth type
You must select an authentication type and configure it appropriately.

- Service Account : Access to only your own spreadsheets.
- OAuth : Access to user's spreadsheets.
- API key: Access to public spreadsheets.

## Usage

| id | name | mail |
Expand Down Expand Up @@ -92,9 +99,10 @@ $values = Sheets::setAccessToken($token)->spreadsheet('spreadsheetId')->sheet('S
```

### Basic Non-Laravel Usage

```php
use Google\Client;
use Revolution\Google\Sheets\Sheets;
use Revolution\Google\Sheets\SheetsClient;

$client = new Client();
$client->setScopes([Google\Service\Sheets::DRIVE, Google\Service\Sheets::SPREADSHEETS]);
Expand All @@ -103,7 +111,7 @@ $client->setScopes([Google\Service\Sheets::DRIVE, Google\Service\Sheets::SPREADS

$service = new \Google\Service\Sheets($client);

$sheets = new Sheets();
$sheets = new SheetsClient();
$sheets->setService($service);

$values = $sheets->spreadsheet('spreadsheetID')->sheet('Sheet 1')->all();
Expand Down
9 changes: 9 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# UPGRADING

## 6.x to 7.0
- Require PHP>=8.2 and Laravel>=11.x
- Change Google Client namespace `Revolution\Google\Client`. Move to `lib/google/`. If you only use "Sheets", there will be little effect.
- Remove `Sheets` short Facade alias. Always recommended to use the full namespace.

```php
use Revolution\Google\Sheets\Facades\Sheets;
```

## 5.x to 6.0
- Require PHP>=8.0 and Laravel>=8.x

Expand Down
20 changes: 9 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
],
"license": "MIT",
"require": {
"php": "^8.1",
"illuminate/container": "^10.0||^11.0",
"illuminate/support": "^10.0||^11.0",
"google/apiclient": "^2.15"
"php": "^8.2",
"illuminate/container": "^11.0",
"illuminate/support": "^11.0",
"google/apiclient": "^2.16"
},
"require-dev": {
"orchestra/testbench": "^8.0||^9.0",
"orchestra/testbench": "^9.0",
"pulkitjalan/google-apiclient": "^6.2"
},
"autoload": {
"psr-4": {
"Revolution\\Google\\Sheets\\": "src/"
"Revolution\\Google\\Sheets\\": "src/",
"Revolution\\Google\\Client\\": "lib/google/"
}
},
"autoload-dev": {
"psr-4": {
"Revolution\\Google\\Sheets\\Tests\\": "tests/"
"Tests\\": "tests/"
}
},
"authors": [
Expand All @@ -40,11 +41,8 @@
"laravel": {
"providers": [
"Revolution\\Google\\Sheets\\Providers\\SheetsServiceProvider",
"Revolution\\Google\\Sheets\\Providers\\GoogleServiceProvider"
"Revolution\\Google\\Client\\Providers\\GoogleServiceProvider"
],
"aliases": {
"Sheets": "Revolution\\Google\\Sheets\\Facades\\Sheets"
},
"google/apiclient-services": [
"Drive",
"Sheets"
Expand Down
1 change: 0 additions & 1 deletion docs/google-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(GoogleClient::class, fn ($app) => new GoogleClient(config('google')));
$this->app->alias(GoogleClient::class, 'google-client');
}
}
Expand Down
6 changes: 1 addition & 5 deletions docs/trait.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ class User extends Authenticatable
use Notifiable;
use GoogleSheets;

protected $casts = [
'created' => 'datetime',
];

/**
* Get the Access Token
*
Expand All @@ -44,7 +40,7 @@ Add `sheetsAccessToken()`(abstract) for access_token.
Trait has `sheets()` that returns Sheets instance.

```php
use Revolution\Google\Sheets\Facades\Google;
use Revolution\Google\Client\Facades\Google;
use Revolution\Google\Sheets\Facades\Sheets;

public function __invoke(Request $request)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Revolution\Google\Sheets\Exceptions;
namespace Revolution\Google\Client\Exceptions;

class UnknownServiceException extends \Exception
{
Expand Down
4 changes: 2 additions & 2 deletions src/Facades/Google.php → lib/google/Facades/Google.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Revolution\Google\Sheets\Facades;
namespace Revolution\Google\Client\Facades;

use Illuminate\Support\Facades\Facade;
use Psr\Cache\CacheItemPoolInterface;
use Revolution\Google\Sheets\GoogleSheetClient;
use Revolution\Google\Client\GoogleSheetClient;

/**
* @method static mixed make(string $service)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Revolution\Google\Sheets;
namespace Revolution\Google\Client;

use BadMethodCallException;
use Google\Client as GoogleClient;
use Illuminate\Support\Arr;
use Revolution\Google\Sheets\Exceptions\UnknownServiceException;
use Revolution\Google\Client\Exceptions\UnknownServiceException;

class GoogleSheetClient
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Revolution\Google\Sheets\Providers;
namespace Revolution\Google\Client\Providers;

use Illuminate\Support\ServiceProvider;
use Revolution\Google\Sheets\GoogleSheetClient;
use Revolution\Google\Client\GoogleSheetClient;

class GoogleServiceProvider extends ServiceProvider
{
Expand All @@ -12,7 +12,7 @@ class GoogleServiceProvider extends ServiceProvider
*/
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../../config/google.php', 'google');
$this->mergeConfigFrom(__DIR__.'/../../../config/google.php', 'google');

$this->app->singleton(GoogleSheetClient::class, fn ($app) => new GoogleSheetClient($app['config']['google']));

Expand All @@ -26,7 +26,7 @@ public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../../config/google.php' => config_path('google.php'),
__DIR__.'/../../../config/google.php' => config_path('google.php'),
], 'google-config');
}
}
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<source>
<include>
<directory>src/</directory>
<directory>lib/google/</directory>
</include>
</source>
<coverage>
Expand Down
10 changes: 3 additions & 7 deletions src/Concerns/SheetsDrive.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Revolution\Google\Sheets\Concerns;

use Google\Service\Drive;
use Revolution\Google\Sheets\Facades\Google;
use Revolution\Google\Client\Facades\Google;

trait SheetsDrive
{
Expand All @@ -18,7 +18,7 @@ public function spreadsheetList(): array
->listFiles(
[
'q' => "mimeType = 'application/vnd.google-apps.spreadsheet'",
]
],
)
->getFiles();

Expand All @@ -38,10 +38,6 @@ public function setDriveService(mixed $drive): static

public function getDriveService(): Drive
{
if (is_null($this->drive)) {
$this->drive = Google::make(service: 'drive');
}

return $this->drive;
return $this->drive ??= Google::make('drive');
}
}
9 changes: 3 additions & 6 deletions src/Concerns/SheetsValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ public function orderAppendables(array $values): array
$ordered = [];
// Gets just the values of an array that has been re-ordered to match the header order
foreach ($values as $value) {
array_push(
$ordered,
array_values(array_replace(array_flip($header), $value))
);
$ordered[] = array_values(array_replace(array_flip($header), $value));
}

// Replaces null values with empty strings to work with Google's API
Expand All @@ -146,9 +143,9 @@ public function orderAppendables(array $values): array
// If key is the same as value, that's because the user
// didn't specify a header that exists in the sheet.
if (is_null($value) || $key === $value) {
array_push($notNull, '');
$notNull[] = '';
} else {
array_push($notNull, $value);
$notNull[] = $value;
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Facades/Sheets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Revolution\Google\Sheets\Facades;

use Google\Service\Drive;
use Google\Service\Sheets\AppendValuesResponse;
use Google\Service\Sheets as GoogleSheets;
use Google\Service\Sheets\BatchUpdateSpreadsheetResponse;
Expand Down Expand Up @@ -32,13 +33,16 @@
* @method static static valueRenderOption(string $valueRenderOption)
* @method static static dateTimeRenderOption(string $dateTimeRenderOption)
* @method static GoogleSheets getService()
* @method static static setService(mixed $service)
* @method static Drive getDriveService()
* @method static static setDriveService(mixed $drive)
* @method static array spreadsheetList()
* @method static object spreadsheetProperties()
* @method static object sheetProperties()
* @method static string getSpreadsheetId()
* @method static void macro(string $name, object|callable $macro)
*
* @see \Revolution\Google\Sheets\Sheets
* @see \Revolution\Google\Sheets\SheetsClient
*/
class Sheets extends Facade
{
Expand Down
4 changes: 2 additions & 2 deletions src/Providers/SheetsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Revolution\Google\Sheets\Contracts\Factory;
use Revolution\Google\Sheets\Sheets;
use Revolution\Google\Sheets\SheetsClient;

class SheetsServiceProvider extends ServiceProvider implements DeferrableProvider
{
Expand All @@ -14,7 +14,7 @@ class SheetsServiceProvider extends ServiceProvider implements DeferrableProvide
*/
public function register(): void
{
$this->app->singleton(Factory::class, Sheets::class);
$this->app->singleton(Factory::class, SheetsClient::class);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Sheets.php → src/SheetsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use Revolution\Google\Client\Facades\Google;
use Revolution\Google\Sheets\Contracts\Factory;
use Revolution\Google\Sheets\Facades\Google;

class Sheets implements Factory
class SheetsClient implements Factory
{
use Concerns\SheetsValues;
use Concerns\SheetsDrive;
Expand All @@ -38,7 +38,7 @@ public function setService(mixed $service): static
public function getService(): GoogleSheets
{
if (is_null($this->service)) {
$this->service = Google::make(service: 'sheets');
$this->service = Google::make('sheets');
}

return $this->service;
Expand All @@ -51,14 +51,14 @@ public function setAccessToken(array|string $token): static
{
Google::getCache()->clear();

Google::setAccessToken(token: $token);
Google::setAccessToken($token);

if (isset($token['refresh_token']) && Google::isAccessTokenExpired()) {
Google::fetchAccessTokenWithRefreshToken();
}

return $this->setService(Google::make(service: 'sheets'))
->setDriveService(Google::make(service: 'drive'));
return $this->setService(Google::make('sheets'))
->setDriveService(Google::make('drive'));
}

/**
Expand Down
11 changes: 6 additions & 5 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace Revolution\Google\Sheets\Tests;
namespace Tests;

use Mockery as m;
use PHPUnit\Framework\Attributes\RequiresMethod;
use PulkitJalan\Google\Client as GoogleClient;
use Revolution\Google\Sheets\Exceptions\UnknownServiceException;
use Revolution\Google\Sheets\Facades\Google;
use Revolution\Google\Sheets\GoogleSheetClient;
use Revolution\Google\Client\Exceptions\UnknownServiceException;
use Revolution\Google\Client\Facades\Google;
use Revolution\Google\Client\GoogleSheetClient;

class ClientTest extends TestCase
{
Expand Down Expand Up @@ -79,12 +80,12 @@ public function testDefaultCredentials()
$this->assertTrue($client->isUsingApplicationDefaultCredentials());
}

#[RequiresMethod(GoogleClient::class, 'make')]
public function test_original_client()
{
$this->assertInstanceOf(GoogleSheetClient::class, Google::getFacadeRoot());
Google::clearResolvedInstances();

$this->app->singleton(GoogleClient::class, fn ($app) => new GoogleClient(config('google', [])));
$this->app->alias(GoogleClient::class, 'google-client');

$this->assertInstanceOf(GoogleClient::class, app('google-client'));
Expand Down
2 changes: 1 addition & 1 deletion tests/SheetsCollectionTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Revolution\Google\Sheets\Tests;
namespace Tests;

use Revolution\Google\Sheets\Facades\Sheets;

Expand Down
Loading

0 comments on commit a595c98

Please sign in to comment.