Skip to content

Commit 9533653

Browse files
authored
Merge pull request #303 from Geolim4/final
Provided Psr6 Interfaces for legacy users
2 parents 26dc30c + 3545669 commit 9533653

File tree

10 files changed

+332
-8
lines changed

10 files changed

+332
-8
lines changed

bin/legacy/Psr/Cache/LICENSE.txt

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

bin/legacy/Psr/Cache/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
PSR Cache
2+
=========
3+
4+
This repository holds all interfaces defined by
5+
[PSR-6](http://www.php-fig.org/psr/psr-6/).
6+
7+
Note that this is not a Cache implementation of its own. It is merely an
8+
interface that describes a Cache implementation. See the specification for more
9+
details.

bin/legacy/Psr/Cache/composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "psr/cache",
3+
"description": "Common interface for caching libraries",
4+
"keywords": ["psr", "psr-6", "cache"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "PHP-FIG",
9+
"homepage": "http://www.php-fig.org/"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.3.0"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Psr\\Cache\\": "src/"
18+
}
19+
},
20+
"extra": {
21+
"branch-alias": {
22+
"dev-master": "1.0.x-dev"
23+
}
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Psr\Cache;
4+
5+
/**
6+
* Exception interface for all exceptions thrown by an Implementing Library.
7+
*/
8+
interface CacheException
9+
{
10+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Psr\Cache;
4+
5+
/**
6+
* CacheItemInterface defines an interface for interacting with objects inside a cache.
7+
*
8+
* Each Item object MUST be associated with a specific key, which can be set
9+
* according to the implementing system and is typically passed by the
10+
* Cache\CacheItemPoolInterface object.
11+
*
12+
* The Cache\CacheItemInterface object encapsulates the storage and retrieval of
13+
* cache items. Each Cache\CacheItemInterface is generated by a
14+
* Cache\CacheItemPoolInterface object, which is responsible for any required
15+
* setup as well as associating the object with a unique Key.
16+
* Cache\CacheItemInterface objects MUST be able to store and retrieve any type
17+
* of PHP value defined in the Data section of the specification.
18+
*
19+
* Calling Libraries MUST NOT instantiate Item objects themselves. They may only
20+
* be requested from a Pool object via the getItem() method. Calling Libraries
21+
* SHOULD NOT assume that an Item created by one Implementing Library is
22+
* compatible with a Pool from another Implementing Library.
23+
*
24+
*/
25+
interface CacheItemInterface
26+
{
27+
/**
28+
* Returns the key for the current cache item.
29+
*
30+
* The key is loaded by the Implementing Library, but should be available to
31+
* the higher level callers when needed.
32+
*
33+
* @return string
34+
* The key string for this cache item.
35+
*/
36+
public function getKey();
37+
38+
/**
39+
* Retrieves the value of the item from the cache associated with this object's key.
40+
*
41+
* The value returned must be identical to the value originally stored by set().
42+
*
43+
* If isHit() returns false, this method MUST return null. Note that null
44+
* is a legitimate cached value, so the isHit() method SHOULD be used to
45+
* differentiate between "null value was found" and "no value was found."
46+
*
47+
* @return mixed
48+
* The value corresponding to this cache item's key, or null if not found.
49+
*/
50+
public function get();
51+
52+
/**
53+
* Confirms if the cache item lookup resulted in a cache hit.
54+
*
55+
* Note: This method MUST NOT have a race condition between calling isHit()
56+
* and calling get().
57+
*
58+
* @return bool
59+
* True if the request resulted in a cache hit. False otherwise.
60+
*/
61+
public function isHit();
62+
63+
/**
64+
* Sets the value represented by this cache item.
65+
*
66+
* The $value argument may be any item that can be serialized by PHP,
67+
* although the method of serialization is left up to the Implementing
68+
* Library.
69+
*
70+
* @param mixed $value
71+
* The serializable value to be stored.
72+
*
73+
* @return static
74+
* The invoked object.
75+
*/
76+
public function set($value);
77+
78+
/**
79+
* Sets the expiration time for this cache item.
80+
*
81+
* @param \DateTimeInterface $expiration
82+
* The point in time after which the item MUST be considered expired.
83+
* If null is passed explicitly, a default value MAY be used. If none is set,
84+
* the value should be stored permanently or for as long as the
85+
* implementation allows.
86+
*
87+
* @return static
88+
* The called object.
89+
*/
90+
public function expiresAt($expiration);
91+
92+
/**
93+
* Sets the expiration time for this cache item.
94+
*
95+
* @param int|\DateInterval $time
96+
* The period of time from the present after which the item MUST be considered
97+
* expired. An integer parameter is understood to be the time in seconds until
98+
* expiration. If null is passed explicitly, a default value MAY be used.
99+
* If none is set, the value should be stored permanently or for as long as the
100+
* implementation allows.
101+
*
102+
* @return static
103+
* The called object.
104+
*/
105+
public function expiresAfter($time);
106+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace Psr\Cache;
4+
5+
/**
6+
* CacheItemPoolInterface generates CacheItemInterface objects.
7+
*
8+
* The primary purpose of Cache\CacheItemPoolInterface is to accept a key from
9+
* the Calling Library and return the associated Cache\CacheItemInterface object.
10+
* It is also the primary point of interaction with the entire cache collection.
11+
* All configuration and initialization of the Pool is left up to an
12+
* Implementing Library.
13+
*
14+
*/
15+
interface CacheItemPoolInterface
16+
{
17+
/**
18+
* Returns a Cache Item representing the specified key.
19+
*
20+
* This method must always return a CacheItemInterface object, even in case of
21+
* a cache miss. It MUST NOT return null.
22+
*
23+
* @param string $key
24+
* The key for which to return the corresponding Cache Item.
25+
*
26+
* @throws InvalidArgumentException
27+
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
28+
* MUST be thrown.
29+
*
30+
* @return CacheItemInterface
31+
* The corresponding Cache Item.
32+
*/
33+
public function getItem($key);
34+
35+
/**
36+
* Returns a traversable set of cache items.
37+
*
38+
* @param array $keys
39+
* An indexed array of keys of items to retrieve.
40+
*
41+
* @throws InvalidArgumentException
42+
* If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
43+
* MUST be thrown.
44+
*
45+
* @return array|\Traversable
46+
* A traversable collection of Cache Items keyed by the cache keys of
47+
* each item. A Cache item will be returned for each key, even if that
48+
* key is not found. However, if no keys are specified then an empty
49+
* traversable MUST be returned instead.
50+
*/
51+
public function getItems(array $keys = array());
52+
53+
/**
54+
* Confirms if the cache contains specified cache item.
55+
*
56+
* Note: This method MAY avoid retrieving the cached value for performance reasons.
57+
* This could result in a race condition with CacheItemInterface::get(). To avoid
58+
* such situation use CacheItemInterface::isHit() instead.
59+
*
60+
* @param string $key
61+
* The key for which to check existence.
62+
*
63+
* @throws InvalidArgumentException
64+
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
65+
* MUST be thrown.
66+
*
67+
* @return bool
68+
* True if item exists in the cache, false otherwise.
69+
*/
70+
public function hasItem($key);
71+
72+
/**
73+
* Deletes all items in the pool.
74+
*
75+
* @return bool
76+
* True if the pool was successfully cleared. False if there was an error.
77+
*/
78+
public function clear();
79+
80+
/**
81+
* Removes the item from the pool.
82+
*
83+
* @param string $key
84+
* The key for which to delete
85+
*
86+
* @throws InvalidArgumentException
87+
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
88+
* MUST be thrown.
89+
*
90+
* @return bool
91+
* True if the item was successfully removed. False if there was an error.
92+
*/
93+
public function deleteItem($key);
94+
95+
/**
96+
* Removes multiple items from the pool.
97+
*
98+
* @param array $keys
99+
* An array of keys that should be removed from the pool.
100+
101+
* @throws InvalidArgumentException
102+
* If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
103+
* MUST be thrown.
104+
*
105+
* @return bool
106+
* True if the items were successfully removed. False if there was an error.
107+
*/
108+
public function deleteItems(array $keys);
109+
110+
/**
111+
* Persists a cache item immediately.
112+
*
113+
* @param CacheItemInterface $item
114+
* The cache item to save.
115+
*
116+
* @return bool
117+
* True if the item was successfully persisted. False if there was an error.
118+
*/
119+
public function save(CacheItemInterface $item);
120+
121+
/**
122+
* Sets a cache item to be persisted later.
123+
*
124+
* @param CacheItemInterface $item
125+
* The cache item to save.
126+
*
127+
* @return bool
128+
* False if the item could not be queued or if a commit was attempted and failed. True otherwise.
129+
*/
130+
public function saveDeferred(CacheItemInterface $item);
131+
132+
/**
133+
* Persists any deferred cache items.
134+
*
135+
* @return bool
136+
* True if all not-yet-saved items were successfully saved or there were none. False otherwise.
137+
*/
138+
public function commit();
139+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Psr\Cache;
4+
5+
/**
6+
* Exception interface for invalid cache arguments.
7+
*
8+
* Any time an invalid argument is passed into a method it must throw an
9+
* exception class which implements Psr\Cache\InvalidArgumentException.
10+
*/
11+
interface InvalidArgumentException extends CacheException
12+
{
13+
}

examples/legacy.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
// Include phpFastCache autoloader
1818
require __DIR__ . '/../src/autoload.php';
1919

20-
// Not provided by phpFastCache
21-
// you have to do it yourself
22-
require __DIR__ . 'YOUR_PROJECT/fig_cache_interfaces.php';
23-
2420
$InstanceCache = CacheManager::getInstance('memcache');
2521

2622
/**

src/autoload.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414

1515
define('PFC_PHP_EXT', 'php');
16+
define('PFC_BIN_DIR', __DIR__ . '../bin/legacy/');
1617

1718
/**
1819
* Register Autoload
@@ -26,10 +27,15 @@
2627
*/
2728
return;
2829
} else if (strpos($entity, 'Psr\Cache') === 0) {
29-
trigger_error('If you cannot use <b>composer</b>, you have to include manually the Psr\\Cache interfaces.<br />See: https://github.com/php-fig/cache/tree/master/src<br /> Called ' . $entity,
30-
E_USER_ERROR);
30+
$entity = str_replace('\\', '/', $entity);
31+
$path = PFC_BIN_DIR . $entity . '.' . PFC_PHP_EXT;
3132

32-
return;
33+
if (is_readable($path)) {
34+
require_once $path;
35+
}else{
36+
trigger_error('Cannot locate the Psr/Cache files', E_USER_ERROR);
37+
return;
38+
}
3339
}
3440

3541
$entity = str_replace('\\', '/', $entity);

tests/Autoload.test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
if (!class_exists('phpFastCache\CacheManager')) {
1616
echo "[FAIL] Autoload failed to find the CacheManager\n";
1717
$status = 255;
18+
}else{
19+
echo "[PASS] Autoload successfully found the CacheManager\n";
1820
}
19-
echo "[PASS] Autoload successfully found the CacheManager\n";
2021

2122
exit($status);

0 commit comments

Comments
 (0)