Skip to content

Commit

Permalink
feat: add TypedItem to allow for psr/cache:3 (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Apr 11, 2022
1 parent b0a2392 commit 321e5b6
Show file tree
Hide file tree
Showing 20 changed files with 317 additions and 116 deletions.
18 changes: 0 additions & 18 deletions .github/apply-phpunit-patches.sh

This file was deleted.

24 changes: 6 additions & 18 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0", "8.1" ]
php: [ "7.1", "7.2", "7.3", "7.4", "8.0", "8.1" ]
name: PHP ${{matrix.php }} Unit Test
steps:
- uses: actions/checkout@v3
Expand All @@ -23,46 +23,34 @@ jobs:
timeout_minutes: 10
max_attempts: 3
command: composer install
- if: ${{ contains(fromJson('["5.6", "7.0", "7.1"]'), matrix.php)}}
name: Run PHPUnit Patches
run: sh .github/apply-phpunit-patches.sh
- name: Run Script
run: vendor/bin/phpunit
test_lowest:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ "5.6", "7.2" ]
name: PHP ${{matrix.php }} Unit Test Prefer Lowest
name: Test Prefer Lowest
steps:
- uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
php-version: "7.1"
- name: Install Dependencies
uses: nick-invision/retry@v2
with:
timeout_minutes: 10
max_attempts: 3
command: composer update --prefer-lowest
- if: ${{ matrix.php == '5.6' }}
name: Run PHPUnit Patches
run: sh .github/apply-phpunit-patches.sh
- name: Run Script
run: vendor/bin/phpunit
guzzle6:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ "7.2" ]
name: PHP ${{ matrix.php }} Unit Test Guzzle 6
name: Test Guzzle 6
steps:
- uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
php-version: "7.2"
- name: Install Dependencies
uses: nick-invision/retry@v2
with:
Expand All @@ -79,7 +67,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "7.4"
php-version: "8.0"
- name: Install Dependencies
uses: nick-invision/retry@v2
with:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
"docs": "https://googleapis.github.io/google-auth-library-php/main/"
},
"require": {
"php": ">=5.6",
"php": "^7.1||^8.0",
"firebase/php-jwt": "~5.0",
"guzzlehttp/guzzle": "^6.2.1|^7.0",
"guzzlehttp/psr7": "^1.7|^2.0",
"psr/http-message": "^1.0",
"psr/cache": "^1.0|^2.0"
"psr/cache": "^1.0|^2.0|^3.0"
},
"require-dev": {
"guzzlehttp/promises": "0.1.1|^1.3",
"squizlabs/php_codesniffer": "^3.5",
"phpunit/phpunit": "^5.7||^8.5.13",
"phpunit/phpunit": "^7.5||^8.5",
"phpspec/prophecy-phpunit": "^1.1",
"sebastian/comparator": ">=1.2.3",
"phpseclib/phpseclib": "^2.0.31",
Expand Down
24 changes: 12 additions & 12 deletions src/Cache/MemoryCacheItemPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@ final class MemoryCacheItemPool implements CacheItemPoolInterface
* @return CacheItemInterface
* The corresponding Cache Item.
*/
public function getItem($key)
public function getItem($key): CacheItemInterface
{
return current($this->getItems([$key]));
}

/**
* {@inheritdoc}
*
* @return array
* @return iterable
* A traversable collection of Cache Items keyed by the cache keys of
* each item. A Cache item will be returned for each key, even if that
* key is not found. However, if no keys are specified then an empty
* traversable MUST be returned instead.
*/
public function getItems(array $keys = [])
public function getItems(array $keys = []): iterable
{
$items = [];

$itemClass = \PHP_VERSION_ID >= 80000 ? TypedItem::class : Item::class;
foreach ($keys as $key) {
$items[$key] = $this->hasItem($key) ? clone $this->items[$key] : new Item($key);
$items[$key] = $this->hasItem($key) ? clone $this->items[$key] : new $itemClass($key);
}

return $items;
Expand All @@ -72,7 +72,7 @@ public function getItems(array $keys = [])
* @return bool
* True if item exists in the cache, false otherwise.
*/
public function hasItem($key)
public function hasItem($key): bool
{
$this->isValidKey($key);

Expand All @@ -85,7 +85,7 @@ public function hasItem($key)
* @return bool
* True if the pool was successfully cleared. False if there was an error.
*/
public function clear()
public function clear(): bool
{
$this->items = [];
$this->deferredItems = [];
Expand All @@ -99,7 +99,7 @@ public function clear()
* @return bool
* True if the item was successfully removed. False if there was an error.
*/
public function deleteItem($key)
public function deleteItem($key): bool
{
return $this->deleteItems([$key]);
}
Expand All @@ -110,7 +110,7 @@ public function deleteItem($key)
* @return bool
* True if the items were successfully removed. False if there was an error.
*/
public function deleteItems(array $keys)
public function deleteItems(array $keys): bool
{
array_walk($keys, [$this, 'isValidKey']);

Expand All @@ -127,7 +127,7 @@ public function deleteItems(array $keys)
* @return bool
* True if the item was successfully persisted. False if there was an error.
*/
public function save(CacheItemInterface $item)
public function save(CacheItemInterface $item): bool
{
$this->items[$item->getKey()] = $item;

Expand All @@ -140,7 +140,7 @@ public function save(CacheItemInterface $item)
* @return bool
* False if the item could not be queued or if a commit was attempted and failed. True otherwise.
*/
public function saveDeferred(CacheItemInterface $item)
public function saveDeferred(CacheItemInterface $item): bool
{
$this->deferredItems[$item->getKey()] = $item;

Expand All @@ -153,7 +153,7 @@ public function saveDeferred(CacheItemInterface $item)
* @return bool
* True if all not-yet-saved items were successfully saved or there were none. False otherwise.
*/
public function commit()
public function commit(): bool
{
foreach ($this->deferredItems as $item) {
$this->save($item);
Expand Down
21 changes: 11 additions & 10 deletions src/Cache/SysVCacheItemPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function __construct($options = [])
$this->sysvKey = ftok(__FILE__, $this->options['proj']);
}

public function getItem($key)
public function getItem($key): CacheItemInterface
{
$this->loadItems();
return current($this->getItems([$key]));
Expand All @@ -99,22 +99,23 @@ public function getItem($key)
/**
* {@inheritdoc}
*/
public function getItems(array $keys = [])
public function getItems(array $keys = []): iterable
{
$this->loadItems();
$items = [];
$itemClass = \PHP_VERSION_ID >= 80000 ? TypedItem::class : Item::class;
foreach ($keys as $key) {
$items[$key] = $this->hasItem($key) ?
clone $this->items[$key] :
new Item($key);
new $itemClass($key);
}
return $items;
}

/**
* {@inheritdoc}
*/
public function hasItem($key)
public function hasItem($key): bool
{
$this->loadItems();
return isset($this->items[$key]) && $this->items[$key]->isHit();
Expand All @@ -123,7 +124,7 @@ public function hasItem($key)
/**
* {@inheritdoc}
*/
public function clear()
public function clear(): bool
{
$this->items = [];
$this->deferredItems = [];
Expand All @@ -133,15 +134,15 @@ public function clear()
/**
* {@inheritdoc}
*/
public function deleteItem($key)
public function deleteItem($key): bool
{
return $this->deleteItems([$key]);
}

/**
* {@inheritdoc}
*/
public function deleteItems(array $keys)
public function deleteItems(array $keys): bool
{
if (!$this->hasLoadedItems) {
$this->loadItems();
Expand All @@ -156,7 +157,7 @@ public function deleteItems(array $keys)
/**
* {@inheritdoc}
*/
public function save(CacheItemInterface $item)
public function save(CacheItemInterface $item): bool
{
if (!$this->hasLoadedItems) {
$this->loadItems();
Expand All @@ -169,7 +170,7 @@ public function save(CacheItemInterface $item)
/**
* {@inheritdoc}
*/
public function saveDeferred(CacheItemInterface $item)
public function saveDeferred(CacheItemInterface $item): bool
{
$this->deferredItems[$item->getKey()] = $item;
return true;
Expand All @@ -178,7 +179,7 @@ public function saveDeferred(CacheItemInterface $item)
/**
* {@inheritdoc}
*/
public function commit()
public function commit(): bool
{
foreach ($this->deferredItems as $item) {
if ($this->save($item) === false) {
Expand Down
Loading

0 comments on commit 321e5b6

Please sign in to comment.