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