Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.14% |
23 / 28 |
|
37.50% |
3 / 8 |
CRAP | |
0.00% |
0 / 1 |
| File | |
82.14% |
23 / 28 |
|
37.50% |
3 / 8 |
24.76 | |
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 | |||
| hasDir | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| setFormat | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| getFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| save | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 | |||
| clear | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
7.14 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Debug\Storage; |
| 15 | |
| 16 | use Pop\Csv\Csv; |
| 17 | use Pop\Debug\Handler\AbstractHandler; |
| 18 | |
| 19 | /** |
| 20 | * Debug file storage class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Debug |
| 24 | * @author Nick Sagona, III <dev@noladev.com> |
| 25 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 3.0.0 |
| 28 | */ |
| 29 | class File extends AbstractStorage |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Storage dir |
| 34 | * @var ?string |
| 35 | */ |
| 36 | protected ?string $dir = null; |
| 37 | |
| 38 | /** |
| 39 | * Format (csv or tsv)) |
| 40 | * @var string |
| 41 | */ |
| 42 | protected string $format = 'csv'; |
| 43 | |
| 44 | /** |
| 45 | * Constructor |
| 46 | * |
| 47 | * Instantiate the file storage object |
| 48 | * |
| 49 | * @param string $dir |
| 50 | * @param string $format |
| 51 | */ |
| 52 | public function __construct(string $dir, string $format = 'csv') |
| 53 | { |
| 54 | $this->setDir($dir); |
| 55 | $this->setFormat($format); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set the current storage dir |
| 60 | * |
| 61 | * @param string $dir |
| 62 | * @throws Exception |
| 63 | * @return File |
| 64 | */ |
| 65 | public function setDir(string $dir): File |
| 66 | { |
| 67 | if (!file_exists($dir)) { |
| 68 | throw new Exception('Error: That directory does not exist.'); |
| 69 | } else if (!is_writable($dir)) { |
| 70 | throw new Exception('Error: That directory is not writable.'); |
| 71 | } |
| 72 | |
| 73 | $this->dir = realpath($dir); |
| 74 | |
| 75 | return $this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the storage dir |
| 80 | * |
| 81 | * @return ?string |
| 82 | */ |
| 83 | public function getDir(): ?string |
| 84 | { |
| 85 | return $this->dir; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Has storage dir |
| 90 | * |
| 91 | * @return bool |
| 92 | */ |
| 93 | public function hasDir(): bool |
| 94 | { |
| 95 | return (!empty($this->dir) && file_exists($this->dir)); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Set the format |
| 100 | * |
| 101 | * @param string $format |
| 102 | * @throws \InvalidArgumentException |
| 103 | * @return File |
| 104 | */ |
| 105 | public function setFormat(string $format): File |
| 106 | { |
| 107 | $format = strtolower($format); |
| 108 | |
| 109 | if (!in_array($format, ['csv', 'tsv'])) { |
| 110 | throw new \InvalidArgumentException('Error: The format must be either "csv" or "tsv".'); |
| 111 | } |
| 112 | |
| 113 | $this->format = $format; |
| 114 | |
| 115 | return $this; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get the format |
| 120 | * |
| 121 | * @return string |
| 122 | */ |
| 123 | public function getFormat(): string |
| 124 | { |
| 125 | return $this->format; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Save debug data |
| 130 | * |
| 131 | * @param string $id |
| 132 | * @param string $name |
| 133 | * @param AbstractHandler $handler |
| 134 | * @return void |
| 135 | */ |
| 136 | public function save(string $id, string $name, AbstractHandler $handler): void |
| 137 | { |
| 138 | |
| 139 | $events = $this->prepareEvents($id, $name, $handler); |
| 140 | $filename = $this->dir . DIRECTORY_SEPARATOR . $id . '-' . $name . '.' . $this->format; |
| 141 | |
| 142 | if (!file_exists($filename) && isset($events[0])) { |
| 143 | file_put_contents($filename, Csv::getFieldHeaders($events[0], (($this->format == 'tsv') ? "\t" : ','))); |
| 144 | } |
| 145 | |
| 146 | Csv::appendDataToFile($filename, $events, ['delimiter' => (($this->format == 'tsv') ? "\t" : ',')]); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Clear all debug data |
| 151 | * |
| 152 | * @return void |
| 153 | */ |
| 154 | public function clear(): void |
| 155 | { |
| 156 | if (!$dh = @opendir($this->dir)) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | while (false !== ($obj = readdir($dh))) { |
| 161 | if (($obj != '.') && ($obj != '..') && |
| 162 | !is_dir($this->dir . DIRECTORY_SEPARATOR . $obj) && is_file($this->dir . DIRECTORY_SEPARATOR . $obj)) { |
| 163 | unlink($this->dir . DIRECTORY_SEPARATOR . $obj); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | closedir($dh); |
| 168 | } |
| 169 | |
| 170 | } |