|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
|
5
|
|
|
* To change this template file, choose Tools | Templates |
|
6
|
|
|
* and open the template in the editor. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Maslosoft\Addendum\Cache; |
|
10
|
|
|
|
|
11
|
|
|
use Maslosoft\Addendum\Addendum; |
|
12
|
|
|
use Maslosoft\Addendum\Helpers\SoftIncluder; |
|
13
|
|
|
use Maslosoft\Addendum\Options\MetaOptions; |
|
14
|
|
|
use Maslosoft\Cli\Shared\Helpers\PhpExporter; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Meta Namespaces cache |
|
18
|
|
|
* |
|
19
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
20
|
|
|
*/ |
|
21
|
|
|
class NsCache |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
const FileName = '_ns.php'; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
private $_file = ''; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Addendum instance |
|
30
|
|
|
* @var Addendum |
|
31
|
|
|
*/ |
|
32
|
|
|
private $ad = null; |
|
33
|
|
|
private $_ns = []; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
* @var |
|
38
|
|
|
*/ |
|
39
|
|
|
private static $_nsCache = []; |
|
40
|
|
|
|
|
41
|
3 |
|
public function __construct($path, Addendum $addendum, MetaOptions $options = null) |
|
42
|
|
|
{ |
|
43
|
3 |
|
$this->_file = sprintf('%s/%s', $path, self::FileName); |
|
44
|
3 |
|
$this->ad = $addendum; |
|
45
|
3 |
|
$this->setOptions($options); |
|
46
|
3 |
|
} |
|
47
|
|
|
|
|
48
|
30 |
|
public function setOptions(MetaOptions $options = null) |
|
49
|
|
|
{ |
|
50
|
30 |
|
if (!empty($options)) |
|
51
|
30 |
|
{ |
|
52
|
3 |
|
foreach ($options->namespaces as $ns) |
|
53
|
|
|
{ |
|
54
|
3 |
|
if (!$this->_valid($ns)) |
|
55
|
3 |
|
{ |
|
56
|
3 |
|
$this->ad->addNamespace($ns); |
|
57
|
3 |
|
} |
|
58
|
3 |
|
} |
|
59
|
3 |
|
} |
|
60
|
30 |
|
} |
|
61
|
|
|
|
|
62
|
28 |
|
public function valid() |
|
63
|
|
|
{ |
|
64
|
28 |
|
$ns = $this->get(); |
|
65
|
28 |
|
$valid = $this->_valid($ns); |
|
66
|
|
|
|
|
67
|
|
|
// Ovverride existing cache if not valid |
|
68
|
28 |
|
if (!$valid) |
|
69
|
28 |
|
{ |
|
70
|
2 |
|
$this->set(); |
|
71
|
2 |
|
} |
|
72
|
28 |
|
return $valid; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
28 |
|
public function get() |
|
76
|
|
|
{ |
|
77
|
28 |
|
if (!empty(self::$_nsCache[$this->_file])) |
|
78
|
28 |
|
{ |
|
79
|
26 |
|
return self::$_nsCache[$this->_file]; |
|
80
|
|
|
} |
|
81
|
2 |
|
self::$_nsCache[$this->_file] = SoftIncluder::includeFile($this->_file); |
|
82
|
2 |
|
return self::$_nsCache[$this->_file]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
19 |
|
public function set() |
|
86
|
|
|
{ |
|
87
|
19 |
|
foreach ($this->ad->namespaces as $name) |
|
88
|
|
|
{ |
|
89
|
19 |
|
$ns[$name] = true; |
|
|
|
|
|
|
90
|
19 |
|
} |
|
91
|
19 |
|
$data = PhpExporter::export($ns); |
|
|
|
|
|
|
92
|
19 |
|
$mask = umask(0); |
|
93
|
19 |
|
file_put_contents($this->_file, $data); |
|
94
|
19 |
|
umask($mask); |
|
95
|
19 |
|
self::$_nsCache[$this->_file] = $ns; |
|
96
|
19 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function remove() |
|
99
|
|
|
{ |
|
100
|
|
|
unset(self::$_nsCache[$this->_file]); |
|
101
|
|
|
if (file_exists($this->_file)) |
|
102
|
|
|
{ |
|
103
|
|
|
unlink($this->_file); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
28 |
|
private function _valid($ns) |
|
108
|
|
|
{ |
|
109
|
|
|
// Fresh data |
|
110
|
28 |
|
if (empty($ns)) |
|
111
|
28 |
|
{ |
|
112
|
2 |
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
26 |
|
foreach ($this->ad->namespaces as $name) |
|
116
|
|
|
{ |
|
117
|
26 |
|
if (empty($ns[$name])) |
|
118
|
26 |
|
{ |
|
119
|
3 |
|
return false; |
|
120
|
|
|
} |
|
121
|
26 |
|
} |
|
122
|
25 |
|
return true; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|