1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace steevanb\DevBundle\Cache; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
6
|
|
|
use Symfony\Component\Finder\Finder; |
7
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
8
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
9
|
|
|
|
10
|
|
|
class SessionCache extends CacheProvider |
11
|
|
|
{ |
12
|
|
|
/** @var SessionInterface */ |
13
|
|
|
protected $session; |
14
|
|
|
|
15
|
|
|
/** @var KernelInterface */ |
16
|
|
|
protected $kernel; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
protected $mappingPaths = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param SessionInterface $session |
23
|
|
|
* @param KernelInterface $kernel |
24
|
|
|
*/ |
25
|
|
|
public function __construct(SessionInterface $session, KernelInterface $kernel) |
26
|
|
|
{ |
27
|
|
|
$this->session = $session; |
28
|
|
|
$this->kernel = $kernel; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $path |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function addPathToScan($path) |
36
|
|
|
{ |
37
|
|
|
if (in_array($path, $this->mappingPaths) === false) { |
38
|
|
|
$this->mappingPaths[] = $path; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $bundle |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function addBundleToScan($bundle) |
49
|
|
|
{ |
50
|
|
|
$path = $this->kernel->getBundle($bundle)->getPath(); |
51
|
|
|
$path .= DIRECTORY_SEPARATOR . 'Resources'; |
52
|
|
|
$path .= DIRECTORY_SEPARATOR . 'config'; |
53
|
|
|
$path .= DIRECTORY_SEPARATOR . 'doctrine'; |
54
|
|
|
$this->addPathToScan($path); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function refresh() |
63
|
|
|
{ |
64
|
|
|
$lastRefresh = $this->getLastRefresh(); |
65
|
|
|
if ($lastRefresh === null) { |
66
|
|
|
$this->flushAll(); |
67
|
|
|
|
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
$lastRefreshTimestamp = $lastRefresh->format('U'); |
71
|
|
|
|
72
|
|
|
foreach ($this->mappingPaths as $path) { |
73
|
|
|
if (is_dir($path)) { |
74
|
|
|
$finder = new Finder(); |
75
|
|
|
foreach ($finder->in($path)->files() as $file) { |
76
|
|
|
if (filemtime($file) >= $lastRefreshTimestamp) { |
77
|
|
|
$this->flushAll(); |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function defineLastRefresh() |
92
|
|
|
{ |
93
|
|
|
$this->session->set('session-cache-last-refresh', new \DateTime()); |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return \DateTime|null |
100
|
|
|
*/ |
101
|
|
|
public function getLastRefresh() |
102
|
|
|
{ |
103
|
|
|
return $this->session->get('session-cache-last-refresh'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $id |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function getSessionId($id) |
111
|
|
|
{ |
112
|
|
|
return 'session-cache_' . $id; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
protected function doFetch($id) |
119
|
|
|
{ |
120
|
|
|
return $this->session->get($this->getSessionId($id), false); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
protected function doContains($id) |
127
|
|
|
{ |
128
|
|
|
return $this->session->has($this->getSessionId($id)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
protected function doSave($id, $data, $lifeTime = 0) |
135
|
|
|
{ |
136
|
|
|
$this->session->set($this->getSessionId($id), $data); |
137
|
|
|
|
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
protected function doDelete($id) |
145
|
|
|
{ |
146
|
|
|
$this->session->remove($this->getSessionId($id)); |
147
|
|
|
|
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
protected function doFlush() |
155
|
|
|
{ |
156
|
|
|
$sessionPrefix = $this->getSessionId(null); |
157
|
|
|
$sessionPrefixLength = strlen($sessionPrefix); |
158
|
|
|
foreach (array_keys($this->session->all()) as $name) { |
159
|
|
|
if (substr($name, 0, $sessionPrefixLength) == $sessionPrefix) { |
160
|
|
|
$this->session->remove($name); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
|
|
protected function doGetStats() |
171
|
|
|
{ |
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|