1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/manager Module. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Puli\Manager\Cache; |
13
|
|
|
|
14
|
|
|
use Puli\Manager\Api\Cache\CacheFile; |
15
|
|
|
use Puli\Manager\Api\Cache\CacheManager; |
16
|
|
|
use Puli\Manager\Api\Config\Config; |
17
|
|
|
use Puli\Manager\Api\Context\ProjectContext; |
18
|
|
|
use Puli\Manager\Api\Module\Module; |
19
|
|
|
use Puli\Manager\Api\Module\ModuleList; |
20
|
|
|
use Puli\Manager\Api\Module\ModuleFile; |
21
|
|
|
use Puli\Manager\Api\Module\ModuleManager; |
22
|
|
|
use Puli\Manager\Api\Module\RootModule; |
23
|
|
|
use Puli\Manager\Assert\Assert; |
24
|
|
|
use Webmozart\Expression\Expr; |
25
|
|
|
use Webmozart\Expression\Expression; |
26
|
|
|
use Webmozart\PathUtil\Path; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Manages cached Modules information. |
30
|
|
|
* |
31
|
|
|
* @since 1.0 |
32
|
|
|
* |
33
|
|
|
* @author Mateusz Sojda <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class CacheManagerImpl implements CacheManager |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var ModuleManager |
39
|
|
|
*/ |
40
|
|
|
private $moduleManager; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var CacheFileStorage |
44
|
|
|
*/ |
45
|
|
|
private $storage; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ProjectContext |
49
|
|
|
*/ |
50
|
|
|
private $context; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $rootDir; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Creates new cache manager. |
59
|
|
|
* |
60
|
|
|
* @param ModuleManager $moduleManager The Module manager. |
61
|
|
|
* @param CacheFileStorage $storage The file storage. |
62
|
|
|
* @param ProjectContext $context Project context. |
63
|
|
|
*/ |
64
|
15 |
|
public function __construct(ModuleManager $moduleManager, CacheFileStorage $storage, ProjectContext $context) |
65
|
|
|
{ |
66
|
15 |
|
$this->moduleManager = $moduleManager; |
67
|
15 |
|
$this->storage = $storage; |
68
|
15 |
|
$this->context = $context; |
69
|
15 |
|
$this->rootDir = $context->getRootDirectory(); |
70
|
15 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
13 |
|
public function getContext() |
76
|
|
|
{ |
77
|
13 |
|
return $this->context; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
7 |
|
public function getCacheFile() |
84
|
|
|
{ |
85
|
7 |
|
$path = $this->getContext()->getConfig()->get(Config::CACHE_FILE); |
86
|
7 |
|
$path = Path::makeAbsolute($path, $this->rootDir); |
87
|
|
|
|
88
|
7 |
|
if (false == $this->storage->fileExists($path)) { |
|
|
|
|
89
|
6 |
|
$this->refreshCacheFile(); |
90
|
|
|
} |
91
|
|
|
|
92
|
7 |
|
$cacheFile = $this->storage->loadCacheFile($path); |
93
|
|
|
|
94
|
7 |
|
return $cacheFile; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
10 |
|
public function refreshCacheFile() |
101
|
|
|
{ |
102
|
10 |
|
$path = $this->getContext()->getConfig()->get(Config::CACHE_FILE); |
103
|
10 |
|
$path = Path::makeAbsolute($path, $this->rootDir); |
104
|
|
|
|
105
|
10 |
|
if ($this->storage->fileExists($path) && false === $this->isRootModuleFileModified()) { |
106
|
1 |
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
9 |
|
$cacheFile = new CacheFile(Path::makeAbsolute($path, $this->rootDir)); |
110
|
|
|
|
111
|
9 |
|
foreach ($this->getInstalledModules() as $module) { |
112
|
9 |
|
$moduleFile = $module->getModuleFile(); |
113
|
9 |
|
if (false === $moduleFile instanceof ModuleFile) { |
114
|
|
|
continue; |
115
|
|
|
} |
116
|
|
|
|
117
|
9 |
|
$cacheFile->addModuleFile($moduleFile); |
118
|
|
|
|
119
|
9 |
|
$installInfo = $module->getInstallInfo(); |
120
|
9 |
|
$cacheFile->addInstallInfo($installInfo); |
121
|
|
|
} |
122
|
|
|
|
123
|
9 |
|
$this->storage->saveCacheFile($cacheFile); |
124
|
9 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
2 |
|
public function getModule($name) |
130
|
|
|
{ |
131
|
2 |
|
Assert::string($name, 'The Module name must be a string. Got: %s'); |
132
|
|
|
|
133
|
1 |
|
$cacheFile = $this->getCacheFile(); |
134
|
1 |
|
$moduleFile = $cacheFile->getModuleFile($name); |
135
|
|
|
|
136
|
1 |
|
return $this->buildModule($moduleFile, $cacheFile); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
4 |
|
public function getRootModule() |
143
|
|
|
{ |
144
|
4 |
|
return new RootModule( |
145
|
4 |
|
$this->getContext()->getRootModuleFile(), |
146
|
4 |
|
$this->rootDir |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
2 |
|
public function getModules() |
154
|
|
|
{ |
155
|
2 |
|
$cacheFile = $this->getCacheFile(); |
156
|
2 |
|
$modules = new ModuleList(); |
157
|
|
|
|
158
|
2 |
|
foreach ($cacheFile->getModuleFiles() as $moduleFile) { |
159
|
2 |
|
$module = $this->buildModule($moduleFile, $cacheFile); |
160
|
2 |
|
$modules->add($module); |
161
|
|
|
} |
162
|
|
|
|
163
|
2 |
|
return $modules; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
1 |
|
public function findModules(Expression $expr) |
170
|
|
|
{ |
171
|
1 |
|
$cacheFile = $this->getCacheFile(); |
172
|
1 |
|
$modules = new ModuleList(); |
173
|
|
|
|
174
|
1 |
|
foreach ($cacheFile->getModuleFiles() as $moduleFile) { |
175
|
1 |
|
$module = $this->buildModule($moduleFile, $cacheFile); |
176
|
|
|
|
177
|
1 |
|
if ($expr->evaluate($module)) { |
178
|
1 |
|
$modules->add($module); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
return $modules; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
2 |
|
public function hasModule($name) |
189
|
|
|
{ |
190
|
2 |
|
Assert::string($name, 'The Module name must be a string. Got: %s'); |
191
|
|
|
|
192
|
1 |
|
$cacheFile = $this->getCacheFile(); |
193
|
|
|
|
194
|
1 |
|
return $cacheFile->hasModuleFile($name); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
1 |
|
public function hasModules(Expression $expr = null) |
201
|
|
|
{ |
202
|
1 |
|
$cacheFile = $this->getCacheFile(); |
203
|
|
|
|
204
|
1 |
|
if (!$expr) { |
205
|
1 |
|
return $cacheFile->hasModuleFiles(); |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
foreach ($this->getModules() as $module) { |
209
|
1 |
|
if ($expr->evaluate($module)) { |
210
|
1 |
|
return true; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
1 |
|
return false; |
215
|
|
|
} |
216
|
|
|
|
217
|
9 |
|
private function getInstalledModules() |
|
|
|
|
218
|
|
|
{ |
219
|
9 |
|
$modules = $this->moduleManager->findModules(Expr::true()); |
220
|
|
|
|
221
|
9 |
|
return $modules->getInstalledModules(); |
222
|
|
|
} |
223
|
|
|
|
224
|
4 |
|
private function buildModule(ModuleFile $moduleFile, CacheFile $cacheFile) |
225
|
|
|
{ |
226
|
4 |
|
$installInfo = $cacheFile->getInstallInfo($moduleFile->getModuleName()); |
227
|
4 |
|
$installPath = Path::makeAbsolute($installInfo->getInstallPath(), $this->rootDir); |
228
|
|
|
|
229
|
4 |
|
return new Module($moduleFile, $installPath, $installInfo); |
230
|
|
|
} |
231
|
|
|
|
232
|
3 |
|
private function isRootModuleFileModified() |
233
|
|
|
{ |
234
|
3 |
|
$cacheFilePath = $this->getContext()->getConfig()->get(Config::CACHE_FILE); |
235
|
3 |
|
$cacheFilePath = Path::makeAbsolute($cacheFilePath, $this->rootDir); |
236
|
|
|
|
237
|
3 |
|
clearstatcache(true, $cacheFilePath); |
238
|
3 |
|
$cacheFileMtime = filemtime($cacheFilePath); |
239
|
|
|
|
240
|
3 |
|
$rootModuleFilePath = $this->getRootModule()->getModuleFile()->getPath(); |
241
|
|
|
|
242
|
3 |
|
if (false === $this->storage->fileExists($rootModuleFilePath)) { |
243
|
1 |
|
return true; |
244
|
|
|
} |
245
|
|
|
|
246
|
2 |
|
clearstatcache(true, $rootModuleFilePath); |
247
|
2 |
|
$rootModuleFileMtime = filemtime($rootModuleFilePath); |
248
|
|
|
|
249
|
2 |
|
if ($rootModuleFileMtime > $cacheFileMtime) { |
|
|
|
|
250
|
1 |
|
return true; |
251
|
|
|
} |
252
|
|
|
|
253
|
1 |
|
return false; |
254
|
|
|
} |
255
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.