forked from doppar/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeConsoleCommand.php
More file actions
123 lines (102 loc) · 3.28 KB
/
MakeConsoleCommand.php
File metadata and controls
123 lines (102 loc) · 3.28 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
<?php
namespace Phaseolies\Console\Commands;
use Phaseolies\Console\Schedule\Command;
class MakeConsoleCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'make:command {name}';
/**
* The description of the console command.
*
* @var string
*/
protected $description = 'Create a new console command class';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
return $this->executeWithTiming(function() {
$name = $this->argument('name');
$parts = explode('/', $name);
$className = array_pop($parts);
$namespace = 'App\\Schedule\\Commands' . (count($parts) > 0 ? '\\' . implode('\\', $parts) : '');
$filePath = base_path('app/Schedule/Commands/' . str_replace('/', DIRECTORY_SEPARATOR, $name) . '.php');
if (file_exists($filePath)) {
$this->displayError('Command already exists at:');
$this->line('<fg=white>' . str_replace(base_path(), '', $filePath) . '</>');
return Command::FAILURE;
}
$directoryPath = dirname($filePath);
if (!is_dir($directoryPath)) {
mkdir($directoryPath, 0755, true);
}
$content = $this->generateCommandContent($namespace, $className);
file_put_contents($filePath, $content);
$this->displaySuccess('Command created successfully');
$this->line('<fg=yellow>📁 File:</> <fg=white>' . str_replace(base_path('/'), '', $filePath) . '</>');
$this->newLine();
$this->line('<fg=yellow>📌 Command Name:</> <fg=white>doppar:' . $this->convertToKebabCase($className) . '</>');
return Command::SUCCESS;
});
}
/**
* Generate the command class content.
*/
protected function generateCommandContent(string $namespace, string $className): string
{
return $this->generateRegularControllerContent($namespace, $className);
}
/**
* Convert class name to kebab-case for command name.
*/
protected function convertToKebabCase(string $input): string
{
$input = str_replace('Command', '', $input);
$output = preg_replace('/(?<!^)([A-Z])/', '-$1', $input);
return strtolower($output);
}
/**
* Generate standard command class content.
*/
protected function generateRegularControllerContent(string $namespace, string $className): string
{
$commandName = $this->convertToKebabCase($className);
$appName = strtolower(str_replace(' ', '_', config('app.name', 'doppar')));
return <<<EOT
<?php
namespace {$namespace};
use Phaseolies\Console\Schedule\Command;
class {$className} extends Command
{
/**
* The name of the console command.
*
* @var string
*/
protected \$name = '{$appName}:{$commandName}';
/**
* The console command description.
*
* @var string
*/
protected \$description = 'Command description';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
return Command::SUCCESS;
}
}
EOT;
}
}