|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\ConsoleTasker\Console\Tasks; |
|
4
|
|
|
|
|
5
|
|
|
use Cerbero\ConsoleTasker\Console\ParsedTask; |
|
6
|
|
|
use Cerbero\ConsoleTasker\Console\TasksParser; |
|
7
|
|
|
use Cerbero\ConsoleTasker\Tasks\AbstractCreatorTask; |
|
8
|
|
|
use Cerbero\ConsoleTasker\Traits\ConfigAware; |
|
9
|
|
|
use Illuminate\Container\Container; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The task to create the Artisan command. |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
class CreateCommand extends AbstractCreatorTask |
|
17
|
|
|
{ |
|
18
|
|
|
use ConfigAware; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The tasks parser. |
|
22
|
|
|
* |
|
23
|
|
|
* @var TasksParser |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $parser; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Instantiate the class. |
|
29
|
|
|
* |
|
30
|
|
|
* @param TasksParser $parser |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(TasksParser $parser) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->parser = $parser; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Retrieve the path of the stub |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function getStubPath(): string |
|
43
|
|
|
{ |
|
44
|
|
|
return __DIR__ . '/../../../stubs/command.stub'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Retrieve the fully qualified name of the file to create |
|
49
|
|
|
* |
|
50
|
|
|
* @return string|null |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function getFullyQualifiedName(): ?string |
|
53
|
|
|
{ |
|
54
|
|
|
$name = str_replace('/', '\\', $this->argument('name')); |
|
55
|
|
|
$namespace = Container::getInstance()->make('app')->getNamespace(); |
|
56
|
|
|
|
|
57
|
|
|
if (Str::startsWith($name, $namespace)) { |
|
58
|
|
|
return $name; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return "{$namespace}Console\Commands\\{$name}"; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Retrieve the replacements to apply on the stub |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function getReplacements(): array |
|
70
|
|
|
{ |
|
71
|
|
|
$parsedTasks = $this->option('tasks') ? $this->parser->parse($this->option('tasks')) : []; |
|
72
|
|
|
|
|
73
|
|
|
return [ |
|
74
|
|
|
'{{ command }}' => $this->option('command'), |
|
75
|
|
|
'{{ tasks }}' => $this->getPaddedTasks($parsedTasks), |
|
76
|
|
|
'{{ namespaces }}' => $this->getNamespaces($parsedTasks), |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Retrieve the command tasks with padding |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $parsedTasks |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function getPaddedTasks(array $parsedTasks): string |
|
87
|
|
|
{ |
|
88
|
|
|
if (empty($parsedTasks)) { |
|
89
|
|
|
return str_repeat(' ', 12) . '//'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$paddedTasks = array_map(function (ParsedTask $task) { |
|
93
|
|
|
return str_repeat(' ', 12) . $task->name . '::class,'; |
|
94
|
|
|
}, $parsedTasks); |
|
95
|
|
|
|
|
96
|
|
|
return implode(PHP_EOL, $paddedTasks); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Retrieve the task namespaces |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $parsedTasks |
|
103
|
|
|
* @return string|null |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getNamespaces(array $parsedTasks): ?string |
|
106
|
|
|
{ |
|
107
|
|
|
if (empty($parsedTasks)) { |
|
108
|
|
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$namespace = vsprintf('%s%s\%s', [ |
|
112
|
|
|
Container::getInstance()->make('app')->getNamespace(), |
|
113
|
|
|
str_replace('/', '\\', $this->config('tasks_directory')), |
|
114
|
|
|
$this->argument('name'), |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
|
|
return array_reduce($parsedTasks, function (string $carry, ParsedTask $task) use ($namespace) { |
|
118
|
|
|
return $carry .= "use {$namespace}\\{$task->name};" . PHP_EOL; |
|
119
|
|
|
}, '') . PHP_EOL; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|