1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leonidas\Console\Command; |
4
|
|
|
|
5
|
|
|
use Jawira\CaseConverter\CaseConverter; |
6
|
|
|
use Leonidas\Library\Core\Abstracts\ConvertsCaseTrait; |
7
|
|
|
use Noodlehaus\Config; |
8
|
|
|
use PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor; |
9
|
|
|
use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter; |
10
|
|
|
use RuntimeException; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
16
|
|
|
|
17
|
|
|
abstract class HopliteCommand extends Command |
18
|
|
|
{ |
19
|
|
|
use ConvertsCaseTrait; |
20
|
|
|
|
21
|
|
|
protected Config $config; |
22
|
|
|
|
23
|
|
|
protected InputInterface $input; |
24
|
|
|
|
25
|
|
|
protected SymfonyStyle $output; |
26
|
|
|
|
27
|
|
|
protected Config $composerConfig; |
28
|
|
|
|
29
|
|
|
protected Filesystem $filesystem; |
30
|
|
|
|
31
|
|
|
protected Highlighter $highlighter; |
32
|
|
|
|
33
|
|
|
public function __construct(string $name = null) |
34
|
|
|
{ |
35
|
|
|
$this->config = new Config($this->external('/hoplite.yml')); |
36
|
|
|
$this->composerConfig = new Config($this->external('/composer.json')); |
37
|
|
|
$this->filesystem = new Filesystem(); |
38
|
|
|
$this->highlighter = new Highlighter(new ConsoleColor()); |
39
|
|
|
$this->caseConverter = new CaseConverter(); |
40
|
|
|
|
41
|
|
|
parent::__construct($name); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function run(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
$this->input = $input; |
47
|
|
|
$this->output = new SymfonyStyle($input, $output); |
48
|
|
|
|
49
|
|
|
return parent::run($this->input, $this->output); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
53
|
|
|
{ |
54
|
|
|
return $this->handle(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function config(string $key, $default = null) |
58
|
|
|
{ |
59
|
|
|
return $this->config->get($key, $default); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function composer(string $key, $default = null) |
63
|
|
|
{ |
64
|
|
|
return $this->composerConfig->get($key, $default); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function pathToNamespace(string $path, string $extra = ''): string |
68
|
|
|
{ |
69
|
|
|
foreach ($this->composer('autoload.psr-4') as $namespace => $dir) { |
70
|
|
|
if (str_starts_with($path, $dir)) { |
71
|
|
|
$local = str_replace([$dir, '/'], ['', '\\'], $path); |
72
|
|
|
$extra = $extra ? '\\' . $extra : ''; |
73
|
|
|
|
74
|
|
|
return $namespace . $local . $extra; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
throw new RuntimeException( |
79
|
|
|
'Could not find namespace in composer.json that corresponds with path: ' . $path |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function resolveAbstractNamespace(string $namespace): string |
84
|
|
|
{ |
85
|
|
|
return $this->config('abstract_dir', true) |
86
|
|
|
? $namespace . '\\Abstracts' |
87
|
|
|
: $namespace; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function resolveAbstractDir(string $dir): string |
91
|
|
|
{ |
92
|
|
|
return $this->config('abstract_dir', true) |
93
|
|
|
? $dir . DIRECTORY_SEPARATOR . 'Abstracts' |
94
|
|
|
: $dir; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function configurableArgument(string $option, string $configKey, $default = null) |
98
|
|
|
{ |
99
|
|
|
return $this->input->getArgument($option) |
100
|
|
|
?? $this->config($configKey, $default); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function configurableOption(string $option, string $configKey, $default = null) |
104
|
|
|
{ |
105
|
|
|
return $this->input->getOption($option) |
106
|
|
|
?? $this->config($configKey, $default); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function internal(string $path = ''): string |
110
|
|
|
{ |
111
|
|
|
return dirname(__DIR__, 1) . $path; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function external(string $path = ''): string |
115
|
|
|
{ |
116
|
|
|
return getcwd() . $path; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function printPhp(string $code): void |
120
|
|
|
{ |
121
|
|
|
echo $this->highlighter->getWholeFile($code); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function writeFile(string $path, string $content): void |
125
|
|
|
{ |
126
|
|
|
$this->filesystem->dumpFile($path, $content); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function writePhpFile(string $path, string $name, string $content): void |
130
|
|
|
{ |
131
|
|
|
$this->writeFile($this->phpFile($path, $name), $content); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function writePhpFileRel(string $path, string $name, string $content): void |
135
|
|
|
{ |
136
|
|
|
$this->writeFile($this->phpFileRel($path, $name), $content); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function phpFile(string $path, string $name): string |
140
|
|
|
{ |
141
|
|
|
return $path . DIRECTORY_SEPARATOR . $name . '.php'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function phpFileRel(string $path, string $name): string |
145
|
|
|
{ |
146
|
|
|
return $this->phpFile($this->external(DIRECTORY_SEPARATOR . $path), $name); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
abstract protected function handle(): int; |
150
|
|
|
} |
151
|
|
|
|