10
10
abstract class CacheState
11
11
{
12
12
public const TYPE = 'type ' ;
13
+ public const DATA = 'data ' ;
13
14
14
15
public const ORIGINAL_FILE_PATH_KEY = 'originalFilePath ' ;
16
+ public const NAMESPACED_CLASS_KEY = 'namespacedClass ' ;
15
17
public const MODIFICATION_TIME_KEY = 'modificationTime ' ;
16
18
17
19
public string $ originalFilePath ;
18
- public int $ modificationTime ;
20
+ protected string $ namespacedClass ;
21
+ protected int $ modificationTime ;
19
22
20
23
/**
21
24
* CacheState constructor.
@@ -80,18 +83,39 @@ public function getRequiredKeys(): array
80
83
{
81
84
return [
82
85
static ::ORIGINAL_FILE_PATH_KEY ,
86
+ static ::NAMESPACED_CLASS_KEY ,
83
87
static ::MODIFICATION_TIME_KEY ,
84
88
];
85
89
}
86
90
91
+ /**
92
+ * Create a cache state if it is valid.
93
+ *
94
+ * @param array $cacheStateArray
95
+ *
96
+ * @return self|null
97
+ */
98
+ public function createIfValid (array $ cacheStateArray ): ?CacheState
99
+ {
100
+ if (!$ this ->valid ($ cacheStateArray )) {
101
+ // @codeCoverageIgnoreStart
102
+ return null ;
103
+ // @codeCoverageIgnoreEnd
104
+ }
105
+
106
+ $ this ->setData ($ cacheStateArray );
107
+
108
+ return $ this ;
109
+ }
110
+
87
111
/**
88
112
* Validate the cache state.
89
113
*
90
114
* @param array<string, (string|int|string[])> $cacheStateArray
91
115
*
92
116
* @return bool
93
117
*/
94
- public function valid (array $ cacheStateArray ): bool
118
+ private function valid (array $ cacheStateArray ): bool
95
119
{
96
120
// Check if all required keys are present
97
121
foreach ($ this ->getRequiredKeys () as $ requiredKey ) {
@@ -110,7 +134,7 @@ public function valid(array $cacheStateArray): bool
110
134
*
111
135
* @param array<string, (string|int|string[])> $cacheStateArray
112
136
*/
113
- public function setData (array $ cacheStateArray ): void
137
+ private function setData (array $ cacheStateArray ): void
114
138
{
115
139
foreach ($ cacheStateArray as $ key => $ value ) {
116
140
$ this ->{$ key } = $ value ;
@@ -124,6 +148,12 @@ public function setData(array $cacheStateArray): void
124
148
*/
125
149
public function isFresh (): bool
126
150
{
151
+ if (!file_exists ($ this ->originalFilePath )) {
152
+ // @codeCoverageIgnoreStart
153
+ return false ;
154
+ // @codeCoverageIgnoreEnd
155
+ }
156
+
127
157
if (filemtime ($ this ->originalFilePath ) > $ this ->modificationTime ) {
128
158
return false ;
129
159
}
@@ -137,161 +167,4 @@ public function isFresh(): bool
137
167
* @return string|null
138
168
*/
139
169
abstract public function getFilePath (): ?string ;
140
-
141
- // /**
142
- // * CacheState constructor.
143
- // *
144
- // * @param string $originalFilePath
145
- // * @param string $className
146
- // * @param string|null $cachedFilePath
147
- // * @param int|null $transformedTime
148
- // * @param string[]|null $transformerFilePaths
149
- // */
150
- // public function __construct(
151
- // public string $originalFilePath,
152
- // public string $className,
153
- // public ?string $cachedFilePath,
154
- // public ?int $transformedTime,
155
- // public ?array $transformerFilePaths,
156
- // ) {}
157
- //
158
- // /**
159
- // * Use the cached file path if aspects have been applied.
160
- // * Otherwise, use the original file path if no aspects have been applied.
161
- // *
162
- // * @return string
163
- // */
164
- // public function getFilePath(): string
165
- // {
166
- // return $this->cachedFilePath ?? $this->originalFilePath;
167
- // }
168
- //
169
- //
170
- //
171
- //
172
- // /**
173
- // * Get the cache state as an array.
174
- // *
175
- // * @return array
176
- // */
177
- // public function toArray(): array
178
- // {
179
- // return [
180
- // $this->originalFilePath,
181
- // $this->className,
182
- // $this->cachedFilePath,
183
- // $this->transformedTime,
184
- // $this->transformerFilePaths,
185
- // ];
186
- // }
187
- //
188
- // /**
189
- // * Check if the cache is not outdated.
190
- // *
191
- // * @return bool
192
- // */
193
- // public function isFresh(): bool
194
- // {
195
- // // @codeCoverageIgnoreStart
196
- // // This should only happen if the project is misconfigured
197
- // if ($this->checkInfiniteLoop()) {
198
- // return false;
199
- // }
200
- // // @codeCoverageIgnoreEnd
201
- //
202
- // $allFiles = array_merge(
203
- // [$this->originalFilePath],
204
- // $this->transformerFilePaths,
205
- // );
206
- //
207
- // if ($this->checkFilesModified($allFiles)) {
208
- // return false;
209
- // }
210
- //
211
- // if ($this->cachedFilePath) {
212
- // $allFiles[] = $this->cachedFilePath;
213
- // }
214
- //
215
- // if (!$this->checkFilesExist($allFiles)) {
216
- // return false;
217
- // }
218
- //
219
- // if (!$this->checkTransformerCount()) {
220
- // return false;
221
- // }
222
- //
223
- // return true;
224
- // }
225
- //
226
- // /**
227
- // * Check if the cache is in an infinite loop.
228
- // *
229
- // * @return bool True if the cache is in an infinite loop
230
- // */
231
- // protected function checkInfiniteLoop(): bool
232
- // {
233
- // if ($this->cachedFilePath !== null) {
234
- // // Same original file and cached file
235
- // if ($this->originalFilePath === $this->cachedFilePath) {
236
- // return true;
237
- // }
238
- // }
239
- //
240
- // return false;
241
- // }
242
- //
243
- // /**
244
- // * Check if the files have been modified.
245
- // *
246
- // * @param string[] $files
247
- // *
248
- // * @return bool True if any file has been modified
249
- // */
250
- // protected function checkFilesModified(array $files): bool
251
- // {
252
- // $lastModified = max(array_map('filemtime', $files));
253
- // if ($lastModified >= $this->transformedTime) {
254
- // return true;
255
- // }
256
- //
257
- // return false;
258
- // }
259
- //
260
- // /**
261
- // * Check if the files exist.
262
- // *
263
- // * @param string[] $files
264
- // *
265
- // * @return bool True if all files exist
266
- // */
267
- // protected function checkFilesExist(array $files): bool
268
- // {
269
- // // Check if the cache file exists
270
- // foreach ($files as $file) {
271
- // if (!file_exists($file)) {
272
- // return false;
273
- // }
274
- // }
275
- //
276
- // return true;
277
- // }
278
- //
279
- // /**
280
- // * Check if the transformer count is the same.
281
- // *
282
- // * @return bool True if the count is the same
283
- // */
284
- // protected function checkTransformerCount(): bool
285
- // {
286
- // // Checking the count alone should be enough
287
- // $cachedTransformerCount = count($this->transformerFilePaths);
288
- // $currentTransformerCount = count(
289
- // $this->transformerMatcher->match($this->className),
290
- // );
291
- // if ($cachedTransformerCount !== $currentTransformerCount) {
292
- // return false;
293
- // }
294
- //
295
- // return true;
296
- // }
297
170
}
0 commit comments