Skip to content

Commit

Permalink
Add a prototype for the file system cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed May 7, 2024
1 parent 23e8e69 commit 0f072f4
Showing 1 changed file with 212 additions and 0 deletions.
212 changes: 212 additions & 0 deletions src/Cache/FileSystemCacheItemPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php
/**
* Copyright 2024 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Auth\Cache;

use ErrorException;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheItemInterface;

class FileSystemCacheItemPool implements CacheItemPoolInterface
{
private string $cachePath = 'cache/';
private array $deferredPool = [];

Check failure on line 26 in src/Cache/FileSystemCacheItemPool.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Property Google\Auth\Cache\FileSystemCacheItemPool::$deferredPool type has no value type specified in iterable type array.

public function __construct($options = [])

Check failure on line 28 in src/Cache/FileSystemCacheItemPool.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Method Google\Auth\Cache\FileSystemCacheItemPool::__construct() has parameter $options with no type specified.
{
if (array_key_exists('path', $options)) {
$this->cachePath = $options['path'];
}

if (is_dir($this->cachePath)) {
return true;

Check failure on line 35 in src/Cache/FileSystemCacheItemPool.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Method Google\Auth\Cache\FileSystemCacheItemPool::__construct() with return type void returns true but should not return anything.
}

if (!mkdir($this->cachePath)) {
throw new ErrorException("Cache folde couldn't be created");
}
}

/**
* {@inheritdoc}
*/
public function getItem(string $key): CacheItemInterface
{
if (!$this->isValidKey($key)) {
throw new InvalidArgumentException("The key '$key' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|");
}

if (!$this->hasItem($key)) {
return new TypedItem($key);
}

// Could this cause any issues? Check for result first maybe?
return unserialize(file_get_contents($this->cacheFilePath($key)));

Check failure on line 57 in src/Cache/FileSystemCacheItemPool.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Parameter #1 $data of function unserialize expects string, string|false given.
}

/**
* {@inheritdoc}
*/
public function getItems(array $keys = []): iterable

Check failure on line 63 in src/Cache/FileSystemCacheItemPool.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Method Google\Auth\Cache\FileSystemCacheItemPool::getItems() return type has no value type specified in iterable type iterable.
{
$result = [];

foreach ($keys as $key) {
if ($this->isValidKey($key)){
throw new InvalidArgumentException("The key '$key' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|");
}

$result[$key] = $this->getItem($key);
}

return $result;
}

/**
* {@inheritdoc}
*/
public function hasItem(string $key): bool
{
$itemPath = $this->cacheFilePath($key);

if (!file_exists($itemPath)) {
return false;
}

$serializedItem = file_get_contents($this->cacheFilePath($key));
return unserialize($serializedItem)->isHit();

Check failure on line 90 in src/Cache/FileSystemCacheItemPool.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Parameter #1 $data of function unserialize expects string, string|false given.
}

/**
* {@inheritdoc}
*/
public function clear(): bool
{
if (!is_dir($this->cachePath)) {
return false;
}

foreach (scandir($this->cachePath) as $fileName) {

Check failure on line 102 in src/Cache/FileSystemCacheItemPool.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Argument of an invalid type array<int, string>|false supplied for foreach, only iterables are supported.
if ($fileName === '.' || $fileName === '..') {
continue;
}

// We are not worrying for folders as the cache shouldn't have
// folders inside. Should we continue deleting files on false?
if (!unlink("$this->cachePath/$fileName")) {
return false;
}
}

if (!rmdir($this->cachePath)) {
return false;
}

if (!mkdir($this->cachePath)) {
return false;
}

return true;
}

/**
* {@inheritdoc}
*/
public function deleteItem(string $key): bool
{
if ($this->isValidKey($key)) {
throw new InvalidArgumentException("The key '$key' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|");
}

return unlink($this->cacheFilePath($key));
}

/**
* {@inheritdoc}
*/
public function deleteItems(array $keys): bool
{
$result = true;

foreach ($keys as $key) {
if (!$this->isValidKey($key)) {
throw new InvalidArgumentException("The key '$key' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|");
}

if (!$this->deleteItem($key)) {
$result = false;
}
}

return $result;
}

/**
* {@inheritdoc}
*/
public function save(CacheItemInterface $item): bool
{
$serializedItem = serialize($item);

$result = file_put_contents($this->cacheFilePath($item->getKey()), $serializedItem);

// file_put_contents returns the number of bytes written
// or a boolean. In theory there should never be a case
// where is 0 bytes written but I still preffer to check
// for a boolean
if ($result === false) {
return false;
}

return true;
}

/**
* {@inheritdoc}
*/
public function saveDeferred(CacheItemInterface $item): bool
{
array_push($this->deferredPool, $item);

return true;
}

/**
* {@inheritdoc}
*/
public function commit(): bool
{
$result = true;

foreach ($this->deferredPool as $item) {
if (!$this->save($item)) {
$result = false;
}
}

return $result;
}

private function cacheFilePath($key): string

Check failure on line 203 in src/Cache/FileSystemCacheItemPool.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Method Google\Auth\Cache\FileSystemCacheItemPool::cacheFilePath() has parameter $key with no type specified.
{
return "$this->cachePath/$key";
}

private function isValidKey(string $key): bool
{
return !preg_match('|^[a-zA-Z0-9_\.! ]+$|', $key);
}
}

0 comments on commit 0f072f4

Please sign in to comment.