-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakeHelperCommand.php
More file actions
161 lines (119 loc) · 4.64 KB
/
Copy pathMakeHelperCommand.php
File metadata and controls
161 lines (119 loc) · 4.64 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
<?php
declare(strict_types=1);
namespace Zero\Lib\Console\Commands;
use Zero\Lib\Console\Command\CommandInterface;
use Zero\Lib\Console\Input;
use Zero\Lib\Console\Support\Filesystem;
use Zero\Lib\Support\Str;
use Zero\Lib\Template;
final class MakeHelperCommand implements CommandInterface
{
public function getName(): string
{
return 'make:helper';
}
public function getDescription(): string
{
return 'Generate a helper class';
}
public function getUsage(): string
{
return 'php zero make:helper Name [--force]';
}
public function execute(array $argv, ?Input $input = null): int
{
$input ??= new Input([], []);
$arguments = $input->arguments();
$name = $arguments[0] ?? $argv[2] ?? null;
$force = (bool) ($input->option('force', false) || in_array('--force', $argv, true));
if ($name === null) {
\Zero\Lib\Log::channel('internal')->error("Usage: {$this->getUsage()}");
return 1;
}
$name = str_replace('\\', '/', $name);
$className = Str::studly(basename($name));
if ($className === '') {
\Zero\Lib\Log::channel('internal')->error('Invalid helper name provided.');
return 1;
}
$directory = dirname($name);
$directory = $directory === '.' ? '' : trim($directory, '/') . '/';
$path = app_path('helpers/' . $directory . $className . '.php');
if (file_exists($path) && ! $force) {
\Zero\Lib\Log::channel('internal')->error("Helper {$className} already exists. Use --force to overwrite.");
return 1;
}
Filesystem::ensureDirectory(dirname($path));
$namespace = 'App\\Helpers';
if ($directory !== '') {
$namespace .= '\\' . str_replace('/', '\\', rtrim($directory, '/'));
}
$signature = Str::snake($className);
$contents = Template::render('helper.tmpl', [
'namespace' => $namespace,
'class' => $className,
'signature' => $signature,
]);
file_put_contents($path, $contents);
\Zero\Lib\Log::channel('internal')->info("Helper created: {$path}");
$fullyQualified = '\\' . ltrim($namespace . '\\' . $className, '\\');
if ($this->appendToHelperRegistry($fullyQualified)) {
\Zero\Lib\Log::channel('internal')->info('Helper registered in app/helpers/Helper.php');
} else {
\Zero\Lib\Log::channel('internal')->error('Unable to update app/helpers/Helper.php automatically.');
}
return 0;
}
private function appendToHelperRegistry(string $class): bool
{
$helperPath = app_path('helpers/Helper.php');
if (! file_exists($helperPath) || ! is_writable($helperPath)) {
return false;
}
$contents = file_get_contents($helperPath);
if ($contents === false) {
return false;
}
if (str_contains($contents, $class . '::class')) {
return true;
}
if (! preg_match('/\$this->register\(\s*\[\s*(.*?)\s*\]\);/s', $contents, $matches, PREG_OFFSET_CAPTURE)) {
return false;
}
$block = $matches[1][0];
$blockOffset = (int) $matches[1][1];
$lines = preg_split('/\R/', trim($block)) ?: [];
$entries = [];
$comments = [];
foreach ($lines as $line) {
$trimmed = trim($line);
if ($trimmed === '') {
continue;
}
if (str_starts_with($trimmed, '//')) {
$comments[] = $trimmed;
continue;
}
$entries[] = rtrim($trimmed, ',');
}
$entries[] = $class . '::class';
$entries = array_unique($entries);
$entries = array_values($entries);
usort($entries, static function (string $a, string $b): int {
$normalize = static fn (string $value): string => ltrim(preg_replace('/::class$/', '', $value) ?? $value, '\\');
return strcmp($normalize($a), $normalize($b));
});
$indent = ' ';
$rebuilt = [];
foreach ($entries as $entry) {
$rebuilt[] = $indent . $entry . ',';
}
foreach ($comments as $comment) {
$rebuilt[] = $indent . $comment;
}
$replacement = PHP_EOL . implode(PHP_EOL, $rebuilt) . PHP_EOL . ' ';
$updated = substr_replace($contents, $replacement, $blockOffset, strlen($block));
$updated = preg_replace("/\n[ \t]+\n/", "\n", $updated) ?? $updated;
return file_put_contents($helperPath, $updated) !== false;
}
}