1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Maslosoft\Addendum\Cache; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use function get_class; |
8
|
|
|
use function is_object; |
9
|
|
|
use Maslosoft\Addendum\Addendum; |
10
|
|
|
use Maslosoft\Addendum\Cache\PhpCache\Checker; |
11
|
|
|
use Maslosoft\Addendum\Cache\PhpCache\Cleaner; |
12
|
|
|
use Maslosoft\Addendum\Cache\PhpCache\Reader; |
13
|
|
|
use Maslosoft\Addendum\Cache\PhpCache\Writer; |
14
|
|
|
use Maslosoft\Addendum\Helpers\Cacher; |
15
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
16
|
|
|
use Maslosoft\Addendum\Options\MetaOptions; |
17
|
|
|
use Maslosoft\Cli\Shared\ConfigDetector; |
18
|
|
|
use UnexpectedValueException; |
19
|
|
|
|
20
|
|
|
class ClassCache |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private static $runtimePath = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $className = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Root path of storage |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $storagePath = ''; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Base path for current meta class |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $basePath = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $metaClass = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var NsCache |
51
|
|
|
*/ |
52
|
|
|
private $nsCache; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Addendum |
56
|
|
|
*/ |
57
|
|
|
private $addendum; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var Writer |
61
|
|
|
*/ |
62
|
|
|
private $writer; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var Checker |
66
|
|
|
*/ |
67
|
|
|
private $checker; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var Reader |
71
|
|
|
*/ |
72
|
|
|
private $reader; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var Cleaner |
76
|
|
|
*/ |
77
|
|
|
private $cleaner; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* @param string $metaClass |
82
|
|
|
* @param AnnotatedInterface|object|string $component |
83
|
|
|
* @param MetaOptions|Addendum $options |
84
|
|
|
*/ |
85
|
1 |
|
public function __construct(string $metaClass, $component, $options = null) |
86
|
|
|
{ |
87
|
1 |
|
if (null === self::$runtimePath) |
88
|
|
|
{ |
89
|
1 |
|
self::$runtimePath = (new ConfigDetector)->getRuntimePath(); |
90
|
|
|
} |
91
|
1 |
|
$this->storagePath = self::$runtimePath . '/addendum'; |
92
|
1 |
|
$this->metaClass = $metaClass; |
93
|
1 |
|
if(is_object($component)) |
94
|
|
|
{ |
95
|
1 |
|
$component = get_class($component); |
96
|
|
|
} |
97
|
1 |
|
$this->className = $component; |
98
|
1 |
|
$this->setOptions($options); |
|
|
|
|
99
|
|
|
|
100
|
1 |
|
$this->reader = new Reader($this->basePath); |
101
|
1 |
|
$this->writer = new Writer($this->basePath); |
102
|
1 |
|
$this->cleaner = new Cleaner($this->basePath); |
103
|
1 |
|
$this->checker = new Checker($this->basePath); |
104
|
1 |
|
} |
105
|
|
|
|
106
|
37 |
|
public function get($className) |
107
|
|
|
{ |
108
|
37 |
|
assert(!empty($className)); |
109
|
37 |
|
if($this->addendum->checkMTime) |
110
|
|
|
{ |
111
|
|
|
if(!$this->checker->isValid($className)) |
112
|
|
|
{ |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
} |
116
|
37 |
|
$data = $this->reader->read($className); |
117
|
|
|
|
118
|
37 |
|
return $data; |
119
|
|
|
} |
120
|
|
|
|
121
|
30 |
|
public function set($className, $data): bool |
122
|
|
|
{ |
123
|
30 |
|
$this->cleaner->clean($className); |
124
|
30 |
|
return $this->writer->write($className, $data); |
125
|
|
|
} |
126
|
|
|
|
127
|
37 |
|
public function setOptions(MetaOptions $options = null) |
128
|
|
|
{ |
129
|
37 |
|
$instanceId = $this->getInstanceId($options); |
130
|
37 |
|
$this->addendum = Addendum::fly($instanceId); |
131
|
37 |
|
$this->basePath = sprintf('%s/%s@%s', |
132
|
37 |
|
$this->storagePath, |
133
|
37 |
|
Cacher::classToFile($this->metaClass), |
134
|
37 |
|
$instanceId |
135
|
|
|
); |
136
|
37 |
|
$this->nsCache = new NsCache($this->basePath, $this->addendum); |
137
|
37 |
|
$this->nsCache->setOptions($options); |
138
|
37 |
|
} |
139
|
|
|
|
140
|
37 |
|
private function getInstanceId(MetaOptions $options = null) |
141
|
|
|
{ |
142
|
37 |
|
if (empty($options)) |
143
|
|
|
{ |
144
|
33 |
|
$instanceId = Addendum::DefaultInstanceId; |
145
|
|
|
} |
146
|
4 |
|
elseif ($options instanceof Addendum) |
147
|
|
|
{ |
148
|
|
|
$instanceId = $options->getInstanceId(); |
149
|
|
|
} |
150
|
4 |
|
elseif ($options instanceof MetaOptions) |
151
|
|
|
{ |
152
|
4 |
|
$instanceId = $options->instanceId; |
153
|
|
|
} |
154
|
|
|
else |
155
|
|
|
{ |
156
|
|
|
throw new UnexpectedValueException('Unknown options'); |
157
|
|
|
} |
158
|
37 |
|
return $instanceId; |
159
|
|
|
} |
160
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.