diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..24f9470 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,22 @@ +/tests export-ignore +/docs/examples export-ignore +/bin export-ignore +/.github export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.gitmodules export-ignore +.travis.yml export-ignore +.scrutinizer.yml export-ignore +.codeclimate.yml export-ignore +CONTRIBUTING.md export-ignore +CODE_OF_CONDUCT.md export-ignore +CODING_GUIDELINE.md export-ignore +phpunit.xml.dist export-ignore +phpcs.xml.dist export-ignore +composer.lock export-ignore +quality.bat export-ignore +phpstan.neon export-ignore +phpstan_lite.neon export-ignore +phpcs.xml export-ignore +phpmd.xml export-ignore +CNAME export-ignore diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..2706135 --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,57 @@ +name: Dynamodb extension tests + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +permissions: + contents: read + +jobs: + run: + environment: github-ci + runs-on: ${{ matrix.operating-system }} + timeout-minutes: 60 + strategy: + matrix: + operating-system: [ubuntu-latest] + php-versions: ['8.0', '8.1', '8.2', '8.3'] + mongodb-version: ['6.0'] + + name: PHP ${{ matrix.php-versions }} quality/tests on ${{ matrix.operating-system }} + env: + key: cache-v1 + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v3 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + - name: Run quality tools + run: composer run-script quality + + - name: Run tests + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + run: composer run-script tests diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e8d8fb2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +## 9.2.0 +##### 10 january 2024 +- __Driver Core__ + - Driver is maintained by @Geolim4 + - `Xouchdb` is now an extension separated from the main Phpfastcache repository. diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..3395a8a --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,46 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at team@phpfastcache.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..1ccf0a3 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,38 @@ +Contributing to PhpFastCache +======================== + +Please note that this project is released with a +[Contributor Code of Conduct](https://www.contributor-covenant.org/version/1/4/code-of-conduct/). +By participating in this project you agree to abide by its terms. + +Reporting Issues +---------------- + +When reporting issues, please try to be as descriptive as possible, and include +as much relevant information as you can. A step-by-step guide on how to +reproduce the issue will greatly increase the chances of your issue being +resolved in a timely manner. + +⚠️ Support for this extension must be posted to the main [Phpfastcache repository](https://github.com/PHPSocialNetwork/phpfastcache/issues). + +Contributing policy +------------------- + +Our contributing policy is described in our [Coding Guideline (Phpfastcache Repository)](https://github.com/PHPSocialNetwork/phpfastcache/blob/master/CODING_GUIDELINE.md) + +Developer notes +------------------- +If you want to contribute to the repository you will need to install/configure some things first. + +To run tests follow the steps: +1) Run `composer install` *(Do not ignore platform reqs)* +2) Run `./vendor/bin/phpcs lib/ --report=summary` +3) Run `./vendor/bin/phpmd lib/ ansi phpmd.xml` +4) Run `./vendor/bin/phpstan analyse lib/ -c phpstan_lite.neon 2>&1` + +*If you are on Windows environment simply run the file `quality.bat` located at the root of the project to run the step 2, 3 and 4 in once.* + +The last command will run all the unit tests of the project. +If an error appears, fix it then you can submit your pull request. + +⚠️ **All tests and quality tools MUST pass or the merge request will be automatically closed.** diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..6b5101b --- /dev/null +++ b/LICENCE @@ -0,0 +1,20 @@ +Copyright (c) 2023 Phpfastcache & its Extensions + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +Software), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ef93b54 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +## Contributing [![PHP Tests](https://github.com/PHPSocialNetwork/mongodb-extension/actions/workflows/php.yml/badge.svg)](https://github.com/PHPSocialNetwork/mongodb-extension/actions/workflows/php.yml) +Merge requests are welcome but will require the tests plus the quality tools to pass: + +_(Commands must be run from the repository root)_ +### PHPCS, PHPMD, PHPSTAN (Level 6), unit tests: + +```bash +composer run-script quality +composer run-script tests + +# In case you want to fix the code style automatically: +composer run-script phpcbf +``` + +## Support & Security + +Support for this extension must be posted to the main [Phpfastcache repository](https://github.com/PHPSocialNetwork/phpfastcache/issues). + +## Composer installation: + +```php +composer install phpfastcache/mongodb-extension +``` + +#### ⚠️ This extension requires: +1️ The composer `mongodb/mongodb` library `1.10` at least. + +## Events +This driver is currently not emitting [customs events](https://github.com/PHPSocialNetwork/phpfastcache/blob/master/docs/EVENTS.md). diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..1481162 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,15 @@ +# Security Policy +If you discover any vulnerability please be aware of the following table of supported versions below. +Then feel free to contact me at the email address provided in the bottom of that page. + +## Supported Versions + +Check the details on the [Wiki](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV4%CB%96%5D-Global-support-timeline). + +As per SEMVER policy, this extension will follow the Phpfastcache *MAJOR and MINOR* version but will have its own *PATCH* versions. + +## Reporting a Vulnerability +If you discover any security vulnerability contact me at contact#at#geolim4.com with a subject formatted like that:\ +`[PHPFASTCACHE][VULNERABILITY] Your mail subject goes here` + +Thanks in advance for taking the time to report me that in private. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6af94e9 --- /dev/null +++ b/composer.json @@ -0,0 +1,61 @@ +{ + "name": "phpfastcache/dynamodb-extension", + "type" : "phpfastcache-extension", + "description": "Phpfastcache Dynamodb extension", + "keywords": ["cache", "phpfastcache", "dynamodb"], + "homepage": "https://github.com/PHPSocialNetwork/dynamodb-extension", + "license": "MIT", + "readme": "README.md", + "minimum-stability": "beta", + "authors": [ + { + "name": "Georges.L", + "email": "contact@geolim4.com", + "homepage": "https://github.com/Geolim4", + "role": "Project Manager" + } + ], + "require": { + "php": ">=8.0", + "phpfastcache/phpfastcache": "^9.2", + "aws/aws-sdk-php": "~3.0" + }, + "require-dev": { + "phpmd/phpmd": "@stable", + "squizlabs/php_codesniffer": "@stable", + "phpstan/phpstan": "^1.5", + "jetbrains/phpstorm-stubs": "dev-master", + "phpfastcache/phpfastcache-devtools": "^9.2" + }, + "autoload": { + "psr-4": { + "Phpfastcache\\Extensions\\": "lib/Phpfastcache/Extensions" + }, + "files": [ + "lib/ExtensionRegister.php" + ] + }, + "scripts": { + "phpcs": "vendor/bin/phpcs lib/ --report=summary", + "phpcbf": "vendor/bin/phpcbf lib/ --report=summary", + "phpmd": "vendor/bin/phpmd lib/ ansi phpmd.xml", + "phpstan": "vendor/bin/phpstan analyse lib/ -l 6 -c phpstan.neon", + "quality": ["@phpcs", "@phpmd", "@phpstan"], + "tests": [ + "php tests/Dynamodb.test.php" + ] + }, + "support": { + "issues": "https://github.com/PHPSocialNetwork/phpfastcache/issues", + "wiki": "https://github.com/PHPSocialNetwork/phpfastcache/wiki", + "docs": "https://github.com/PHPSocialNetwork/phpfastcache/wiki", + "source": "https://github.com/PHPSocialNetwork/phpfastcache", + "security": "https://github.com/PHPSocialNetwork/phpfastcache/blob/master/SECURITY.md" + }, + "funding": [ + { + "type": "patreon", + "url": "https://www.patreon.com/geolim4" + } + ] +} diff --git a/lib/ExtensionRegister.php b/lib/ExtensionRegister.php new file mode 100644 index 0000000..18cb26c --- /dev/null +++ b/lib/ExtensionRegister.php @@ -0,0 +1,15 @@ + + * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors + */ + +declare(strict_types=1); + +namespace Phpfastcache\Extensions\Drivers\Dynamodb; + +use Phpfastcache\Config\ConfigurationOption; +use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; +use Phpfastcache\Exceptions\PhpfastcacheLogicException; + +/** + * @SuppressWarnings(PHPMD.TooManyFields) + */ +class Config extends ConfigurationOption +{ + protected ?string $awsAccessKeyId = null; + protected ?string $awsSecretAccessKey = null; + protected bool $allowEnvCredentialOverride = false; + protected ?string $endpoint = null; +// List of endpoints here: https://docs.aws.amazon.com/general/latest/gr/ddb.html + + protected string $region; + protected string $table; + protected bool $debugEnabled = false; + protected string $version = 'latest'; + protected string $partitionKey = ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX; + public function __construct(array $parameters = []) + { + parent::__construct($parameters); + $this->awsAccessKeyId = $this->getSuperGlobalAccessor()('SERVER', 'AWS_ACCESS_KEY_ID'); + $this->awsSecretAccessKey = $this->getSuperGlobalAccessor()('SERVER', 'AWS_SECRET_ACCESS_KEY'); + } + + /** + * @return string|null + */ + public function getAwsAccessKeyId(): ?string + { + return $this->awsAccessKeyId; + } + + /** + * @param string|null $awsAccessKeyId + * @return Config + * @throws PhpfastcacheLogicException + */ + public function setAwsAccessKeyId(?string $awsAccessKeyId): Config + { + if ($awsAccessKeyId !== null) { + if (!getenv('AWS_ACCESS_KEY_ID')) { + if (!$this->isAllowEnvCredentialOverride()) { + throw new PhpfastcacheLogicException('You are not allowed to override AWS environment variables.'); + } + \putenv("AWS_ACCESS_KEY_ID=$awsAccessKeyId"); + } + + return $this->setProperty('awsAccessKeyId', getenv('AWS_ACCESS_KEY_ID')); + } + return $this; + } + + /** + * @return string|null + */ + public function getAwsSecretAccessKey(): ?string + { + return $this->awsSecretAccessKey; + } + + /** + * @param string|null $awsSecretAccessKey + * @return Config + * @throws PhpfastcacheLogicException + */ + public function setAwsSecretAccessKey(?string $awsSecretAccessKey): Config + { + if ($awsSecretAccessKey !== null) { + if (!getenv('AWS_SECRET_ACCESS_KEY')) { + if (!$this->isAllowEnvCredentialOverride()) { + throw new PhpfastcacheLogicException('You are not allowed to override AWS environment variables.'); + } + \putenv("AWS_SECRET_ACCESS_KEY=$awsSecretAccessKey"); + } + + return $this->setProperty('awsSecretAccessKey', getenv('AWS_SECRET_ACCESS_KEY')); + } + return $this; + } + + /** + * @return bool + */ + public function isAllowEnvCredentialOverride(): bool + { + return $this->allowEnvCredentialOverride; + } + + /** + * @param bool $allowEnvCredentialOverride + * @return Config + * @throws PhpfastcacheLogicException + */ + public function setAllowEnvCredentialOverride(bool $allowEnvCredentialOverride): Config + { + return $this->setProperty('allowEnvCredentialOverride', $allowEnvCredentialOverride); + } + + /** + * @return ?string + */ + public function getEndpoint(): ?string + { + return $this->endpoint; + } + + /** + * @param ?string $endpoint + * @return Config + * @throws PhpfastcacheLogicException + */ + public function setEndpoint(?string $endpoint): Config + { + if (!\str_starts_with($endpoint, 'https://') && \str_ends_with($endpoint, 'amazonaws.com')) { + $endpoint = 'https://' . $endpoint; + } + return $this->setProperty('endpoint', $endpoint); + } + + /** + * @return string + */ + public function getRegion(): string + { + return $this->region; + } + + /** + * @param string $region + * @return Config + * @throws PhpfastcacheLogicException + */ + public function setRegion(string $region): Config + { + return $this->setProperty('region', $region); + } + + /** + * @return string + */ + public function getTable(): string + { + return $this->table; + } + + /** + * @param string $table + * @return Config + * @throws PhpfastcacheLogicException + */ + public function setTable(string $table): Config + { + return $this->setProperty('table', $table); + } + + /** + * @return bool + */ + public function isDebugEnabled(): bool + { + return $this->debugEnabled; + } + + /** + * @param bool $debugEnabled + * @return Config + * @throws PhpfastcacheLogicException + */ + public function setDebugEnabled(bool $debugEnabled): Config + { + return $this->setProperty('debugEnabled', $debugEnabled); + } + + /** + * @return string + */ + public function getVersion(): string + { + return $this->version; + } + + /** + * @param string $version + * @return Config + * @throws PhpfastcacheLogicException + */ + public function setVersion(string $version): Config + { + return $this->setProperty('version', $version); + } + + /** + * @return string + */ + public function getPartitionKey(): string + { + return $this->partitionKey; + } + + /** + * @param string $partitionKey + * @return Config + * @throws PhpfastcacheLogicException + */ + public function setPartitionKey(string $partitionKey): Config + { + return $this->setProperty('partitionKey', $partitionKey); + } +} diff --git a/lib/Phpfastcache/Extensions/Drivers/Dynamodb/Driver.php b/lib/Phpfastcache/Extensions/Drivers/Dynamodb/Driver.php new file mode 100644 index 0000000..e61e75b --- /dev/null +++ b/lib/Phpfastcache/Extensions/Drivers/Dynamodb/Driver.php @@ -0,0 +1,306 @@ + + * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors + */ + +declare(strict_types=1); + +namespace Phpfastcache\Extensions\Drivers\Dynamodb; + +use Aws\Sdk as AwsSdk; +use Aws\DynamoDb\DynamoDbClient as AwsDynamoDbClient; +use Aws\DynamoDb\Marshaler as AwsMarshaler; +use Aws\DynamoDb\Exception\DynamoDbException as AwsDynamoDbException; +use Phpfastcache\Cluster\AggregatablePoolInterface; +use Phpfastcache\Core\Item\ExtendedCacheItemInterface; +use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; +use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait; +use Phpfastcache\Entities\DriverStatistic; +use Phpfastcache\Event\EventReferenceParameter; +use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException; +use Phpfastcache\Exceptions\PhpfastcacheDriverException; +use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; +use Phpfastcache\Exceptions\PhpfastcacheLogicException; +use Psr\Http\Message\UriInterface; + +/** + * Class Driver + * @method Config getConfig() + * @property AwsDynamoDbClient $instance + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ +class Driver implements AggregatablePoolInterface +{ + use TaggableCacheItemPoolTrait; + + protected const TTL_FIELD_NAME = 't'; + + protected AwsSdk $awsSdk; + + protected AwsMarshaler $marshaler; + + /** + * @return bool + */ + public function driverCheck(): bool + { + return \class_exists(AwsSdk::class) && \class_exists(AwsDynamoDbClient::class); + } + + /** + * @return bool + * @throws PhpfastcacheDriverConnectException + * @throws PhpfastcacheDriverException + * @throws PhpfastcacheLogicException + * @throws PhpfastcacheInvalidArgumentException + */ + protected function driverConnect(): bool + { + $wsAccessKey = $this->getConfig()->getSuperGlobalAccessor()('SERVER', 'AWS_ACCESS_KEY_ID'); + $awsSecretKey = $this->getConfig()->getSuperGlobalAccessor()('SERVER', 'AWS_SECRET_ACCESS_KEY'); + + if (empty($wsAccessKey)) { + throw new PhpfastcacheDriverConnectException('The environment configuration AWS_ACCESS_KEY_ID must be set'); + } + + if (empty($awsSecretKey)) { + throw new PhpfastcacheDriverConnectException('The environment configuration AWS_SECRET_ACCESS_KEY must be set'); + } + + $this->awsSdk = new AwsSdk([ + 'endpoint' => $this->getConfig()->getEndpoint(), + 'region' => $this->getConfig()->getRegion(), + 'version' => $this->getConfig()->getVersion(), + 'debug' => $this->getConfig()->isDebugEnabled(), + ]); + $this->instance = $this->awsSdk->createDynamoDb(); + $this->marshaler = new AwsMarshaler(); + + if (!$this->hasTable()) { + $this->createTable(); + } + + if (!$this->hasTtlEnabled()) { + $this->enableTtl(); + } + + return true; + } + + /** + * @param ExtendedCacheItemInterface $item + * @return bool + * @throws PhpfastcacheLogicException + */ + protected function driverWrite(ExtendedCacheItemInterface $item): bool + { + $awsItem = $this->marshaler->marshalItem( + \array_merge( + $this->encodeDocument($this->driverPreWrap($item, true)), + ['t' => $item->getExpirationDate()->getTimestamp()] + ) + ); + + $result = $this->instance->putItem([ + 'TableName' => $this->getConfig()->getTable(), + 'Item' => $awsItem + ]); + + return ($result->get('@metadata')['statusCode'] ?? null) === 200; + } + + /** + * @param ExtendedCacheItemInterface $item + * @return ?array + * @throws \Exception + */ + protected function driverRead(ExtendedCacheItemInterface $item): ?array + { + $key = $this->marshaler->marshalItem([ + $this->getConfig()->getPartitionKey() => $item->getKey() + ]); + + $result = $this->instance->getItem([ + 'TableName' => $this->getConfig()->getTable(), + 'Key' => $key + ]); + + $awsItem = $result->get('Item'); + + if ($awsItem !== null) { + return $this->decodeDocument( + $this->marshaler->unmarshalItem($awsItem) + ); + } + + return null; + } + + /** + * @param string $key + * @param string $encodedKey + * @return bool + */ + protected function driverDelete(string $key, string $encodedKey): bool + { + $dynKey = $this->marshaler->marshalItem([ + $this->getConfig()->getPartitionKey() => $key + ]); + + $result = $this->instance->deleteItem([ + 'TableName' => $this->getConfig()->getTable(), + 'Key' => $dynKey + ]); + + return ($result->get('@metadata')['statusCode'] ?? null) === 200; + } + + /** + * @return bool + * @throws PhpfastcacheDriverException + */ + protected function driverClear(): bool + { + $params = [ + 'TableName' => $this->getConfig()->getTable(), + ]; + + $result = $this->instance->deleteTable($params); + + $this->instance->waitUntil('TableNotExists', $params); + + $this->createTable(); + $this->enableTtl(); + + return ($result->get('@metadata')['statusCode'] ?? null) === 200; + } + + protected function hasTable(): bool + { + return \count($this->instance->listTables(['TableNames' => [$this->getConfig()->getTable()]])->get('TableNames')) > 0; + } + + protected function createTable(): void + { + $params = [ + 'TableName' => $this->getConfig()->getTable(), + 'KeySchema' => [ + [ + 'AttributeName' => $this->getConfig()->getPartitionKey(), + 'KeyType' => 'HASH' + ] + ], + 'AttributeDefinitions' => [ + [ + 'AttributeName' => $this->getConfig()->getPartitionKey(), + 'AttributeType' => 'S' + ], + ], + 'ProvisionedThroughput' => [ + 'ReadCapacityUnits' => 10, + 'WriteCapacityUnits' => 10 + ] + ]; + + $this->eventManager->dispatch(Event::DYNAMODB_CREATE_TABLE, $this, new EventReferenceParameter($params)); + + $this->instance->createTable($params); + $this->instance->waitUntil('TableExists', $params); + } + + protected function hasTtlEnabled(): bool + { + $ttlDesc = $this->instance->describeTimeToLive(['TableName' => $this->getConfig()->getTable()])->get('TimeToLiveDescription'); + + if (!isset($ttlDesc['AttributeName'], $ttlDesc['TimeToLiveStatus'])) { + return false; + } + + return $ttlDesc['TimeToLiveStatus'] === 'ENABLED' && $ttlDesc['AttributeName'] === self::TTL_FIELD_NAME; + } + + /** + * @throws PhpfastcacheDriverException + */ + protected function enableTtl(): void + { + try { + $this->instance->updateTimeToLive([ + 'TableName' => $this->getConfig()->getTable(), + 'TimeToLiveSpecification' => [ + "AttributeName" => self::TTL_FIELD_NAME, + "Enabled" => true + ], + ]); + } catch (AwsDynamoDbException $e) { + /** + * Error 400 can be an acceptable error of a + * Dynamodb restriction: "Time to live has been modified multiple times within a fixed interval" + * @see https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateTimeToLive.html + */ + if ($e->getStatusCode() !== 400) { + throw new PhpfastcacheDriverException( + 'Failed to enable TTL with the following error: ' . $e->getMessage() + ); + } + } + } + + public function getStats(): DriverStatistic + { + /** @var UriInterface $endpoint */ + $endpoint = $this->instance->getEndpoint(); + $table = $this->instance->describeTable(['TableName' => $this->getConfig()->getTable()])->get('Table'); + + $info = \sprintf( + 'Dynamo server "%s" | Table "%s" with %d item(s) stored', + $endpoint->getHost(), + $table['TableName'] ?? 'Unknown table name', + $table['ItemCount'] ?? 'Unknown item count', + ); + + $data = [ + 'dynamoEndpoint' => $endpoint, + 'dynamoTable' => $table, + 'dynamoConfig' => $this->instance->getConfig(), + 'dynamoApi' => $this->instance->getApi()->toArray(), + ]; + + return (new DriverStatistic()) + ->setData(implode(', ', array_keys($this->itemInstances))) + ->setInfo($info) + ->setRawData($data) + ->setSize($data['dynamoTable']['TableSizeBytes'] ?? 0); + } + + /** + * @param array $data + * @return array + */ + protected function encodeDocument(array $data): array + { + $data[self::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[self::DRIVER_DATA_WRAPPER_INDEX]); + + return $data; + } + + /** + * @param array $data + * @return array + */ + protected function decodeDocument(array $data): array + { + $data[self::DRIVER_DATA_WRAPPER_INDEX] = $this->unserialize($data[self::DRIVER_DATA_WRAPPER_INDEX]); + + return $data; + } +} diff --git a/lib/Phpfastcache/Extensions/Drivers/Dynamodb/Event.php b/lib/Phpfastcache/Extensions/Drivers/Dynamodb/Event.php new file mode 100644 index 0000000..82dbf3b --- /dev/null +++ b/lib/Phpfastcache/Extensions/Drivers/Dynamodb/Event.php @@ -0,0 +1,8 @@ + + * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors + */ + +declare(strict_types=1); + +namespace Phpfastcache\Extensions\Drivers\Dynamodb; + +use Phpfastcache\Core\Item\ExtendedCacheItemInterface; +use Phpfastcache\Core\Item\TaggableCacheItemTrait; + +class Item implements ExtendedCacheItemInterface +{ + use TaggableCacheItemTrait; + + protected function getDriverClass(): string + { + return Driver::class; + } +} diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..016503f --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,21 @@ + + + The Phpfastcache Coding Standards + + + + + + + tests/* + bin/* + docs/* + vendor/* + + + + + + + + diff --git a/phpmd.xml b/phpmd.xml new file mode 100644 index 0000000..ec3cf87 --- /dev/null +++ b/phpmd.xml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..f30448d --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +################################################### +# Complete PHPSTAN configuration for Travis CI +################################################### +parameters: + treatPhpDocTypesAsCertain: false diff --git a/quality.bat b/quality.bat new file mode 100644 index 0000000..bb5b909 --- /dev/null +++ b/quality.bat @@ -0,0 +1,9 @@ +@echo off +setlocal + +call vendor\bin\phpcbf lib/ --report=summary +call vendor\bin\phpcs lib/ --report=summary +call vendor\bin\phpmd lib/ ansi phpmd.xml +call vendor\bin\phpstan analyse lib/ -l 6 -c phpstan.neon 2>&1 + +endlocal diff --git a/quality.sh b/quality.sh new file mode 100644 index 0000000..f0cec37 --- /dev/null +++ b/quality.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +./vendor/bin/phpcbf lib/ --report=summary +./vendor/bin/phpcs lib/ --report=summary +./vendor/bin/phpmd lib/ ansi phpmd.xml +./vendor/bin/phpstan analyse lib/ -l 6 -c phpstan.neon 2>&1 diff --git a/tests/Configs/github-actions.php b/tests/Configs/github-actions.php new file mode 100644 index 0000000..7727fe6 --- /dev/null +++ b/tests/Configs/github-actions.php @@ -0,0 +1,8 @@ +setRegion('eu-west-3') + ->setEndpoint('dynamodb.eu-west-3.amazonaws.com') + ->setTable('phpfastcache'); diff --git a/tests/Dynamodb.test.php b/tests/Dynamodb.test.php new file mode 100644 index 0000000..db0694e --- /dev/null +++ b/tests/Dynamodb.test.php @@ -0,0 +1,30 @@ + + * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors + */ + +use Phpfastcache\CacheManager; +use Phpfastcache\EventManager; +use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException; +use Phpfastcache\Tests\Helper\TestHelper; + +chdir(__DIR__); +require_once __DIR__ . '/../vendor/autoload.php'; +$testHelper = new TestHelper('Dynamodb driver'); +$configFileName = __DIR__ . '/Configs/' . ($argv[1] ?? 'github-actions') . '.php'; +if (!file_exists($configFileName)) { + $configFileName = __DIR__ . '/Configs/github-actions.php'; +} + +$cacheInstance = CacheManager::getInstance('Dynamodb', include $configFileName); +$testHelper->runCRUDTests($cacheInstance, false); +$testHelper->terminateTest();