Skip to content

Commit

Permalink
Primary functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Hall committed Jan 4, 2018
1 parent 0a56409 commit cd3ba48
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
composer.lock
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "rapidwebltd/rw-file-cache-psr-6",
"description": "PSR-6 adapter for RW File Cache",
"type": "library",
"authors": [
{
"name": "Jordan Hall",
"email": "[email protected]"
}
],
"license": "LGPL3",
"require": {
"rapidwebltd/rw-file-cache": "^1.1",
"psr/cache": "^1.0"
},
"require-dev": {
"cache/integration-tests": "^0.16.0",
"phpunit/phpunit": "^5.7"
},
"autoload": {
"psr-4": {
"rapidweb\\RWFileCachePSR6\\": "src/"
}
}
}
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit Tests">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Functional Tests">
<directory suffix="Test.php">./tests/Functional</directory>
</testsuite>
</testsuites>
<filter>

</filter>
<php>

</php>
</phpunit>
11 changes: 11 additions & 0 deletions src/CacheInvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace rapidweb\RWFileCachePSR6;

use Psr\Cache\InvalidArgumentException;
use Exception;

class CacheInvalidArgumentException extends Exception implements InvalidArgumentException
{

}
64 changes: 64 additions & 0 deletions src/CacheItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace rapidweb\RWFileCachePSR6;

use Psr\Cache\CacheItemInterface;

class CacheItem implements CacheItemInterface
{
private $key;
private $value;
private $expires = 0;
public $isDeferred = false;

public function __construct($key, $value)
{
$this->key = $key;
$this->value = $value;
}

public function getKey()
{
return $this->key;
}

public function get()
{
if ($this->isHit()===false) {
return null;
}

return $this->value;
}

public function getExpires()
{
return $this->expires;
}

public function isHit()
{
return $this->value !== false;
}

public function set($value)
{
if ($this->isDeferred) {
return;
}

$this->value = $value;
return $this;
}

public function expiresAt($expiration)
{
return $this;
}

public function expiresAfter($time)
{
return $this;
}

}
118 changes: 118 additions & 0 deletions src/CacheItemPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace rapidweb\RWFileCachePSR6;

use Psr\Cache\CacheItemPoolInterface;
use rapidweb\RWFileCache\RWFileCache;
use Psr\Cache\CacheItemInterface;

class CacheItemPool implements CacheItemPoolInterface
{
private $rwFileCache;
private $deferredItems = [];

public function __construct()
{
$this->rwFileCache = new RWFileCache();
}

public function changeConfig(array $config)
{
return $this->rwFileCache->changeConfig($config);
}

private function sanityCheckKey($key)
{
if (!is_string($key)) {
throw new CacheInvalidArgumentException;
}

$invalidChars = ['{', '}', '(', ')', '/', '\\', '@', ':'];

foreach($invalidChars as $invalidChar) {
if (stripos($key, $invalidChar)!==false) {
throw new CacheInvalidArgumentException;
}
}

}

public function getItem($key)
{
$this->sanityCheckKey($key);

if (array_key_exists($key, $this->deferredItems)) {
return $this->deferredItems[$key];
}

return new CacheItem($key, $this->rwFileCache->get($key));
}

public function getItems(array $keys = [])
{
$results = [];

foreach($keys as $key) {
$results[$key] = $this->getItem($key);
}

return $results;
}

public function hasItem($key)
{
return $this->getItem($key)->isHit();
}

public function clear()
{
$this->deferredItems = [];
return $this->rwFileCache->flush();
}

public function deleteItem($key)
{
$this->sanityCheckKey($key);

if (array_key_exists($key, $this->deferredItems)) {
unset($this->deferredItems[$key]);
return true;
}

$this->rwFileCache->delete($key);

return true;
}

public function deleteItems(array $keys)
{


foreach($keys as $key) {
$this->deleteItem($key);
}

return true;
}

public function save(CacheItemInterface $item)
{
return $this->rwFileCache->set($item->getKey(), $item->get(), $item->getExpires());
}

public function saveDeferred(CacheItemInterface $item)
{
$item->isDeferred = true;
$this->deferredItems[$item->getKey()] = $item;
return true;
}

public function commit()
{
foreach($this->deferredItems as $item) {
$this->save($item);
}
$this->deferredItems = [];
return true;
}
}
16 changes: 16 additions & 0 deletions tests/Functional/PoolIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use PHPUnit\Framework\TestCase;

use Cache\IntegrationTests\CachePoolTest;
use rapidweb\RWFileCachePSR6\CacheItemPool;

class PoolIntegrationTest extends CachePoolTest
{
public function createCachePool()
{
$cacheItemPool = new CacheItemPool();
$cacheItemPool->changeConfig(['cacheDirectory' => __DIR__.'/Data/']);
return $cacheItemPool;
}
}
Empty file added tests/Unit/.keep
Empty file.

0 comments on commit cd3ba48

Please sign in to comment.