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
134 changes: 5 additions & 129 deletions src/FMDataAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

namespace INTERMediator\FileMakerServer\RESTAPI;

use Exception;
use INTERMediator\FileMakerServer\RESTAPI\PersistentSession\ApcuSessionCache;
use INTERMediator\FileMakerServer\RESTAPI\PersistentSession\PersistentSession;
use INTERMediator\FileMakerServer\RESTAPI\PersistentSession\SessionCacheInterface;
use INTERMediator\FileMakerServer\RESTAPI\Supporting\CommunicationProvider;
use INTERMediator\FileMakerServer\RESTAPI\Supporting\FileMakerLayout;
use INTERMediator\FileMakerServer\RESTAPI\Supporting\FileMakerRelation;
use RuntimeException;
use INTERMediator\FileMakerServer\RESTAPI\Supporting\CommunicationProvider;
use Exception;

/**
* Class FMDataAPI is the wrapper of The REST API in Claris FileMaker Server and FileMaker Cloud for AWS.
Expand Down Expand Up @@ -42,12 +38,6 @@
*/
private CommunicationProvider|null $provider;

/**
* @var null|PersistentSession Keeping the PersistentSession object.
* @ignore
*/
private PersistentSession|null $persistentSession = null;

/**
* FMDataAPI constructor. If you want to activate OAuth authentication, $user and $password are set as
* oAuthRequestId and oAuthIdentifier. Moreover, call useOAuth method before accessing layouts.
Expand All @@ -68,8 +58,6 @@
* Ex. [{"database"=>"<databaseName>", "username"=>"<username>", "password"=>"<password>"}].
* If you use OAuth, "oAuthRequestId" and "oAuthIdentifier" keys have to be specified.
* @param boolean $isUnitTest If it's set to true, the communication provider just works locally.
* @param SessionCacheInterface|null $sessionCache Cache backend for persistent sessions. If omitted, APCu will be
* used if available, otherwise, no caching will be used.
*/
public function __construct(string $solution,
string $user,
Expand All @@ -78,25 +66,16 @@
int|null $port = null,
string|null $protocol = null,
array|null $fmDataSource = null,
bool $isUnitTest = false,
SessionCacheInterface|null $sessionCache = null)
bool $isUnitTest = false)
{
if (is_null($password)) {
$password = "password"; // For testing purpose.
}
if (!$isUnitTest) {
$this->provider = new Supporting\CommunicationProvider($solution, $user, $password, $host, $port, $protocol, $fmDataSource);

Check failure on line 75 in src/FMDataAPI.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #5 $port of class INTERMediator\FileMakerServer\RESTAPI\Supporting\CommunicationProvider constructor expects string|null, int|null given.

Check failure on line 75 in src/FMDataAPI.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #5 $port of class INTERMediator\FileMakerServer\RESTAPI\Supporting\CommunicationProvider constructor expects string|null, int|null given.
} else {
$this->provider = new Supporting\TestProvider($solution, $user, $password, $host, $port, $protocol, $fmDataSource);

Check failure on line 77 in src/FMDataAPI.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #5 $port of class INTERMediator\FileMakerServer\RESTAPI\Supporting\TestProvider constructor expects string|null, int|null given.

Check failure on line 77 in src/FMDataAPI.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #5 $port of class INTERMediator\FileMakerServer\RESTAPI\Supporting\TestProvider constructor expects string|null, int|null given.
}

if ($sessionCache !== null) {
$this->persistentSession = new PersistentSession($sessionCache, $solution, $user);
$this->provider->keepPersistentSession = true;
} elseif (function_exists('apcu_fetch')) {
$this->persistentSession = new PersistentSession(new ApcuSessionCache(), $solution, $user);
$this->provider->keepPersistentSession = true;
}
}

/**
Expand Down Expand Up @@ -210,9 +189,9 @@

/**
* Set session token
* @param string|null $value The session token.
* @param string $value The session token.
*/
public function setSessionToken(string|null $value): void
public function setSessionToken(string $value): void
{
$this->provider->accessToken = $value;
}
Expand Down Expand Up @@ -314,109 +293,6 @@
$this->provider->logout();
}

/**
* Begin a persistent session which is a serial calling of any database operations, and keep the session token.
*
* This persistent session persists between multiple PHP requests.
* @throws Exception
*/
public function beginPersistentSession(): void
{
if ($this->persistentSession === null) {
throw new RuntimeException(
"Persistent sessions require a cache backend. Install ext-apcu or provide a SessionCacheInterface implementation."
);
}

try {
if (!$this->persistentSession->applyCachedSessionToken($this)) {
if ($this->provider->login()) {
$this->persistentSession->cacheCurrentSessionToken($this);
}
}
$this->provider->keepPersistentSession = true;
} catch (Exception $e) {
$this->persistentSession->clearCachedSessionToken();
$this->setSessionToken(null);
$this->provider->keepPersistentSession = false;
throw $e;
}
}

/**
* End a persistent session which is a serial calling of any database operations, and logout.
* @throws Exception
*/
public function closePersistentSession(): void
{
if ($this->persistentSession !== null) {
$this->persistentSession->clearCachedSessionToken();
}
$this->provider->keepPersistentSession = false;
$this->provider->logout();
}

/**
* Execute a callback with this FMDataAPI instance.
*
* When persistent sessions are enabled, this method uses the cached session when available. If the session is no
* longer valid, it creates and caches a new one, then retries the callback once.
*
* When persistent sessions are not enabled, the callback is invoked immediately with this instance.
*
* Example:
* <code>
* $client = new FMDataAPI('MySolution', 'MyUser', 'MyPassword');
* $result = $client->execute(
* fn (FMDataAPI $fm) => $fm->layout('MyLayout')->query(
* // do stuff
* )
* );
* </code>
*
* @template TReturn
* @param callable(FMDataAPI): TReturn $fn
* @return TReturn
* @throws Exception Any exception thrown by the callback or the underlying provider.
*/
public function execute(callable $fn)
{
if (!$this->provider->keepPersistentSession || $this->persistentSession === null) {
return $fn($this);
}

if ($this->provider->throwExceptionInError) {
try {
return $fn($this);
} catch (Exception $e) {
if ($this->errorCode() == 952) {
$this->refreshSession();
return $fn($this);
}
throw $e;
}
}

$result = $fn($this);
if ($this->errorCode() == 952) {
$this->refreshSession();
return $fn($this);
}
return $result;
}

/**
* Clear the current persistent session state, log in again, and cache the refreshed session token.
* @throws Exception
*/
private function refreshSession(): void
{
$this->persistentSession->clearCachedSessionToken();
$this->setSessionToken(null);
$this->provider->login();
$this->persistentSession->cacheCurrentSessionToken($this);
}

/**
* Set the value to the global field.
* @param array $fields Associated array contains the global field names (Field names must be Fully Qualified) and its values.
Expand Down
31 changes: 0 additions & 31 deletions src/PersistentSession/ApcuSessionCache.php

This file was deleted.

120 changes: 0 additions & 120 deletions src/PersistentSession/PersistentSession.php

This file was deleted.

26 changes: 0 additions & 26 deletions src/PersistentSession/SessionCacheInterface.php

This file was deleted.

10 changes: 1 addition & 9 deletions src/Supporting/CommunicationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,6 @@
* @ignore
*/
public bool $keepAuth = false;
/**
* @var bool Use persistent session for communication with FileMaker server. A persistent session is when a
* communication with FileMaker server is kept open for multiple PHP requests, making all PHP requests share the
* same session token.
* @ignore
*/
public bool $keepPersistentSession = false;

/**
* @var bool
Expand Down Expand Up @@ -521,7 +514,6 @@
return true;
}
} catch (Exception $e) {
$this->storeToProperties();
$this->accessToken = null;
throw $e;
}
Expand All @@ -536,7 +528,7 @@
*/
public function logout(): void
{
if ($this->keepAuth || $this->keepPersistentSession) {
if ($this->keepAuth) {
return;
}
$params = ["sessions" => $this->accessToken];
Expand Down Expand Up @@ -628,11 +620,11 @@
$request = $this->justifyRequest($request);
}
$ch = $this->_createCurlHandle($url);
curl_setopt($ch, CURLOPT_VERBOSE, 0);

Check failure on line 623 in src/Supporting/CommunicationProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #3 $value of function curl_setopt expects bool, int given.

Check failure on line 623 in src/Supporting/CommunicationProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #3 $value of function curl_setopt expects bool, int given.
curl_setopt($ch, CURLOPT_HEADER, 1);

Check failure on line 624 in src/Supporting/CommunicationProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #3 $value of function curl_setopt expects bool, int given.

Check failure on line 624 in src/Supporting/CommunicationProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #3 $value of function curl_setopt expects bool, int given.
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
if ($methodLower == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);

Check failure on line 627 in src/Supporting/CommunicationProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #3 $value of function curl_setopt expects bool, int given.

Check failure on line 627 in src/Supporting/CommunicationProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #3 $value of function curl_setopt expects bool, int given.
} elseif (in_array($methodLower, ['put', 'patch', 'delete', 'get'], true)) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($methodLower));
}
Expand Down Expand Up @@ -904,7 +896,7 @@
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_DEFAULT);
if ($this->isCertValidating) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);

Check failure on line 899 in src/Supporting/CommunicationProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #3 $value of function curl_setopt expects bool, int given.

Check failure on line 899 in src/Supporting/CommunicationProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #3 $value of function curl_setopt expects bool, int given.
/* Use the OS native certificate authorities, if possible.
This fixes SSL validation errors if `php.ini` doesn't have [curl] `curl.cainfo`,
set properly of if this PEM file isn't up to date.
Expand All @@ -915,7 +907,7 @@
}
} else {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

Check failure on line 910 in src/Supporting/CommunicationProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #3 $value of function curl_setopt expects bool, int given.

Check failure on line 910 in src/Supporting/CommunicationProvider.php

View workflow job for this annotation

GitHub Actions / Run PHPStan (5)

Parameter #3 $value of function curl_setopt expects bool, int given.
}
if (!is_null($this->timeout)) {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
Expand Down
Loading