Skip to content

Commit 93d1ec8

Browse files
committed
Prepared Firestore files
1 parent 473b890 commit 93d1ec8

4 files changed

Lines changed: 272 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of Phpfastcache.
5+
*
6+
* @license MIT License (MIT)
7+
*
8+
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
9+
*
10+
* @author Georges.L (Geolim4) <contact@geolim4.com>
11+
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Phpfastcache\Drivers\Firestore;
17+
18+
use Phpfastcache\Config\ConfigurationOption;
19+
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
20+
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
21+
22+
/**
23+
* @see https://github.com/arangodb/arangodb-php/blob/devel/examples/init.php
24+
* @SuppressWarnings(PHPMD.TooManyFields)
25+
*/
26+
class Config extends ConfigurationOption
27+
{
28+
protected string $collection;
29+
30+
protected string $partitionKey = ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX;
31+
32+
/**
33+
* @return string
34+
*/
35+
public function getCollection(): string
36+
{
37+
return $this->collection;
38+
}
39+
40+
/**
41+
* @param string $collection
42+
* @return Config
43+
* @throws PhpfastcacheLogicException
44+
*/
45+
public function setCollection(string $collection): Config
46+
{
47+
$this->enforceLockedProperty(__FUNCTION__);
48+
$this->collection = $collection;
49+
return $this;
50+
}
51+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of Phpfastcache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10+
*
11+
* @author Georges.L (Geolim4) <contact@geolim4.com>
12+
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13+
*/
14+
declare(strict_types=1);
15+
16+
namespace Phpfastcache\Drivers\Firestore;
17+
18+
use Google\Cloud\Firestore\FirestoreClient as GoogleFirestoreClient;
19+
20+
use Phpfastcache\Cluster\AggregatablePoolInterface;
21+
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
22+
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
23+
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
24+
use Phpfastcache\Entities\DriverStatistic;
25+
26+
/**
27+
* Class Driver
28+
* @property Config $config
29+
* @property GoogleFirestoreClient $instance
30+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
31+
*/
32+
class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface
33+
{
34+
use TaggableCacheItemPoolTrait;
35+
36+
protected const TTL_FIELD_NAME = 't';
37+
38+
/**
39+
* @return bool
40+
*/
41+
public function driverCheck(): bool
42+
{
43+
return \class_exists(GoogleFirestoreClient::class) && \extension_loaded('grpc');
44+
}
45+
46+
/**
47+
* @return bool
48+
*/
49+
protected function driverConnect(): bool
50+
{
51+
/*
52+
$this->instance = new GoogleFirestoreClient([
53+
//'projectId' => $projectId,
54+
]);*/
55+
56+
/* if (!$this->hasCollection()) {
57+
$this->createCollection();
58+
}
59+
60+
if (!$this->hasTtlEnabled()) {
61+
$this->enableTtl();
62+
}*/
63+
64+
return true;
65+
}
66+
67+
/**
68+
* @param ExtendedCacheItemInterface $item
69+
* @return bool
70+
*/
71+
protected function driverWrite(ExtendedCacheItemInterface $item): bool
72+
{
73+
return true;
74+
}
75+
76+
/**
77+
* @param ExtendedCacheItemInterface $item
78+
* @return null|array
79+
* @throws \Exception
80+
*/
81+
protected function driverRead(ExtendedCacheItemInterface $item): ?array
82+
{
83+
return null;
84+
}
85+
86+
/**
87+
* @param ExtendedCacheItemInterface $item
88+
* @return bool
89+
*/
90+
protected function driverDelete(ExtendedCacheItemInterface $item): bool
91+
{
92+
return true;
93+
}
94+
95+
/**
96+
* @return bool
97+
*/
98+
protected function driverClear(): bool
99+
{
100+
return true;
101+
}
102+
103+
protected function hasCollection(): bool
104+
{
105+
return true;
106+
}
107+
108+
protected function createCollection() :void
109+
{
110+
}
111+
112+
protected function hasTtlEnabled(): bool
113+
{
114+
return true;
115+
}
116+
117+
protected function enableTtl(): void
118+
{
119+
}
120+
121+
public function getStats(): DriverStatistic
122+
{
123+
return new DriverStatistic();
124+
}
125+
126+
protected function encodeDocument(array $data): array
127+
{
128+
$data[self::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[self::DRIVER_DATA_WRAPPER_INDEX]);
129+
130+
return $data;
131+
}
132+
133+
protected function decodeDocument(array $data): array
134+
{
135+
$data[self::DRIVER_DATA_WRAPPER_INDEX] = $this->decode($data[self::DRIVER_DATA_WRAPPER_INDEX]);
136+
137+
return $data;
138+
}
139+
140+
public function getConfig(): Config
141+
{
142+
return $this->config;
143+
}
144+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of Phpfastcache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10+
*
11+
* @author Georges.L (Geolim4) <contact@geolim4.com>
12+
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13+
*/
14+
declare(strict_types=1);
15+
16+
namespace Phpfastcache\Drivers\Firestore;
17+
18+
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
19+
use Phpfastcache\Core\Item\TaggableCacheItemTrait;
20+
21+
class Item implements ExtendedCacheItemInterface
22+
{
23+
use TaggableCacheItemTrait;
24+
25+
protected function getDriverClass(): string
26+
{
27+
return Driver::class;
28+
}
29+
}

tests/Firestore.test.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of Phpfastcache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10+
*
11+
* @author Georges.L (Geolim4) <contact@geolim4.com>
12+
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13+
*/
14+
15+
use Phpfastcache\CacheManager;
16+
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
17+
use Phpfastcache\Drivers\Dynamodb\Config as DynamodbConfig;
18+
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException;
19+
use Phpfastcache\Tests\Helper\TestHelper;
20+
21+
chdir(__DIR__);
22+
require_once __DIR__ . '/../vendor/autoload.php';
23+
$testHelper = new TestHelper('Amazon Dynamodb driver');
24+
25+
$config = new DynamodbConfig();
26+
27+
try {
28+
$config->setItemDetailedDate(true);
29+
$config->setRegion('eu-west-3');
30+
$config->setEndpoint('dynamodb.eu-west-3.amazonaws.com');
31+
$config->setTable('phpfastcache');
32+
$cacheInstance = CacheManager::getInstance('Dynamodb', $config);
33+
34+
$cacheInstance->getEventManager()->onDynamodbCreateTable(static function(ExtendedCacheItemPoolInterface $pool, array $params) use ($testHelper){
35+
$testHelper->printDebugText(
36+
sprintf(
37+
'Table created with the following parameters: %s',
38+
json_encode($params, JSON_THROW_ON_ERROR)
39+
)
40+
);
41+
});
42+
43+
$testHelper->runCRUDTests($cacheInstance, false);
44+
} catch (PhpfastcacheDriverConnectException $e) {
45+
$testHelper->assertSkip('Dynamodb server unavailable: ' . $e->getMessage());
46+
$testHelper->terminateTest();
47+
}
48+
$testHelper->terminateTest();

0 commit comments

Comments
 (0)