|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL, Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/addendum |
|
7
|
|
|
* @licence AGPL, Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes) |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes) |
|
10
|
|
|
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser) |
|
11
|
|
|
* @link https://maslosoft.com/addendum/ - maslosoft addendum |
|
12
|
|
|
* @link https://code.google.com/p/addendum/ - original addendum project |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Maslosoft\Addendum\Cache; |
|
16
|
|
|
|
|
17
|
|
|
use Maslosoft\Addendum\Addendum; |
|
18
|
|
|
use Maslosoft\Addendum\Helpers\SoftIncluder; |
|
19
|
|
|
use Maslosoft\Addendum\Options\MetaOptions; |
|
20
|
|
|
use Maslosoft\Cli\Shared\Helpers\PhpExporter; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Meta Namespaces cache |
|
24
|
|
|
* |
|
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
26
|
|
|
*/ |
|
27
|
|
|
class NsCache |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
public const FileName = '_ns.php'; |
|
31
|
|
|
|
|
32
|
|
|
private $file = ''; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Addendum instance |
|
36
|
|
|
* @var Addendum |
|
37
|
|
|
*/ |
|
38
|
|
|
private $ad = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* @var |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $nsCache = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Hash map of namespaces. |
|
48
|
|
|
* Namespaces is key, while value is boolean and is not really relevant |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
private $namespaces; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Option holder |
|
55
|
|
|
* @var MetaOptions |
|
56
|
|
|
*/ |
|
57
|
|
|
private $options = null; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @internal Flag that will trigger cache validity check for namespaces cache |
|
61
|
|
|
* But only if no options provided |
|
62
|
|
|
* @var bool |
|
63
|
|
|
*/ |
|
64
|
|
|
public static $addedNs = true; |
|
65
|
|
|
|
|
66
|
61 |
|
public function __construct($path, Addendum $addendum, MetaOptions $options = null) |
|
67
|
|
|
{ |
|
68
|
61 |
|
$this->file = sprintf('%s/%s', $path, self::FileName); |
|
69
|
61 |
|
$this->namespaces = $addendum->nameKeys; |
|
70
|
61 |
|
$this->ad = $addendum; |
|
71
|
61 |
|
$this->setOptions($options); |
|
72
|
61 |
|
} |
|
73
|
|
|
|
|
74
|
61 |
|
public function setOptions(MetaOptions $options = null) |
|
75
|
|
|
{ |
|
76
|
61 |
|
if ($options !== null && !empty($options->namespaces)) |
|
77
|
|
|
{ |
|
78
|
5 |
|
foreach ($options->namespaces as $ns) |
|
79
|
|
|
{ |
|
80
|
5 |
|
if (empty($this->namespaces[$ns])) |
|
81
|
|
|
{ |
|
82
|
1 |
|
self::$addedNs = true; |
|
83
|
5 |
|
$this->namespaces[$ns] = true; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
61 |
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
public function valid() |
|
90
|
|
|
{ |
|
91
|
2 |
|
$ns = $this->get(); |
|
92
|
2 |
|
$valid = $this->isValid($ns); |
|
93
|
|
|
|
|
94
|
|
|
// Override existing cache if not valid |
|
95
|
2 |
|
if (!$valid) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->set(); |
|
98
|
|
|
} |
|
99
|
2 |
|
return $valid; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
public function get() |
|
103
|
|
|
{ |
|
104
|
2 |
|
if (!empty(self::$nsCache[$this->file])) |
|
105
|
|
|
{ |
|
106
|
2 |
|
return self::$nsCache[$this->file]; |
|
107
|
|
|
} |
|
108
|
1 |
|
self::$nsCache[$this->file] = SoftIncluder::includeFile($this->file); |
|
109
|
1 |
|
return self::$nsCache[$this->file]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
54 |
|
public function set() |
|
113
|
|
|
{ |
|
114
|
54 |
|
$ns = []; |
|
115
|
54 |
|
foreach (array_keys($this->namespaces) as $name) |
|
116
|
|
|
{ |
|
117
|
54 |
|
$ns[$name] = true; |
|
118
|
|
|
} |
|
119
|
54 |
|
$data = PhpExporter::export($ns); |
|
120
|
54 |
|
$mask = umask(0); |
|
121
|
54 |
|
file_put_contents($this->file, $data); |
|
122
|
54 |
|
umask($mask); |
|
123
|
54 |
|
self::$nsCache[$this->file] = $ns; |
|
124
|
54 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function remove() |
|
127
|
|
|
{ |
|
128
|
|
|
unset(self::$nsCache[$this->file]); |
|
129
|
|
|
if (file_exists($this->file)) |
|
130
|
|
|
{ |
|
131
|
|
|
unlink($this->file); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
2 |
|
private function isValid($ns) |
|
136
|
|
|
{ |
|
137
|
|
|
// Fresh data |
|
138
|
2 |
|
if (empty($ns)) |
|
139
|
|
|
{ |
|
140
|
1 |
|
return true; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Additional check if added namespace manually |
|
144
|
2 |
|
if (empty($this->options) && self::$addedNs) |
|
145
|
|
|
{ |
|
146
|
2 |
|
$addendumDiff = array_diff_key($this->ad->nameKeys, $this->namespaces); |
|
147
|
2 |
|
if (!empty($addendumDiff)) |
|
148
|
|
|
{ |
|
149
|
|
|
// Add missing namespaces to cache them |
|
150
|
|
|
foreach (array_keys($addendumDiff) as $nsName) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->namespaces[$nsName] = true; |
|
153
|
|
|
} |
|
154
|
|
|
self::$addedNs = false; |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
// Check if has all namespaces |
|
159
|
2 |
|
$cachedDiff = array_diff_key($this->namespaces, $ns); |
|
160
|
2 |
|
if (empty($cachedDiff)) |
|
161
|
|
|
{ |
|
162
|
2 |
|
self::$addedNs = false; |
|
163
|
2 |
|
return true; |
|
164
|
|
|
} |
|
165
|
|
|
return false; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|