-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakeLoggerCommand.php
More file actions
143 lines (108 loc) · 3.95 KB
/
Copy pathMakeLoggerCommand.php
File metadata and controls
143 lines (108 loc) · 3.95 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
<?php
declare(strict_types=1);
namespace Zero\Lib\Console\Commands;
use Zero\Lib\Console\Command\CommandInterface;
use Zero\Lib\Console\Support\Filesystem;
use Zero\Lib\Support\Str;
use Zero\Lib\Template;
final class MakeLoggerCommand implements CommandInterface
{
public function getName(): string
{
return 'make:logger';
}
public function getDescription(): string
{
return 'Generate a custom log handler and register its channel';
}
public function getUsage(): string
{
return 'php zero make:logger Name [--force]';
}
public function execute(array $argv): int
{
$name = $argv[2] ?? null;
$force = in_array('--force', $argv, true);
if ($name === null) {
\Zero\Lib\Log::channel('internal')->error("Usage: {$this->getUsage()}");
return 1;
}
$name = str_replace('\\', '/', $name);
$name = preg_replace('#/+#', '/', $name);
$name = trim((string) $name, '/');
if ($name === '') {
\Zero\Lib\Log::channel('internal')->error("Usage: {$this->getUsage()}");
return 1;
}
$className = Str::studly(basename($name));
$directory = dirname($name);
$directory = $directory === '.' ? '' : $directory . '/';
$namespace = 'App\\Logging';
if ($directory !== '') {
$namespace .= '\\' . str_replace('/', '\\', rtrim($directory, '/'));
}
$path = app_path('logging/' . $directory . $className . '.php');
if (file_exists($path) && ! $force) {
\Zero\Lib\Log::channel('internal')->error("Log handler {$className} already exists. Use --force to overwrite.");
return 1;
}
Filesystem::ensureDirectory(dirname($path));
$contents = Template::render('log-handler.tmpl', [
'namespace' => $namespace,
'class' => $className,
]);
file_put_contents($path, $contents);
$keySource = trim($name) !== '' ? str_replace('/', ' ', $name) : $className;
$channelKey = Str::snake($keySource);
$fullyQualified = $namespace . '\\' . $className;
$registered = $this->registerChannel($channelKey, $fullyQualified);
\Zero\Lib\Log::channel('internal')->info("Log handler created: {$path}");
if ($registered) {
\Zero\Lib\Log::channel('internal')->info("Channel added to config/logging.php as '{$channelKey}'.");
} else {
\Zero\Lib\Log::channel('internal')->error('Unable to update config/logging.php automatically. Please add the channel manually.');
}
return 0;
}
private function registerChannel(string $channelKey, string $handler): bool
{
$configPath = config_path('logging.php');
if (! file_exists($configPath) || ! is_readable($configPath)) {
return false;
}
$contents = file_get_contents($configPath);
if ($contents === false) {
return false;
}
if (str_contains($contents, "'{$channelKey}' =>")) {
return true;
}
$handlerReference = $handler . '::class';
$block = " '{$channelKey}' => [\n"
. " 'driver' => 'custom',\n"
. " 'handler' => {$handlerReference},\n"
. " ],\n";
$updated = preg_replace(
"/(\n\s*'stack'\s*=>\s*\[)/",
"\n" . $block . "$1",
$contents,
1
);
if ($updated === null) {
return false;
}
if ($updated === $contents) {
$updated = preg_replace(
"/(\n\s*\],\n\];\n)/",
"\n" . $block . "$1",
$contents,
1
);
if ($updated === null || $updated === $contents) {
return false;
}
}
file_put_contents($configPath, $updated);
return true;
}
}