1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Storage\Drivers; |
11
|
|
|
|
12
|
|
|
use Psr\Cache\CacheException; |
13
|
|
|
use Psr\Cache\CacheItemInterface; |
14
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
15
|
|
|
use Psr\Cache\InvalidArgumentException; |
16
|
|
|
use Railt\Io\Readable; |
17
|
|
|
use Railt\Storage\Storage; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Psr6Storage |
21
|
|
|
*/ |
22
|
|
|
class Psr6Storage implements Storage |
23
|
|
|
{ |
24
|
|
|
private const DEFAULT_REMEMBER_TIME = 60 * 5; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $timeout; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var CacheItemPoolInterface |
33
|
|
|
*/ |
34
|
|
|
private $pool; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Closure |
38
|
|
|
*/ |
39
|
|
|
private $persist; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Psr6Storage constructor. |
43
|
|
|
* @param CacheItemPoolInterface $pool |
44
|
|
|
* @param \Closure $persist |
45
|
|
|
* @param int $timeout |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
CacheItemPoolInterface $pool, |
49
|
|
|
\Closure $persist, |
50
|
|
|
int $timeout = self::DEFAULT_REMEMBER_TIME |
51
|
|
|
) { |
52
|
|
|
$this->pool = $pool; |
53
|
|
|
$this->persist = $persist; |
54
|
|
|
$this->timeout = $timeout; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Readable $readable |
59
|
|
|
* @param \Closure $then |
60
|
|
|
* @return object|mixed |
61
|
|
|
* @throws \InvalidArgumentException |
62
|
|
|
* @throws InvalidArgumentException |
63
|
|
|
* @throws \Exception |
64
|
|
|
*/ |
65
|
|
|
public function remember(Readable $readable, \Closure $then) |
66
|
|
|
{ |
67
|
|
|
$exists = $this->pool->hasItem($readable->getHash()); |
68
|
|
|
|
69
|
|
|
if ($exists) { |
70
|
|
|
return $this->restore($readable); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->store($readable, $then($readable)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Readable $readable |
78
|
|
|
* @return object|mixed |
79
|
|
|
* @throws \InvalidArgumentException |
80
|
|
|
*/ |
81
|
|
|
private function restore(Readable $readable) |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
|
|
$result = $this->pool->getItem($readable->getHash()); |
85
|
|
|
|
86
|
|
|
return $this->touch($result)->get(); |
87
|
|
|
} catch (InvalidArgumentException | \Throwable $fatal) { |
88
|
|
|
throw new \InvalidArgumentException($fatal->getMessage(), 0, $fatal); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param CacheItemInterface $item |
94
|
|
|
* @return CacheItemInterface |
95
|
|
|
*/ |
96
|
|
|
private function touch(CacheItemInterface $item): CacheItemInterface |
97
|
|
|
{ |
98
|
|
|
return $item->expiresAfter(\time() + $this->timeout); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Readable $readable |
103
|
|
|
* @param mixed $data |
104
|
|
|
* @return mixed |
105
|
|
|
* @throws \Exception |
106
|
|
|
*/ |
107
|
|
|
private function store(Readable $readable, $data) |
108
|
|
|
{ |
109
|
|
|
try { |
110
|
|
|
/** @var CacheItemInterface $item */ |
111
|
|
|
$item = ($this->persist)($readable, $data); |
112
|
|
|
$this->touch($item); |
113
|
|
|
$this->pool->save($item); |
114
|
|
|
} catch (CacheException $error) { |
115
|
|
|
throw $error->getPrevious(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $data; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.