1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Karma; |
6
|
|
|
|
7
|
|
|
use Karma\Logging\OutputAware; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Karma\Logging\OutputInterfaceAdapter; |
12
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
13
|
|
|
|
14
|
|
|
class Command extends \Symfony\Component\Console\Command\Command |
15
|
|
|
{ |
16
|
|
|
use OutputAware; |
17
|
|
|
|
18
|
|
|
protected Application |
|
|
|
|
19
|
|
|
$app; |
20
|
|
|
|
21
|
36 |
|
public function __construct(Application $app) |
22
|
|
|
{ |
23
|
36 |
|
parent::__construct(); |
24
|
|
|
|
25
|
36 |
|
$this->app = $app; |
26
|
36 |
|
} |
27
|
|
|
|
28
|
36 |
|
protected function configure(): void |
29
|
|
|
{ |
30
|
36 |
|
$this->addOption('cache', null, InputOption::VALUE_NONE, 'Cache the dist files list'); |
31
|
36 |
|
$this->addOption('no-title', null, InputOption::VALUE_NONE, 'Do not display logo title'); |
32
|
36 |
|
} |
33
|
|
|
|
34
|
36 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
35
|
|
|
{ |
36
|
36 |
|
$this->configureOutputInterface($output); |
37
|
36 |
|
$this->printHeader($input->getOption('no-title')); |
38
|
|
|
|
39
|
36 |
|
$profile = $this->app['profile']; |
40
|
|
|
|
41
|
36 |
|
$confDir = Application::DEFAULT_CONF_DIRECTORY; |
42
|
36 |
|
if($profile->hasConfigurationDirectory()) |
43
|
|
|
{ |
44
|
1 |
|
$confDir = $profile->getConfigurationDirectory(); |
45
|
|
|
} |
46
|
|
|
|
47
|
36 |
|
$masterFile = Application::DEFAULT_MASTER_FILE; |
48
|
36 |
|
if($profile->hasMasterFilename()) |
49
|
|
|
{ |
50
|
1 |
|
$masterFile = $profile->getMasterFilename(); |
51
|
|
|
} |
52
|
|
|
|
53
|
36 |
|
$suffix = Application::DEFAULT_DISTFILE_SUFFIX; |
54
|
36 |
|
if($profile->hasTemplatesSuffix()) |
55
|
|
|
{ |
56
|
1 |
|
$suffix = $profile->getTemplatesSuffix(); |
57
|
|
|
} |
58
|
|
|
|
59
|
36 |
|
$this->app['configuration.path'] = $confDir; |
60
|
36 |
|
$this->app['configuration.masterFile'] = $masterFile; |
61
|
36 |
|
$this->app['distFiles.suffix'] = $suffix; |
62
|
|
|
|
63
|
36 |
|
$this->app['logger'] = new OutputInterfaceAdapter($output); |
64
|
|
|
|
65
|
36 |
|
if($input->getOption('cache')) |
66
|
|
|
{ |
67
|
2 |
|
$this->enableFinderCache(); |
68
|
|
|
} |
69
|
|
|
|
70
|
36 |
|
return 0; |
71
|
|
|
} |
72
|
|
|
|
73
|
36 |
|
private function configureOutputInterface(OutputInterface $output): void |
74
|
|
|
{ |
75
|
36 |
|
$style = new OutputFormatterStyle('cyan', null, array('bold')); |
76
|
36 |
|
$output->getFormatter()->setStyle('important', $style); |
77
|
|
|
|
78
|
36 |
|
$this->setOutput($output); |
79
|
36 |
|
} |
80
|
|
|
|
81
|
2 |
|
private function enableFinderCache(): void |
82
|
|
|
{ |
83
|
2 |
|
$this->app['sources.fileSystem.finder'] = $this->app['sources.fileSystem.cached']; |
84
|
2 |
|
} |
85
|
|
|
|
86
|
5 |
|
protected function formatValue($value) |
87
|
|
|
{ |
88
|
5 |
|
if($value === false) |
89
|
|
|
{ |
90
|
|
|
$value = 'false'; |
91
|
|
|
} |
92
|
5 |
|
elseif($value === true) |
93
|
|
|
{ |
94
|
|
|
$value = 'true'; |
95
|
|
|
} |
96
|
5 |
|
elseif($value === null) |
97
|
|
|
{ |
98
|
|
|
$value = '<fg=white;options=bold>NULL</fg=white;options=bold>'; |
99
|
|
|
} |
100
|
5 |
|
elseif($value === Configuration::NOT_FOUND) |
101
|
|
|
{ |
102
|
|
|
$value = '<error>NOT FOUND</error>'; |
103
|
|
|
} |
104
|
5 |
|
elseif(is_array($value)) |
105
|
|
|
{ |
106
|
|
|
array_walk($value, function(& $item) { |
107
|
|
|
$item = $this->formatValue($item); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
$value = sprintf('[%s]', implode(', ', $value)); |
111
|
|
|
} |
112
|
|
|
|
113
|
5 |
|
return $value; |
114
|
|
|
} |
115
|
|
|
|
116
|
36 |
|
private function printHeader(bool $noTitle = true): void |
117
|
|
|
{ |
118
|
36 |
|
if($noTitle === true) |
119
|
|
|
{ |
120
|
|
|
$this->output->writeln('Karma ' . Application::VERSION); |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
36 |
|
$this->output->writeln( |
125
|
36 |
|
$this->getLogo($this->output->isDecorated()) |
126
|
|
|
); |
127
|
36 |
|
} |
128
|
|
|
|
129
|
36 |
|
private function getLogo(bool $outputDecorated = true): string |
130
|
|
|
{ |
131
|
|
|
$logo = <<<ASCIIART |
132
|
36 |
|
.@@@@...@@.. |
133
|
|
|
...@@..@.... |
134
|
|
|
...@@.@@.... %K A R M A* |
135
|
|
|
...@@.@@@... %VERSION* |
136
|
|
|
...@@...@@.. |
137
|
|
|
...@@....@@. |
138
|
|
|
|
139
|
|
|
ASCIIART; |
140
|
|
|
|
141
|
36 |
|
$background = 'fg=magenta'; |
142
|
36 |
|
$text = 'fg=white'; |
143
|
|
|
|
144
|
36 |
|
$toBackground = "</$text><$background>"; |
145
|
36 |
|
$toText = "</$background><$text>"; |
146
|
|
|
|
147
|
|
|
// insert style tags |
148
|
36 |
|
if($outputDecorated === true) |
149
|
|
|
{ |
150
|
|
|
$logo = str_replace('@', "$toText@$toBackground", $logo); |
151
|
|
|
$logo = str_replace('.', '@', $logo); |
152
|
|
|
} |
153
|
|
|
|
154
|
36 |
|
$logo = str_replace(['%', '*'], [$toText, $toBackground], $logo); |
155
|
|
|
|
156
|
36 |
|
return sprintf( |
157
|
36 |
|
'<%s>%s</%s>', |
158
|
|
|
$background, |
159
|
36 |
|
str_replace('VERSION', Application::VERSION, $logo), |
160
|
|
|
$background |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|