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 http://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
|
|
|
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 $addeNs = true; |
65
|
|
|
|
66
|
56 |
|
public function __construct($path, Addendum $addendum, MetaOptions $options = null) |
67
|
|
|
{ |
68
|
56 |
|
$this->file = sprintf('%s/%s', $path, self::FileName); |
69
|
56 |
|
$this->namespaces = $addendum->nameKeys; |
70
|
56 |
|
$this->ad = $addendum; |
71
|
56 |
|
$this->setOptions($options); |
72
|
56 |
|
} |
73
|
|
|
|
74
|
56 |
|
public function setOptions(MetaOptions $options = null) |
75
|
|
|
{ |
76
|
56 |
|
if (!empty($options) && !empty($options->namespaces)) |
77
|
56 |
|
{ |
78
|
5 |
|
foreach ($options->namespaces as $ns) |
79
|
|
|
{ |
80
|
5 |
|
if (empty($this->namespaces[$ns])) |
81
|
5 |
|
{ |
82
|
2 |
|
self::$addeNs = true; |
83
|
2 |
|
$this->namespaces[$ns] = true; |
84
|
2 |
|
} |
85
|
5 |
|
} |
86
|
5 |
|
} |
87
|
56 |
|
} |
88
|
|
|
|
89
|
2 |
|
public function valid() |
90
|
|
|
{ |
91
|
2 |
|
$ns = $this->get(); |
92
|
2 |
|
$valid = $this->isValid($ns); |
93
|
|
|
|
94
|
|
|
// Ovverride existing cache if not valid |
95
|
2 |
|
if (!$valid) |
96
|
2 |
|
{ |
97
|
1 |
|
$this->set(); |
98
|
1 |
|
} |
99
|
2 |
|
return $valid; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
public function get() |
103
|
|
|
{ |
104
|
2 |
|
if (!empty(self::$nsCache[$this->file])) |
105
|
2 |
|
{ |
106
|
2 |
|
return self::$nsCache[$this->file]; |
107
|
|
|
} |
108
|
|
|
self::$nsCache[$this->file] = SoftIncluder::includeFile($this->file); |
109
|
|
|
return self::$nsCache[$this->file]; |
110
|
|
|
} |
111
|
|
|
|
112
|
52 |
|
public function set() |
113
|
|
|
{ |
114
|
52 |
|
$ns = []; |
115
|
52 |
|
foreach (array_keys($this->namespaces) as $name) |
116
|
|
|
{ |
117
|
52 |
|
$ns[$name] = true; |
118
|
52 |
|
} |
119
|
52 |
|
$data = PhpExporter::export($ns); |
120
|
52 |
|
$mask = umask(0); |
121
|
52 |
|
file_put_contents($this->file, $data); |
122
|
52 |
|
umask($mask); |
123
|
52 |
|
self::$nsCache[$this->file] = $ns; |
124
|
52 |
|
} |
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
|
2 |
|
{ |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Additional check if added namespace manually |
144
|
2 |
|
if (empty($this->options) && self::$addeNs) |
145
|
2 |
|
{ |
146
|
2 |
|
$addendumDiff = array_diff_key($this->ad->nameKeys, $this->namespaces); |
147
|
2 |
|
if (!empty($addendumDiff)) |
148
|
2 |
|
{ |
149
|
|
|
// Add missing namespaces to cache them |
150
|
1 |
|
foreach (array_keys($addendumDiff) as $ns) |
151
|
|
|
{ |
152
|
1 |
|
$this->namespaces[$ns] = true; |
153
|
1 |
|
} |
154
|
1 |
|
self::$addeNs = false; |
155
|
1 |
|
return false; |
156
|
|
|
} |
157
|
2 |
|
} |
158
|
|
|
// Check if has all namespaces |
159
|
2 |
|
$cachedDiff = array_diff_key($this->namespaces, $ns); |
160
|
2 |
|
if (empty($cachedDiff)) |
161
|
2 |
|
{ |
162
|
2 |
|
self::$addeNs = false; |
163
|
2 |
|
return true; |
164
|
|
|
} |
165
|
1 |
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|