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