From 3b8acb2751147051c12ed19aec76084e0ad13f22 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Thu, 3 Aug 2023 15:47:11 +0300 Subject: [PATCH] initial commit --- .gitattributes | 8 +++++ .github/FUNDING.yml | 4 +++ .github/ISSUE_TEMPLATE.md | 14 ++++++++ .github/PULL_REQUEST_TEMPLATE.md | 7 ++++ .github/workflows/build.yml | 31 +++++++++++++++++ .gitignore | 35 ++++++++++++++++++++ CHANGELOG.md | 7 ++++ LICENSE.md | 32 ++++++++++++++++++ README.md | 41 +++++++++++++++++++++++ composer.json | 40 ++++++++++++++++++++++ phpunit.xml.dist | 15 +++++++++ src/CacheItem.php | 14 ++++++++ src/CacheItemPool.php | 47 ++++++++++++++++++++++++++ tests/CacheItemPoolTest.php | 34 +++++++++++++++++++ tests/TestCase.php | 57 ++++++++++++++++++++++++++++++++ tests/bootstrap.php | 14 ++++++++ 16 files changed, 400 insertions(+) create mode 100644 .gitattributes create mode 100644 .github/FUNDING.yml create mode 100644 .github/ISSUE_TEMPLATE.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .github/workflows/build.yml create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/CacheItem.php create mode 100644 src/CacheItemPool.php create mode 100644 tests/CacheItemPoolTest.php create mode 100644 tests/TestCase.php create mode 100644 tests/bootstrap.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..48572f0 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +# Ignore all test and documentation for archive +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.scrutinizer.yml export-ignore +/phpunit.xml.dist export-ignore +/tests export-ignore +/docs export-ignore \ No newline at end of file diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..99679ca --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,4 @@ +# These are supported funding model platforms + +github: [klimov-paul] +patreon: klimov_paul diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..2cc6439 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,14 @@ +### What steps will reproduce the problem? + +### What is the expected result? + +### What do you get instead? + +### Additional info + +| Q | A +|-----------------------| --- +| This Package Version | 1.?.? +| Yii Framework Version | 1.1.? +| PHP version | +| Operating system | diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..f4af2f3 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,7 @@ +| Q | A +| ------------- | --- +| Is bugfix? | ✔️/❌ +| New feature? | ✔️/❌ +| Breaks BC? | ✔️/❌ +| Tests pass? | ✔️/❌ +| Fixed issues | comma-separated list of tickets # fixed by the PR, if any \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..11a2323 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,31 @@ +name: build + +on: [push, pull_request] + +jobs: + phpunit: + name: PHP ${{ matrix.php }} on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + php: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: mbstring + tools: composer:v2 + coverage: none + + - name: Install dependencies + run: | + composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + - name: Run unit tests + run: vendor/bin/phpunit --colors=always diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..304f7c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +# phpstorm project files +.idea + +# netbeans project files +nbproject + +# zend studio for eclipse project files +.buildpath +.project +.settings + +# windows thumbnail cache +Thumbs.db + +# composer vendor dir +/vendor + +/composer.lock + +# composer itself is not needed +composer.phar + +# Mac DS_Store Files +.DS_Store + +# phpunit itself is not needed +phpunit.phar +# local phpunit config +/phpunit.xml +# phpunit cache +.phpunit.result.cache + +# test runtime files +/.phpunit.cache +/tests/runtime \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..19535a4 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +Yii1 PSR-6 Cache extension +========================== + +1.0.0 Under Development +----------------------- + +- Initial release. diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e641e6f --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,32 @@ +This is free software. It is released under the terms of the +following BSD License. + +Copyright © 2023 by Yii1Tech (https://github.com/yii1tech) +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + * Neither the name of Yii1Tech nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..136ec05 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +

+ + + +

Yii1 PSR-6 Cache Extension

+
+

+ +This extension allows integration with PSR-6 compatible cache for Yii1. + +For license information check the [LICENSE](LICENSE.md)-file. + +[![Latest Stable Version](https://img.shields.io/packagist/v/yii1tech/psr-cache.svg)](https://packagist.org/packages/yii1tech/psr-cache) +[![Total Downloads](https://img.shields.io/packagist/dt/yii1tech/psr-cache.svg)](https://packagist.org/packages/yii1tech/psr-cache) +[![Build Status](https://github.com/yii1tech/psr-cache/workflows/build/badge.svg)](https://github.com/yii1tech/psr-cache/actions) + + +Installation +------------ + +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run + +``` +php composer.phar require --prefer-dist yii1tech/psr-cache +``` + +or add + +```json +"yii1tech/psr-cache": "*" +``` + +to the "require" section of your composer.json. + + +Usage +----- + +This extension allows integration with PSR-6 compatible cache for Yii1. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ebb0b41 --- /dev/null +++ b/composer.json @@ -0,0 +1,40 @@ +{ + "name": "yii1tech/psr-cache", + "description": "Provides support for PSR-6 compatible cache for Yii1 application", + "keywords": ["yii1", "psr", "cache", "psr-6", "caching"], + "license": "BSD-3-Clause", + "support": { + "issues": "https://github.com/yii1tech/psr-cache/issues", + "wiki": "https://github.com/yii1tech/psr-cache/wiki", + "source": "https://github.com/yii1tech/psr-cache" + }, + "authors": [ + { + "name": "Paul Klimov", + "email": "klimov.paul@gmail.com" + } + ], + "require": { + "php": ">=7.1", + "yiisoft/yii": "~1.1.0", + "psr/cache": ">=1.0.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0 || ^7.0 || ^8.0 || ^9.3 || ^10.0.7" + }, + "autoload": { + "psr-4": { + "yii1tech\\psr\\cache\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "yii1tech\\psr\\cache\\test\\": "tests" + } + }, + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + } +} \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..1dc5e87 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,15 @@ + + + + + ./tests + + + diff --git a/src/CacheItem.php b/src/CacheItem.php new file mode 100644 index 0000000..2939f20 --- /dev/null +++ b/src/CacheItem.php @@ -0,0 +1,14 @@ + + * @since 1.0 + */ +class CacheItem extends CComponent +{ + +} \ No newline at end of file diff --git a/src/CacheItemPool.php b/src/CacheItemPool.php new file mode 100644 index 0000000..3bd31ed --- /dev/null +++ b/src/CacheItemPool.php @@ -0,0 +1,47 @@ + + * @since 1.0 + */ +class CacheItemPool extends CApplicationComponent +{ + /** + * @var \ICache|array|string + */ + private $_cache = 'cache'; + + /** + * @return \ICache Yii cache component instance. + */ + public function getCache() + { + if (!is_object($this->_cache)) { + if (is_string($this->_cache)) { + $this->_cache = Yii::app()->getComponent($this->_cache); + } else { + $this->_cache = Yii::createComponent($this->_cache); + } + } + + return $this->_cache; + } + + /** + * @param \ICache|array|string $cache cache component instance, application component ID or array configuration. + * @return static self reference. + */ + public function setCache($cache): self + { + $this->_cache = $cache; + + return $this; + } + + +} \ No newline at end of file diff --git a/tests/CacheItemPoolTest.php b/tests/CacheItemPoolTest.php new file mode 100644 index 0000000..08461a1 --- /dev/null +++ b/tests/CacheItemPoolTest.php @@ -0,0 +1,34 @@ +setCache($cache); + $this->assertSame($cache, $pool->getCache()); + + $pool->setCache([ + 'class' => CDummyCache::class, + ]); + $cache = $pool->getCache(); + $this->assertTrue($cache instanceof CDummyCache); + } + + public function testGetDefaultCache(): void + { + $pool = new CacheItemPool(); + + $cache = $pool->getCache(); + $this->assertTrue($cache instanceof ICache); + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..02305f5 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,57 @@ +mockApplication(); + } + + /** + * {@inheritdoc} + */ + protected function tearDown(): void + { + $this->destroyApplication(); + } + + /** + * Populates Yii::app() with a new application + * The application will be destroyed on tearDown() automatically. + * @param array $config The application configuration, if needed + * @param string $appClass name of the application class to create + */ + protected function mockApplication($config = [], $appClass = CConsoleApplication::class) + { + Yii::setApplication(null); + + new $appClass(CMap::mergeArray([ + 'id' => 'testapp', + 'basePath' => __DIR__, + 'components' => [ + 'cache' => [ + 'class' => \CDummyCache::class, + ], + ], + ], $config)); + } + + /** + * Destroys Yii application by setting it to null. + */ + protected function destroyApplication() + { + Yii::setApplication(null); + } +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..f238b2d --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,14 @@ +