1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Cecil. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Cecil\Command; |
15
|
|
|
|
16
|
|
|
use Cecil\Builder; |
17
|
|
|
use Cecil\Exception\RuntimeException; |
18
|
|
|
use Cecil\Logger\ConsoleLogger; |
19
|
|
|
use Cecil\Util; |
20
|
|
|
use Symfony\Component\Console\Command\Command; |
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
23
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
24
|
|
|
use Symfony\Component\Filesystem\Path; |
25
|
|
|
use Symfony\Component\Process\Process; |
26
|
|
|
use Symfony\Component\Validator\Constraints\Url; |
27
|
|
|
use Symfony\Component\Validator\Validation; |
28
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
29
|
|
|
use Symfony\Component\Yaml\Yaml; |
30
|
|
|
|
31
|
|
|
class AbstractCommand extends Command |
32
|
|
|
{ |
33
|
|
|
public const CONFIG_FILE = ['cecil.yml', 'config.yml']; |
34
|
|
|
public const TMP_DIR = '.cecil'; |
35
|
|
|
|
36
|
|
|
/** @var InputInterface */ |
37
|
|
|
protected $input; |
38
|
|
|
|
39
|
|
|
/** @var OutputInterface */ |
40
|
|
|
protected $output; |
41
|
|
|
|
42
|
|
|
/** @var SymfonyStyle */ |
43
|
|
|
protected $io; |
44
|
|
|
|
45
|
|
|
/** @var null|string */ |
46
|
|
|
private $path = null; |
47
|
|
|
|
48
|
|
|
/** @var array */ |
49
|
|
|
private $configFiles; |
50
|
|
|
|
51
|
|
|
/** @var array */ |
52
|
|
|
private $config; |
53
|
|
|
|
54
|
|
|
/** @var Builder */ |
55
|
|
|
private $builder; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
61
|
|
|
{ |
62
|
|
|
$this->input = $input; |
63
|
|
|
$this->output = $output; |
64
|
|
|
$this->io = new SymfonyStyle($input, $output); |
65
|
|
|
|
66
|
|
|
// set up configuration |
67
|
|
|
if (!\in_array($this->getName(), ['new:site', 'self-update'])) { |
68
|
|
|
// default configuration file |
69
|
|
|
$this->configFiles[$this->findConfigFile('name')] = $this->findConfigFile('path'); |
70
|
|
|
// from --config=<file> |
71
|
|
|
if ($input->hasOption('config') && $input->getOption('config') !== null) { |
72
|
|
|
foreach (explode(',', (string) $input->getOption('config')) as $configFile) { |
73
|
|
|
$this->configFiles[$configFile] = realpath($configFile); |
74
|
|
|
if (!Util\File::getFS()->isAbsolutePath($configFile)) { |
75
|
|
|
$this->configFiles[$configFile] = realpath(Util::joinFile($this->getPath(), $configFile)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
$this->configFiles = array_unique($this->configFiles); |
79
|
|
|
} |
80
|
|
|
// checks file(s) |
81
|
|
|
foreach ($this->configFiles as $fileName => $filePath) { |
82
|
|
|
if ($filePath === false || !file_exists($filePath)) { |
83
|
|
|
unset($this->configFiles[$fileName]); |
84
|
|
|
$this->getBuilder()->getLogger()->error(sprintf('Could not find configuration file "%s".', $fileName)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
if ($this->getName() == 'new:site') { |
89
|
|
|
Util\File::getFS()->mkdir($this->getPath(true)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
parent::initialize($input, $output); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function run(InputInterface $input, OutputInterface $output): int |
99
|
|
|
{ |
100
|
|
|
// disable debug mode if a verbosity level is specified |
101
|
|
|
if ($output->getVerbosity() != OutputInterface::VERBOSITY_NORMAL) { |
102
|
|
|
putenv('CECIL_DEBUG=false'); |
103
|
|
|
} |
104
|
|
|
// force verbosity level to "debug" in debug mode |
105
|
|
|
if (getenv('CECIL_DEBUG') == 'true') { |
106
|
|
|
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); |
107
|
|
|
} |
108
|
|
|
if ($output->isDebug()) { |
109
|
|
|
// set env. variable in debug mode |
110
|
|
|
putenv('CECIL_DEBUG=true'); |
111
|
|
|
|
112
|
|
|
return parent::run($input, $output); |
113
|
|
|
} |
114
|
|
|
// simplified error message |
115
|
|
|
try { |
116
|
|
|
return parent::run($input, $output); |
117
|
|
|
} catch (\Exception $e) { |
118
|
|
|
if ($this->io === null) { |
119
|
|
|
$this->io = new SymfonyStyle($input, $output); |
120
|
|
|
} |
121
|
|
|
$this->io->error($e->getMessage()); |
122
|
|
|
|
123
|
|
|
exit(1); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns the working path. |
129
|
|
|
*/ |
130
|
|
|
protected function getPath(bool $create = false): ?string |
131
|
|
|
{ |
132
|
|
|
if ($this->path === null) { |
133
|
|
|
try { |
134
|
|
|
// get working directory by default |
135
|
|
|
if (false === $this->path = getcwd()) { |
136
|
|
|
throw new \Exception('Can\'t get current working directory.'); |
137
|
|
|
} |
138
|
|
|
// ... or path |
139
|
|
|
if ($this->input->getArgument('path') !== null) { |
140
|
|
|
$this->path = Path::canonicalize($this->input->getArgument('path')); |
141
|
|
|
} |
142
|
|
|
// try to get canonicalized absolute path |
143
|
|
|
if (!$create) { |
144
|
|
|
if (realpath($this->path) === false) { |
145
|
|
|
throw new \Exception(sprintf('The given path "%s" is not valid.', $this->path)); |
146
|
|
|
} |
147
|
|
|
$this->path = realpath($this->path); |
148
|
|
|
} |
149
|
|
|
} catch (\Exception $e) { |
150
|
|
|
throw new \Exception($e->getMessage()); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->path; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns the configuration file name or path, if file exists, otherwise default name or false. |
159
|
|
|
*/ |
160
|
|
|
protected function findConfigFile(string $nameOrPath): string|false |
161
|
|
|
{ |
162
|
|
|
$config = [ |
163
|
|
|
'name' => self::CONFIG_FILE[0], |
164
|
|
|
'path' => false, |
165
|
|
|
]; |
166
|
|
|
foreach (self::CONFIG_FILE as $configFileName) { |
167
|
|
|
if (($configFilePath = realpath(Util::joinFile($this->getPath(), $configFileName))) !== false) { |
168
|
|
|
$config = [ |
169
|
|
|
'name' => $configFileName, |
170
|
|
|
'path' => $configFilePath, |
171
|
|
|
]; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $config[$nameOrPath]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns config file(s) path. |
180
|
|
|
*/ |
181
|
|
|
protected function getConfigFiles(): ?array |
182
|
|
|
{ |
183
|
|
|
return $this->configFiles; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Creates or returns a Builder instance. |
188
|
|
|
* |
189
|
|
|
* @throws RuntimeException |
190
|
|
|
*/ |
191
|
|
|
protected function getBuilder(array $config = []): Builder |
192
|
|
|
{ |
193
|
|
|
try { |
194
|
|
|
// config |
195
|
|
|
if ($this->config === null) { |
196
|
|
|
$siteConfig = []; |
197
|
|
|
foreach ($this->getConfigFiles() as $fileName => $filePath) { |
198
|
|
|
if ($filePath === false || false === $configContent = Util\File::fileGetContents($filePath)) { |
199
|
|
|
throw new RuntimeException(sprintf('Can\'t read configuration file "%s".', $fileName)); |
200
|
|
|
} |
201
|
|
|
$siteConfig = array_replace_recursive($siteConfig, (array) Yaml::parse($configContent)); |
202
|
|
|
} |
203
|
|
|
$this->config = array_replace_recursive($siteConfig, $config); |
204
|
|
|
} |
205
|
|
|
// builder |
206
|
|
|
if ($this->builder === null) { |
207
|
|
|
$this->builder = (new Builder($this->config, new ConsoleLogger($this->output))) |
208
|
|
|
->setSourceDir($this->getPath()) |
209
|
|
|
->setDestinationDir($this->getPath()); |
210
|
|
|
} |
211
|
|
|
} catch (ParseException $e) { |
212
|
|
|
throw new RuntimeException(sprintf('Configuration parsing error: %s', $e->getMessage())); |
213
|
|
|
} catch (\Exception $e) { |
214
|
|
|
throw new RuntimeException($e->getMessage()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this->builder; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Opens path with editor. |
222
|
|
|
* |
223
|
|
|
* @throws RuntimeException |
224
|
|
|
*/ |
225
|
|
|
protected function openEditor(string $path, string $editor): void |
226
|
|
|
{ |
227
|
|
|
$command = sprintf('%s "%s"', $editor, $path); |
228
|
|
|
switch (Util\Plateform::getOS()) { |
229
|
|
|
case Util\Plateform::OS_WIN: |
230
|
|
|
$command = sprintf('start /B "" %s "%s"', $editor, $path); |
231
|
|
|
break; |
232
|
|
|
case Util\Plateform::OS_OSX: |
233
|
|
|
// Typora on macOS |
234
|
|
|
if ($editor == 'typora') { |
235
|
|
|
$command = sprintf('open -a typora "%s"', $path); |
236
|
|
|
} |
237
|
|
|
break; |
238
|
|
|
} |
239
|
|
|
$process = Process::fromShellCommandline($command); |
240
|
|
|
$process->run(); |
241
|
|
|
if (!$process->isSuccessful()) { |
242
|
|
|
throw new RuntimeException(sprintf('Can\'t use "%s" editor.', $editor)); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Validate URL. |
248
|
|
|
* |
249
|
|
|
* @throws RuntimeException |
250
|
|
|
*/ |
251
|
|
|
public static function validateUrl(string $url): string |
252
|
|
|
{ |
253
|
|
|
$validator = Validation::createValidator(); |
254
|
|
|
$violations = $validator->validate($url, new Url()); |
255
|
|
|
if (\count($violations) > 0) { |
256
|
|
|
foreach ($violations as $violation) { |
257
|
|
|
throw new RuntimeException($violation->getMessage()); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
return rtrim($url, '/') . '/'; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|