Skip to content

Commit 37c86c9

Browse files
florianvNyholm
authored andcommitted
Added PSR-6 to Illuminate adapter (#140)
* Added PSR-6 to Illuminate adapter * Required illuminate/cache 5.4+ * Drop PHP 5.5 support
0 parents  commit 37c86c9

14 files changed

+459
-0
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is a READ ONLY repository.
2+
3+
Please make your pull request to https://github.com/php-cache/cache
4+
5+
Thank you for contributing.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: php
2+
sudo: false
3+
4+
matrix:
5+
include:
6+
- php: 7.0
7+
8+
cache:
9+
directories:
10+
- "$HOME/.composer/cache"
11+
12+
install:
13+
- composer update --prefer-dist --prefer-stable
14+
15+
script:
16+
- ./vendor/bin/phpunit --coverage-clover=coverage.xml
17+
18+
after_success:
19+
- pip install --user codecov && codecov
20+
21+
notifications:
22+
email: false

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Change Log
2+
3+
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

IlluminateCachePool.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Illuminate;
13+
14+
use Cache\Adapter\Common\AbstractCachePool;
15+
use Cache\Adapter\Common\PhpCacheItem;
16+
use Illuminate\Contracts\Cache\Store;
17+
18+
/**
19+
* This is a bridge between PSR-6 and an Illuminate cache store.
20+
*
21+
* @author Florian Voutzinos <[email protected]>
22+
*/
23+
class IlluminateCachePool extends AbstractCachePool
24+
{
25+
/**
26+
* @type Store
27+
*/
28+
protected $store;
29+
30+
/**
31+
* @param Store $store
32+
*/
33+
public function __construct(Store $store)
34+
{
35+
$this->store = $store;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
protected function storeItemInCache(PhpCacheItem $item, $ttl)
42+
{
43+
$ttl = null === $ttl ? 0 : $ttl / 60;
44+
45+
$data = serialize([true, $item->get(), $item->getTags(), $item->getExpirationTimestamp()]);
46+
47+
$this->store->put($item->getKey(), $data, $ttl);
48+
49+
return true;
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
protected function fetchObjectFromCache($key)
56+
{
57+
if (null === $data = $this->store->get($key)) {
58+
return [false, null, [], null];
59+
}
60+
61+
return unserialize($data);
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
protected function clearAllObjectsFromCache()
68+
{
69+
return $this->store->flush();
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
protected function clearOneObjectFromCache($key)
76+
{
77+
return $this->store->forget($key);
78+
}
79+
80+
/**
81+
* {@inheritdoc}
82+
*/
83+
protected function getList($name)
84+
{
85+
$list = $this->store->get($name);
86+
87+
if (!is_array($list)) {
88+
return [];
89+
}
90+
91+
return $list;
92+
}
93+
94+
/**
95+
* {@inheritdoc}
96+
*/
97+
protected function removeList($name)
98+
{
99+
return $this->store->forget($name);
100+
}
101+
102+
/**
103+
* {@inheritdoc}
104+
*/
105+
protected function appendListItem($name, $key)
106+
{
107+
$list = $this->getList($name);
108+
$list[] = $key;
109+
110+
$this->store->forever($name, $list);
111+
}
112+
113+
/**
114+
* {@inheritdoc}
115+
*/
116+
protected function removeListItem($name, $key)
117+
{
118+
$list = $this->getList($name);
119+
120+
foreach ($list as $i => $item) {
121+
if ($item === $key) {
122+
unset($list[$i]);
123+
}
124+
}
125+
126+
$this->store->forever($name, $list);
127+
}
128+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Aaron Scherer, Tobias Nyholm
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Illuminate PSR-6 Cache pool
2+
[![Gitter](https://badges.gitter.im/php-cache/cache.svg)](https://gitter.im/php-cache/cache?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
3+
[![Latest Stable Version](https://poser.pugx.org/cache/illuminate-adapter/v/stable)](https://packagist.org/packages/cache/illuminate-adapter)
4+
[![codecov.io](https://codecov.io/github/php-cache/illuminate-adapter/coverage.svg?branch=master)](https://codecov.io/github/php-cache/illuminate-adapter?branch=master)
5+
[![Total Downloads](https://poser.pugx.org/cache/illuminate-adapter/downloads)](https://packagist.org/packages/cache/illuminate-adapter)
6+
[![Monthly Downloads](https://poser.pugx.org/cache/illuminate-adapter/d/monthly.png)](https://packagist.org/packages/cache/illuminate-adapter)
7+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
8+
9+
This is a PSR-6 cache implementation using Illuminate cache. It is a part of the PHP Cache organisation. To read about
10+
features like tagging and hierarchy support please read the shared documentation at [www.php-cache.com](http://www.php-cache.com).
11+
12+
This is a PSR-6 to Illuminate bridge.
13+
14+
### Install
15+
16+
```bash
17+
composer require cache/illuminate-adapter
18+
```
19+
20+
## Use
21+
22+
```php
23+
use Illuminate\Cache\ArrayStore;
24+
use Cache\Adapter\Illuminate\IlluminateCachePool;
25+
26+
// Create an instance of an Illuminate's Store
27+
$store = new ArrayStore();
28+
29+
// Wrap the Illuminate's store with the PSR-6 adapter
30+
$pool = new IlluminateCachePool($store);
31+
```
32+
33+
34+
### Contribute
35+
36+
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/php-cache/cache) or
37+
report any issues you find on the [issue tracker](http://issues.php-cache.com).

Tests/CreatePoolTrait.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Illuminate\Tests;
13+
14+
use Cache\Adapter\Illuminate\IlluminateCachePool;
15+
use Illuminate\Cache\ArrayStore;
16+
17+
trait CreatePoolTrait
18+
{
19+
private $illuminateStore = null;
20+
21+
public function createCachePool()
22+
{
23+
return new IlluminateCachePool($this->getIlluminateStore());
24+
}
25+
26+
private function getIlluminateStore()
27+
{
28+
if ($this->illuminateStore === null) {
29+
$this->illuminateStore = new ArrayStore();
30+
}
31+
32+
return $this->illuminateStore;
33+
}
34+
35+
public function createSimpleCache()
36+
{
37+
return $this->createCachePool();
38+
}
39+
}

Tests/IlluminateAdapterTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Illuminate\Tests;
13+
14+
use Cache\Adapter\Common\CacheItem;
15+
use Cache\Adapter\Illuminate\IlluminateCachePool;
16+
use Illuminate\Contracts\Cache\Store;
17+
use Mockery as m;
18+
use Mockery\MockInterface;
19+
use Psr\Cache\CacheItemPoolInterface;
20+
21+
class IlluminateAdapterTest extends \PHPUnit_Framework_TestCase
22+
{
23+
/**
24+
* @type IlluminateCachePool
25+
*/
26+
private $pool;
27+
28+
/**
29+
* @type MockInterface|CacheItem
30+
*/
31+
private $mockItem;
32+
33+
/**
34+
* @type MockInterface|Store
35+
*/
36+
private $mockStore;
37+
38+
protected function setUp()
39+
{
40+
$this->mockItem = m::mock(CacheItem::class);
41+
$this->mockStore = m::mock(Store::class);
42+
$this->pool = new IlluminateCachePool($this->mockStore);
43+
}
44+
45+
public function testConstructor()
46+
{
47+
$this->assertInstanceOf(IlluminateCachePool::class, $this->pool);
48+
$this->assertInstanceOf(CacheItemPoolInterface::class, $this->pool);
49+
}
50+
}

Tests/IntegrationPoolTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Illuminate\Tests;
13+
14+
use Cache\IntegrationTests\CachePoolTest;
15+
16+
class IntegrationPoolTest extends CachePoolTest
17+
{
18+
use CreatePoolTrait;
19+
}

0 commit comments

Comments
 (0)