Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.61% |
57 / 59 |
|
80.00% |
8 / 10 |
CRAP | |
0.00% |
0 / 1 |
File | |
96.61% |
57 / 59 |
|
80.00% |
8 / 10 |
33 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setDir | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
getDir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
save | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
has | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
delete | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
clear | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
7.10 | |||
encodeValue | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
decodeValue | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Debug\Storage; |
15 | |
16 | /** |
17 | * Debug file storage class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Debug |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 1.3.0 |
25 | */ |
26 | class File extends AbstractStorage |
27 | { |
28 | |
29 | /** |
30 | * Storage dir |
31 | * @var string |
32 | */ |
33 | protected $dir = null; |
34 | |
35 | /** |
36 | * Constructor |
37 | * |
38 | * Instantiate the file storage object |
39 | * |
40 | * @param string $dir |
41 | * @param string $format |
42 | */ |
43 | public function __construct($dir, $format = null) |
44 | { |
45 | parent::__construct($format); |
46 | $this->setDir($dir); |
47 | } |
48 | |
49 | /** |
50 | * Set the current storage dir |
51 | * |
52 | * @param string $dir |
53 | * @throws Exception |
54 | * @return File |
55 | */ |
56 | public function setDir($dir) |
57 | { |
58 | if (!file_exists($dir)) { |
59 | throw new Exception('Error: That directory does not exist.'); |
60 | } else if (!is_writable($dir)) { |
61 | throw new Exception('Error: That directory is not writable.'); |
62 | } |
63 | |
64 | $this->dir = realpath($dir); |
65 | |
66 | return $this; |
67 | } |
68 | |
69 | /** |
70 | * Get the storage dir |
71 | * |
72 | * @return string |
73 | */ |
74 | public function getDir() |
75 | { |
76 | return $this->dir; |
77 | } |
78 | |
79 | /** |
80 | * Save debug data |
81 | * |
82 | * @param string $id |
83 | * @param mixed $value |
84 | * @return File |
85 | */ |
86 | public function save($id, $value) |
87 | { |
88 | $filename = $id; |
89 | |
90 | if ($this->format == self::JSON) { |
91 | $filename .= '.json'; |
92 | } else if ($this->format == self::PHP) { |
93 | $filename .= '.php'; |
94 | } else { |
95 | $filename .= '.log'; |
96 | } |
97 | |
98 | file_put_contents($this->dir . DIRECTORY_SEPARATOR . $filename, $this->encodeValue($value)); |
99 | |
100 | return $this; |
101 | } |
102 | |
103 | /** |
104 | * Get debug data |
105 | * |
106 | * @param string $id |
107 | * @return mixed |
108 | */ |
109 | public function get($id) |
110 | { |
111 | return $this->decodeValue($this->dir . DIRECTORY_SEPARATOR . $id); |
112 | } |
113 | |
114 | /** |
115 | * Determine if debug data exists |
116 | * |
117 | * @param string $id |
118 | * @return boolean |
119 | */ |
120 | public function has($id) |
121 | { |
122 | $fileId = $this->dir . DIRECTORY_SEPARATOR . $id; |
123 | |
124 | if ($this->format == self::JSON) { |
125 | $fileId .= '.json'; |
126 | } else if ($this->format == self::PHP) { |
127 | $fileId .= '.php'; |
128 | } else { |
129 | $fileId .= '.log'; |
130 | } |
131 | |
132 | return (file_exists($fileId)); |
133 | } |
134 | |
135 | /** |
136 | * Delete debug data |
137 | * |
138 | * @param string $id |
139 | * @return File |
140 | */ |
141 | public function delete($id) |
142 | { |
143 | $fileId = $this->dir . DIRECTORY_SEPARATOR . $id; |
144 | |
145 | if ($this->format == self::JSON) { |
146 | $fileId .= '.json'; |
147 | } else if ($this->format == self::PHP) { |
148 | $fileId .= '.php'; |
149 | } else { |
150 | $fileId .= '.log'; |
151 | } |
152 | |
153 | if (file_exists($fileId)) { |
154 | unlink($fileId); |
155 | } |
156 | |
157 | return $this; |
158 | } |
159 | |
160 | /** |
161 | * Clear all debug data |
162 | * |
163 | * @return File |
164 | */ |
165 | public function clear() |
166 | { |
167 | if (!$dh = @opendir($this->dir)) { |
168 | return $this; |
169 | } |
170 | |
171 | while (false !== ($obj = readdir($dh))) { |
172 | if (($obj != '.') && ($obj != '..') && |
173 | !is_dir($this->dir . DIRECTORY_SEPARATOR . $obj) && is_file($this->dir . DIRECTORY_SEPARATOR . $obj)) { |
174 | unlink($this->dir . DIRECTORY_SEPARATOR . $obj); |
175 | } |
176 | } |
177 | |
178 | closedir($dh); |
179 | |
180 | return $this; |
181 | } |
182 | |
183 | /** |
184 | * Encode the value based on the format |
185 | * |
186 | * @param mixed $value |
187 | * @throws Exception |
188 | * @return string |
189 | */ |
190 | public function encodeValue($value) |
191 | { |
192 | if ($this->format == self::JSON) { |
193 | $value = json_encode($value, JSON_PRETTY_PRINT); |
194 | } else if ($this->format == self::PHP) { |
195 | $value = "<?php" . PHP_EOL . "return unserialize(base64_decode('" . |
196 | base64_encode(serialize($value)) . "'));" . PHP_EOL; |
197 | } else if (!is_string($value)) { |
198 | throw new Exception('Error: The value must be a string if storing in text format.'); |
199 | } |
200 | |
201 | return $value; |
202 | } |
203 | |
204 | /** |
205 | * Decode the value based on the format |
206 | * |
207 | * @param mixed $value |
208 | * @return mixed |
209 | */ |
210 | public function decodeValue($value) |
211 | { |
212 | if ($this->format == self::JSON) { |
213 | $value .= '.json'; |
214 | $value = (file_exists($value)) ? json_decode(file_get_contents($value), true) : false; |
215 | } else if ($this->format == self::PHP) { |
216 | $value .= '.php'; |
217 | $value = (file_exists($value)) ? include $value : false; |
218 | } else { |
219 | $value .= '.log'; |
220 | $value = (file_exists($value)) ? file_get_contents($value) : false; |
221 | } |
222 | |
223 | return $value; |
224 | } |
225 | |
226 | } |