|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Maslosoft\Addendum\Cache\PhpCache; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use function array_filter; |
|
8
|
|
|
use function array_sum; |
|
9
|
|
|
use function chmod; |
|
10
|
|
|
use function file_exists; |
|
11
|
|
|
use function file_put_contents; |
|
12
|
|
|
use Maslosoft\Addendum\Helpers\Cacher; |
|
13
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
14
|
|
|
use Maslosoft\Addendum\Utilities\ClassChecker; |
|
15
|
|
|
use Maslosoft\Addendum\Utilities\NameNormalizer; |
|
16
|
|
|
use Maslosoft\Cli\Shared\Helpers\PhpExporter; |
|
17
|
|
|
use Maslosoft\Cli\Shared\Io; |
|
18
|
|
|
use function sprintf; |
|
19
|
|
|
|
|
20
|
|
|
class Writer extends CacheComponent |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
35 |
|
public function write($className, $data): bool |
|
24
|
|
|
{ |
|
25
|
35 |
|
NameNormalizer::normalize($className, false); |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
// Set partials cache |
|
29
|
|
|
$filter = function ($partial) use ($className) { |
|
30
|
34 |
|
$interface = AnnotatedInterface::class; |
|
31
|
34 |
|
NameNormalizer::normalize($interface, false); |
|
32
|
34 |
|
NameNormalizer::normalize($partial, false); |
|
33
|
34 |
|
if ($partial === $interface) |
|
34
|
|
|
{ |
|
35
|
34 |
|
return false; |
|
36
|
|
|
} |
|
37
|
34 |
|
if ($partial === $className) |
|
38
|
|
|
{ |
|
39
|
34 |
|
return false; |
|
40
|
|
|
} |
|
41
|
12 |
|
return true; |
|
42
|
35 |
|
}; |
|
43
|
35 |
|
$partials = array_filter(ClassChecker::getPartials($className), $filter); |
|
44
|
35 |
|
$success = []; |
|
45
|
35 |
|
if (!empty($partials)) |
|
46
|
|
|
{ |
|
47
|
|
|
// Create directory for current class |
|
48
|
|
|
// This directory will hold class names |
|
49
|
|
|
// of partials |
|
50
|
12 |
|
$partialsDir = $this->getPartialsDir($className); |
|
51
|
12 |
|
if (!file_exists($partialsDir)) |
|
52
|
|
|
{ |
|
53
|
12 |
|
Io::mkdir($partialsDir); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
12 |
|
foreach ($partials as $partialClass) |
|
57
|
|
|
{ |
|
58
|
12 |
|
$partialFile = Cacher::classToFile($partialClass); |
|
59
|
|
|
|
|
60
|
|
|
// Write current class name to partial dir |
|
61
|
|
|
// so that it can be retrieved on modification |
|
62
|
|
|
// time check |
|
63
|
12 |
|
$classMarkerFile = sprintf( |
|
64
|
12 |
|
'%s/%s.php', |
|
65
|
12 |
|
$partialsDir, |
|
66
|
12 |
|
$partialFile |
|
67
|
|
|
); |
|
68
|
12 |
|
$success[] = (bool)file_put_contents($classMarkerFile, PhpExporter::export(true)); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
35 |
|
$fileName = $this->getFilename($className); |
|
72
|
35 |
|
$success[] = (bool)file_put_contents($fileName, PhpExporter::export($data)); |
|
73
|
33 |
|
@chmod($fileName, 0666); |
|
|
|
|
|
|
74
|
33 |
|
return array_sum($success) === count($success); |
|
75
|
|
|
} |
|
76
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: