projects
/
g-sheets-php.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
91c5fd4
)
Coding Style: tryng to follow PSR-12
main
author
Eduardo
<
[email protected]
>
Sun, 26 Nov 2023 15:04:26 +0000
(16:04 +0100)
committer
Eduardo
<
[email protected]
>
Sun, 26 Nov 2023 15:04:26 +0000
(16:04 +0100)
g-sheet-sync.php
patch
|
blob
|
history
index.php
patch
|
blob
|
history
diff --git
a/g-sheet-sync.php
b/g-sheet-sync.php
index c9f471eefb8e1155a08ff80340ee33f95cd234ec..c94504339d3533c6b244bc5b8c0b6456ae9c3929 100644
(file)
--- a/
g-sheet-sync.php
+++ b/
g-sheet-sync.php
@@
-10,12
+10,12
@@
require_once "vendor/autoload.php";
/**
* usefull: https://www.nidup.io/blog/manipulate-google-sheets-in-php-with-api
*/
/**
* usefull: https://www.nidup.io/blog/manipulate-google-sheets-in-php-with-api
*/
-class GSheetSync
{
-
+class GSheetSync
+{
private $service;
private $spreadsheetId;
private $service;
private $spreadsheetId;
- function __construct(string $credentialsPath, string $spreadsheetId)
+
public
function __construct(string $credentialsPath, string $spreadsheetId)
{
$client = new \Google_Client();
$client->setApplicationName('Google Sheets API');
{
$client = new \Google_Client();
$client->setApplicationName('Google Sheets API');
@@
-24,7
+24,7
@@
class GSheetSync {
$client->setAuthConfig($credentialsPath);
$client->setAuthConfig($credentialsPath);
- $this->service = new \Google\Service\Sheets($client);
+ $this->service
= new \Google\Service\Sheets($client);
$this->spreadsheetId = $spreadsheetId;
}
$this->spreadsheetId = $spreadsheetId;
}
@@
-32,10
+32,11
@@
class GSheetSync {
* @param string $range the name of the Sheet to get all the rows, extra args to pick. EX: 'Sheet1!A1:F10'
* @return string | false JSON or false if failure
*/
* @param string $range the name of the Sheet to get all the rows, extra args to pick. EX: 'Sheet1!A1:F10'
* @return string | false JSON or false if failure
*/
- function get(string $range): string | false {
+ public function get(string $range): string | false
+ {
// get all the rows of a sheet
$response = $this->service->spreadsheets_values->get($this->spreadsheetId, $range);
// get all the rows of a sheet
$response = $this->service->spreadsheets_values->get($this->spreadsheetId, $range);
- $rows = $response->getValues();
+ $rows
= $response->getValues();
// Remove the first one that contains headers
$headers = array_shift($rows);
// Remove the first one that contains headers
$headers = array_shift($rows);
@@
-43,8
+44,9
@@
class GSheetSync {
$array = [];
foreach ($rows as $row) {
# fill blank spaces at end if needed
$array = [];
foreach ($rows as $row) {
# fill blank spaces at end if needed
- while (count($headers) > count($row))
+ while (count($headers) > count($row))
{
$row[] = null;
$row[] = null;
+ }
$array[] = array_combine($headers, $row);
}
$array[] = array_combine($headers, $row);
}
@@
-57,7
+59,8
@@
class GSheetSync {
* @param string $sheet sheet of the document, where to append the new row
* @param array $arr columns to append at the end of the document
*/
* @param string $sheet sheet of the document, where to append the new row
* @param array $arr columns to append at the end of the document
*/
- function put(string $sheet, array $row): void {
+ public function put(string $sheet, array $row): void
+ {
$valueRange = new \Google\Service\Sheets\ValueRange();
$valueRange->setValues([$row]);
$valueRange = new \Google\Service\Sheets\ValueRange();
$valueRange->setValues([$row]);
diff --git
a/index.php
b/index.php
index 222d510f074b57135a9a2da5fd67678ac3942df3..2a1759921b0ff07a9e79f55dd941a25710f62e58 100644
(file)
--- a/
index.php
+++ b/
index.php
@@
-27,7
+27,7
@@
switch ($_SERVER['REQUEST_METHOD']) {
$filename = generateFilename($_ENV["sheetId"], $sheet);
if (fileExpired($filename)) {
$sheetSync = new GSheetSync($_ENV["credentialsPath"], $_ENV["sheetId"]);
$filename = generateFilename($_ENV["sheetId"], $sheet);
if (fileExpired($filename)) {
$sheetSync = new GSheetSync($_ENV["credentialsPath"], $_ENV["sheetId"]);
- $res = $sheetSync->get($sheet);
+ $res
= $sheetSync->get($sheet);
$etag = md5($res);
header('ETag: ' . $etag);
$etag = md5($res);
header('ETag: ' . $etag);
@@
-48,7
+48,7
@@
switch ($_SERVER['REQUEST_METHOD']) {
$filename = generateFilename($_ENV["sheetId"], $sheet);
if (fileExpired($filename)) {
$sheetSync = new GSheetSync($_ENV["credentialsPath"], $_ENV["sheetId"]);
$filename = generateFilename($_ENV["sheetId"], $sheet);
if (fileExpired($filename)) {
$sheetSync = new GSheetSync($_ENV["credentialsPath"], $_ENV["sheetId"]);
- $res = $sheetSync->get($sheet);
+ $res
= $sheetSync->get($sheet);
// dont make the user wait, print the json
$etag = md5($res);
// dont make the user wait, print the json
$etag = md5($res);
@@
-99,10
+99,14
@@
function fileExpired(string $filename): bool
$filetime = filemtime($filename);
// if file does not exist counts as expired
$filetime = filemtime($filename);
// if file does not exist counts as expired
- if (!$filetime) return true;
+ if (!$filetime) {
+ return true;
+ }
// if file older than TTL minutes IS expired
// if file older than TTL minutes IS expired
- if ($filetime < time() - TTL) return true;
+ if ($filetime < time() - TTL) {
+ return true;
+ }
// else file is NOT expired
return false;
// else file is NOT expired
return false;
@@
-118,7
+122,6
@@
function hasFileContentChanged(string $oldFilePath, string $newFileString): bool
return !(md5_file($oldFilePath) == md5($newFileString));
}
return !(md5_file($oldFilePath) == md5($newFileString));
}
-
/**
* Sends an 304 Not Modified header if the ETag matches the sent one
*/
/**
* Sends an 304 Not Modified header if the ETag matches the sent one
*/
@@
-131,8
+134,9
@@
function sendNotModifiedIfSameETag(string $etag, bool $die = true)
if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
http_response_code(304);
header('HTTP/1.1 304 Not Modified');
if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
http_response_code(304);
header('HTTP/1.1 304 Not Modified');
- if ($die)
+ if ($die)
{
exit();
exit();
+ }
}
}
}
}
}
}