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