-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogClearCommand.php
More file actions
162 lines (128 loc) · 4.55 KB
/
Copy pathLogClearCommand.php
File metadata and controls
162 lines (128 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
namespace Zero\Lib\Console\Commands;
use FilesystemIterator;
use Zero\Lib\Console\Command\CommandInterface;
use function config;
use function storage_path;
/**
* Built-in command to prune log files from the configured log directory.
*/
final class LogClearCommand implements CommandInterface
{
public function getName(): string
{
return 'log:clear';
}
public function getDescription(): string
{
return 'Remove generated *.log files for the configured channel or path.';
}
public function getUsage(): string
{
return 'php zero log:clear [--channel=file] [--path=/absolute/log/path]';
}
public function execute(array $argv): int
{
[, $options] = $this->parseOptions($argv);
$pathOption = $options['path'] ?? null;
$targetPath = is_string($pathOption) && trim($pathOption) !== '' ? $pathOption : null;
if ($targetPath === null) {
$channelOption = $options['channel'] ?? null;
$channel = is_string($channelOption) && trim($channelOption) !== ''
? $channelOption
: (string) config('logging.default', 'file');
$channelConfig = (array) config('logging.channels.' . $channel, []);
$targetPath = (string) ($channelConfig['path'] ?? storage_path('framework/logs'));
}
$targetPath = $this->normalisePath($targetPath);
if (!is_dir($targetPath)) {
fwrite(STDERR, sprintf('Log directory [%s] does not exist.%s', $targetPath, PHP_EOL));
return 1;
}
$removed = 0;
$iterator = new FilesystemIterator($targetPath, FilesystemIterator::SKIP_DOTS);
foreach ($iterator as $item) {
if (! $item->isFile() || !str_ends_with($item->getFilename(), '.log')) {
continue;
}
if (@unlink($item->getPathname())) {
$removed++;
}
}
if ($removed === 0) {
echo sprintf('No log files found in %s%s', $targetPath, PHP_EOL);
} else {
echo sprintf('Removed %d log file(s) from %s%s', $removed, $targetPath, PHP_EOL);
}
return 0;
}
/**
* @return array{0: array<int, string>, 1: array<string, mixed>}
*/
private function parseOptions(array $argv): array
{
$arguments = [];
$options = [];
$count = count($argv);
for ($i = 2; $i < $count; $i++) {
$token = $argv[$i];
if ($token === '--') {
$arguments = array_merge($arguments, array_slice($argv, $i + 1));
break;
}
if (str_starts_with($token, '--')) {
$segment = substr($token, 2);
if ($segment === '') {
continue;
}
if (str_contains($segment, '=')) {
[$key, $value] = explode('=', $segment, 2);
} else {
$key = $segment;
if ($i + 1 < $count && !str_starts_with((string) $argv[$i + 1], '-')) {
$value = $argv[++$i];
} else {
$value = true;
}
}
$options[$key] = $value;
continue;
}
if (str_starts_with($token, '-')) {
$flags = substr($token, 1);
if ($flags === '') {
continue;
}
foreach (str_split($flags) as $flag) {
$options[$flag] = true;
}
continue;
}
$arguments[] = $token;
}
return [$arguments, $options];
}
private function normalisePath(string $path): string
{
$path = trim($path);
if ($path === '') {
return $path;
}
if ($path[0] === '~') {
$home = getenv('HOME') ?: getenv('USERPROFILE');
if ($home !== false && $home !== '') {
$path = $home . DIRECTORY_SEPARATOR . ltrim(substr($path, 1), DIRECTORY_SEPARATOR);
}
}
if ($this->isAbsolutePath($path)) {
return rtrim($path, DIRECTORY_SEPARATOR);
}
return rtrim(getcwd() . DIRECTORY_SEPARATOR . $path, DIRECTORY_SEPARATOR);
}
private function isAbsolutePath(string $path): bool
{
return str_starts_with($path, DIRECTORY_SEPARATOR)
|| (strlen($path) > 1 && ctype_alpha($path[0]) && $path[1] === ':');
}
}