Skip to content

Commit c119191

Browse files
committed
Re-arranged some code
1 parent 75495f4 commit c119191

File tree

7 files changed

+175
-78
lines changed

7 files changed

+175
-78
lines changed

docs/CREDITS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ __Phpfastcache Contributors:__ https://github.com/PHPSocialNetwork/phpfastcac
2727

2828
#### Hall of fame (Special thanks):
2929

30-
- __Jetbrains__ * for providing us a free IDE licence
30+
- __Jetbrains__ * for providing us a free Phpstorm IDE licence for years
3131
- __Scrutinizer-CI__ * for providing us a free Security Analysis
3232
- __VersionEye__ * For being an awesome tool that helped us to watch version dependencies. You will be missed, thank you very much Robert.
3333
- __Sabine van Lommen__ * from __Zend.com__ for providing us a free developer licence of Zend Server

lib/Phpfastcache/Drivers/Arangodb/Driver.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@ protected function driverConnect(): bool
116116
return $this->collectionHandler->has($this->getConfig()->getCollection());
117117
}
118118

119+
/**
120+
* @param ExtendedCacheItemInterface $item
121+
* @return null|array
122+
* @throws PhpfastcacheDriverException
123+
* @throws \Exception
124+
*/
125+
protected function driverRead(ExtendedCacheItemInterface $item): ?array
126+
{
127+
try {
128+
$document = $this->documentHandler->get($this->getConfig()->getCollection(), $item->getEncodedKey());
129+
} catch (ArangoServerException $e) {
130+
if ($e->getCode() === 404) {
131+
return null;
132+
}
133+
throw new PhpfastcacheDriverException(
134+
'Got unexpeced error from Arangodb: ' . $e->getMessage()
135+
);
136+
}
137+
138+
return $this->decode($document);
139+
}
140+
119141
/**
120142
* @param ExtendedCacheItemInterface $item
121143
* @return bool
@@ -147,28 +169,6 @@ protected function driverWrite(ExtendedCacheItemInterface $item): bool
147169
return $this->documentHandler->insert($this->getConfig()->getCollection(), $document, $options) !== null;
148170
}
149171

150-
/**
151-
* @param ExtendedCacheItemInterface $item
152-
* @return null|array
153-
* @throws PhpfastcacheDriverException
154-
* @throws \Exception
155-
*/
156-
protected function driverRead(ExtendedCacheItemInterface $item): ?array
157-
{
158-
try {
159-
$document = $this->documentHandler->get($this->getConfig()->getCollection(), $item->getEncodedKey());
160-
} catch (ArangoServerException $e) {
161-
if ($e->getCode() === 404) {
162-
return null;
163-
}
164-
throw new PhpfastcacheDriverException(
165-
'Got unexpeced error from Arangodb: ' . $e->getMessage()
166-
);
167-
}
168-
169-
return $this->decode($document);
170-
}
171-
172172
/**
173173
* @param ExtendedCacheItemInterface $item
174174
* @return bool

lib/Phpfastcache/Drivers/Cassandra/Driver.php

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,45 +48,6 @@ public function driverCheck(): bool
4848
return extension_loaded('Cassandra') && class_exists(Cassandra::class);
4949
}
5050

51-
/**
52-
* @return string
53-
*/
54-
public function getHelp(): string
55-
{
56-
return <<<HELP
57-
<p>
58-
To install the php Cassandra extension via Pecl:
59-
<code>sudo pecl install cassandra</code>
60-
More information on: https://github.com/datastax/php-driver
61-
Please note that this repository only provide php stubs and C/C++ sources, it does NOT provide php client.
62-
</p>
63-
HELP;
64-
}
65-
66-
/**
67-
* @return DriverStatistic
68-
* @throws Exception
69-
*/
70-
public function getStats(): DriverStatistic
71-
{
72-
$result = $this->instance->execute(
73-
new Cassandra\SimpleStatement(
74-
sprintf(
75-
'SELECT SUM(cache_length) as cache_size FROM %s.%s',
76-
self::CASSANDRA_KEY_SPACE,
77-
self::CASSANDRA_TABLE
78-
)
79-
),
80-
null
81-
);
82-
83-
return (new DriverStatistic())
84-
->setSize($result->first()['cache_size'])
85-
->setRawData([])
86-
->setData(implode(', ', array_keys($this->itemInstances)))
87-
->setInfo('The cache size represents only the cache data itself without counting data structures associated to the cache entries.');
88-
}
89-
9051
/**
9152
* @return bool
9253
* @throws PhpfastcacheLogicException
@@ -305,6 +266,45 @@ protected function driverClear(): bool
305266
}
306267
}
307268

269+
/**
270+
* @return string
271+
*/
272+
public function getHelp(): string
273+
{
274+
return <<<HELP
275+
<p>
276+
To install the php Cassandra extension via Pecl:
277+
<code>sudo pecl install cassandra</code>
278+
More information on: https://github.com/datastax/php-driver
279+
Please note that this repository only provide php stubs and C/C++ sources, it does NOT provide php client.
280+
</p>
281+
HELP;
282+
}
283+
284+
/**
285+
* @return DriverStatistic
286+
* @throws Exception
287+
*/
288+
public function getStats(): DriverStatistic
289+
{
290+
$result = $this->instance->execute(
291+
new Cassandra\SimpleStatement(
292+
sprintf(
293+
'SELECT SUM(cache_length) as cache_size FROM %s.%s',
294+
self::CASSANDRA_KEY_SPACE,
295+
self::CASSANDRA_TABLE
296+
)
297+
),
298+
null
299+
);
300+
301+
return (new DriverStatistic())
302+
->setSize($result->first()['cache_size'])
303+
->setRawData([])
304+
->setData(implode(', ', array_keys($this->itemInstances)))
305+
->setInfo('The cache size represents only the cache data itself without counting data structures associated to the cache entries.');
306+
}
307+
308308
public function getConfig(): Config
309309
{
310310
return $this->config;

lib/Phpfastcache/Drivers/Dynamodb/Driver.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
2727
use Phpfastcache\Entities\DriverStatistic;
2828
use Phpfastcache\Event\EventReferenceParameter;
29+
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException;
2930
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
31+
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
3032
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
3133
use Psr\Http\Message\UriInterface;
3234

@@ -56,10 +58,24 @@ public function driverCheck(): bool
5658

5759
/**
5860
* @return bool
61+
* @throws PhpfastcacheDriverConnectException
5962
* @throws PhpfastcacheDriverException
63+
* @throws PhpfastcacheLogicException
64+
* @throws PhpfastcacheInvalidArgumentException
6065
*/
6166
protected function driverConnect(): bool
6267
{
68+
$wsAccessKey = $this->getConfig()->getSuperGlobalAccessor()('SERVER', 'AWS_ACCESS_KEY_ID');
69+
$awsSecretKey = $this->getConfig()->getSuperGlobalAccessor()('SERVER', 'AWS_SECRET_ACCESS_KEY');
70+
71+
if (empty($wsAccessKey)) {
72+
throw new PhpfastcacheDriverConnectException('The environment configuration AWS_ACCESS_KEY_ID must be set');
73+
}
74+
75+
if (empty($awsSecretKey)) {
76+
throw new PhpfastcacheDriverConnectException('The environment configuration AWS_SECRET_ACCESS_KEY must be set');
77+
}
78+
6379
$this->awsSdk = new AwsSdk([
6480
'endpoint' => $this->getConfig()->getEndpoint(),
6581
'region' => $this->getConfig()->getRegion(),

lib/Phpfastcache/Drivers/Firestore/Config.php

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,20 @@
2525
*/
2626
class Config extends ConfigurationOption
2727
{
28+
protected ?string $googleCloudProject = null;
29+
30+
protected ?string $googleApplicationCredential = null;
31+
32+
protected bool $allowEnvCredentialOverride = false;
33+
2834
protected string $collection;
2935

30-
protected string $partitionKey = ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX;
36+
public function __construct(array $parameters = [])
37+
{
38+
parent::__construct($parameters);
39+
$this->googleCloudProject = $this->getSuperGlobalAccessor()('SERVER', 'GOOGLE_CLOUD_PROJECT');
40+
$this->googleApplicationCredential = $this->getSuperGlobalAccessor()('SERVER', 'GOOGLE_APPLICATION_CREDENTIALS');
41+
}
3142

3243
/**
3344
* @return string
@@ -48,4 +59,74 @@ public function setCollection(string $collection): Config
4859
$this->collection = $collection;
4960
return $this;
5061
}
62+
63+
/**
64+
* @return string|null
65+
*/
66+
public function getGoogleCloudProject(): ?string
67+
{
68+
return $this->googleCloudProject;
69+
}
70+
71+
/**
72+
* @param string|null $googleCloudProject
73+
* @return Config
74+
* @throws PhpfastcacheLogicException
75+
*/
76+
public function setGoogleCloudProject(?string $googleCloudProject): Config
77+
{
78+
$this->enforceLockedProperty(__FUNCTION__);
79+
if ($googleCloudProject !== null) {
80+
if (!$this->isAllowEnvCredentialOverride()) {
81+
throw new PhpfastcacheLogicException('You are not allowed to override GCP environment variables.');
82+
}
83+
\putenv("GOOGLE_CLOUD_PROJECT=$googleCloudProject");
84+
$this->googleApplicationCredential = $googleCloudProject;
85+
}
86+
return $this;
87+
}
88+
89+
/**
90+
* @return string|null
91+
*/
92+
public function getGoogleApplicationCredential(): ?string
93+
{
94+
return $this->googleApplicationCredential;
95+
}
96+
97+
/**
98+
* @param string|null $googleApplicationCredential
99+
* @return Config
100+
* @throws PhpfastcacheLogicException
101+
*/
102+
public function setGoogleApplicationCredential(?string $googleApplicationCredential): Config
103+
{
104+
$this->enforceLockedProperty(__FUNCTION__);
105+
if ($googleApplicationCredential !== null) {
106+
if (!$this->isAllowEnvCredentialOverride()) {
107+
throw new PhpfastcacheLogicException('You are not allowed to override GCP environment variables.');
108+
}
109+
\putenv("GOOGLE_APPLICATION_CREDENTIALS=$googleApplicationCredential");
110+
$this->googleApplicationCredential = $googleApplicationCredential;
111+
}
112+
return $this;
113+
}
114+
115+
/**
116+
* @return bool
117+
*/
118+
public function isAllowEnvCredentialOverride(): bool
119+
{
120+
return $this->allowEnvCredentialOverride;
121+
}
122+
123+
/**
124+
* @param bool $allowEnvCredentialOverride
125+
* @return Config
126+
*/
127+
public function setAllowEnvCredentialOverride(bool $allowEnvCredentialOverride): Config
128+
{
129+
$this->allowEnvCredentialOverride = $allowEnvCredentialOverride;
130+
return $this;
131+
}
51132
}

lib/Phpfastcache/Drivers/Firestore/Driver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected function driverConnect(): bool
6060
}
6161

6262
if (empty($gacPath) || !\is_readable($gacPath)) {
63-
throw new PhpfastcacheDriverConnectException('The environment configuration GOOGLE_APPLICATION_CREDENTIALS must be set and the file must be readable.');
63+
throw new PhpfastcacheDriverConnectException('The environment configuration GOOGLE_APPLICATION_CREDENTIALS must be set and the JSON file must be readable.');
6464
}
6565

6666
$this->instance = new GoogleFirestoreClient();

lib/Phpfastcache/Drivers/Memstatic/Driver.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,6 @@ public function driverCheck(): bool
4444
return true;
4545
}
4646

47-
/**
48-
* @return DriverStatistic
49-
*/
50-
public function getStats(): DriverStatistic
51-
{
52-
$stat = new DriverStatistic();
53-
$stat->setInfo('[Memstatic] A memory static driver')
54-
->setSize(mb_strlen(serialize($this->staticStack)))
55-
->setData(implode(', ', array_keys($this->itemInstances)))
56-
->setRawData($this->staticStack);
57-
58-
return $stat;
59-
}
60-
6147
/**
6248
* @return bool
6349
*/
@@ -116,6 +102,20 @@ protected function driverClear(): bool
116102
return true;
117103
}
118104

105+
/**
106+
* @return DriverStatistic
107+
*/
108+
public function getStats(): DriverStatistic
109+
{
110+
$stat = new DriverStatistic();
111+
$stat->setInfo('[Memstatic] A memory static driver')
112+
->setSize(mb_strlen(serialize($this->staticStack)))
113+
->setData(implode(', ', array_keys($this->itemInstances)))
114+
->setRawData($this->staticStack);
115+
116+
return $stat;
117+
}
118+
119119
public function getConfig(): Config
120120
{
121121
return $this->config;

0 commit comments

Comments
 (0)