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\Filesystem\Filesystem; |
13
|
|
|
|
14
|
|
|
abstract class HopliteCommand extends Command |
15
|
|
|
{ |
16
|
|
|
use ConvertsCaseTrait; |
17
|
|
|
|
18
|
|
|
protected Config $config; |
19
|
|
|
|
20
|
|
|
protected Config $composerConfig; |
21
|
|
|
|
22
|
|
|
protected Filesystem $filesystem; |
23
|
|
|
|
24
|
|
|
protected Highlighter $highlighter; |
25
|
|
|
|
26
|
|
|
public function __construct(string $name = null) |
27
|
|
|
{ |
28
|
|
|
$this->config = new Config($this->external('/hoplite.yml')); |
29
|
|
|
$this->composerConfig = new Config($this->external('/composer.json')); |
30
|
|
|
$this->filesystem = new Filesystem(); |
31
|
|
|
$this->highlighter = new Highlighter(new ConsoleColor()); |
32
|
|
|
$this->caseConverter = new CaseConverter(); |
33
|
|
|
|
34
|
|
|
parent::__construct($name); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function config(string $key, $default = null) |
38
|
|
|
{ |
39
|
|
|
return $this->config->get($key, $default); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function composer(string $key, $default = null) |
43
|
|
|
{ |
44
|
|
|
return $this->composerConfig->get($key, $default); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function getNamespaceFromPath(string $path): string |
48
|
|
|
{ |
49
|
|
|
// todo: match completely |
50
|
|
|
$namespaces = array_flip($this->composer('autoload.psr-4')); |
|
|
|
|
51
|
|
|
$parts = explode('/', $path); |
52
|
|
|
$entry = array_shift($parts); |
53
|
|
|
$base = $namespaces[$entry . '/']; |
54
|
|
|
|
55
|
|
|
return $base . implode('\\', $parts); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function getPathFromNamespace(string $namespace): string |
59
|
|
|
{ |
60
|
|
|
// todo: match completely |
61
|
|
|
$namespaces = $this->composer('autoload.psr-4'); |
62
|
|
|
$parts = explode('\\', $namespace); |
63
|
|
|
$entry = array_shift($parts); |
64
|
|
|
$base = $namespaces[$entry . '\\']; |
65
|
|
|
|
66
|
|
|
return $base . implode('/', $parts); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function fallbackArgument(InputInterface $input, string $option, string $configKey, $default = null) |
70
|
|
|
{ |
71
|
|
|
return $input->getArgument($option) ?? $this->config($configKey, $default); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function fallbackOption(InputInterface $input, string $option, string $configKey, $default = null) |
75
|
|
|
{ |
76
|
|
|
return $input->getOption($option) ?? $this->config($configKey, $default); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function internal(string $path = ''): string |
80
|
|
|
{ |
81
|
|
|
return dirname(__DIR__, 1) . $path; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function external(string $path = ''): string |
85
|
|
|
{ |
86
|
|
|
return getcwd() . $path; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function printPhp(string $code): void |
90
|
|
|
{ |
91
|
|
|
echo $this->highlighter->getWholeFile($code); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function writeFile(string $path, string $content): void |
95
|
|
|
{ |
96
|
|
|
$this->filesystem->dumpFile($path, $content); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|