|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PHINT package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
|
7
|
|
|
* <https://github.com/adhocore> |
|
8
|
|
|
* |
|
9
|
|
|
* Licensed under MIT license. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ahc\Phint\Console; |
|
13
|
|
|
|
|
14
|
|
|
use Ahc\Cli\Input\Command; |
|
15
|
|
|
use Ahc\Cli\IO\Interactor; |
|
16
|
|
|
use Ahc\Phint\Util\Composer; |
|
17
|
|
|
use Ahc\Phint\Util\Git; |
|
18
|
|
|
use Ahc\Phint\Util\Path; |
|
19
|
|
|
|
|
20
|
|
|
abstract class BaseCommand extends Command |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string Full path of log file */ |
|
23
|
|
|
protected $_logFile; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
$logFile = @\tempnam(\sys_get_temp_dir(), 'PHT') ?: ''; |
|
28
|
|
|
|
|
29
|
|
|
$this->_logFile = $logFile; |
|
30
|
|
|
$this->_pathUtil = new Path; |
|
|
|
|
|
|
31
|
|
|
$this->_git = new Git(null, $logFile); |
|
|
|
|
|
|
32
|
|
|
$this->_composer = new Composer(null, $logFile); |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
$this->defaults(); |
|
35
|
|
|
$this->onConstruct(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function onConstruct() |
|
39
|
|
|
{ |
|
40
|
|
|
// ;) |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function promptAll(Interactor $io, array $promptConfig) |
|
44
|
|
|
{ |
|
45
|
|
|
$template = ['default' => null, 'choices' => [], 'retry' => 1, 'extra' => '', 'restore' => false]; |
|
46
|
|
|
|
|
47
|
|
|
foreach ($this->missingOptions($promptConfig) as $name => $option) { |
|
48
|
|
|
$config = ($promptConfig[$name] ?? []) + $template; |
|
49
|
|
|
$default = ($config['default'] ?? $option->default()) ?: null; |
|
50
|
|
|
|
|
51
|
|
|
if ($config['choices']) { |
|
52
|
|
|
$value = $io->choice($option->desc(), $config['choices'], $default); |
|
53
|
|
|
} else { |
|
54
|
|
|
$value = $io->prompt($option->desc() . $config['extra'], $default, null, $config['retry']); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($config['restore']) { |
|
58
|
|
|
$value = $config['choices'][$value] ?? $value; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->set($name, $value); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function missingOptions(array $config) |
|
66
|
|
|
{ |
|
67
|
|
|
return \array_filter($this->userOptions(), function ($name) use ($config) { |
|
68
|
|
|
return false !== ($config[$name] ?? null) && \in_array($this->$name, [null, []], true); |
|
69
|
|
|
}, \ARRAY_FILTER_USE_KEY); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function getCachePath(): string |
|
73
|
|
|
{ |
|
74
|
|
|
if (!\Phar::running(false)) { |
|
75
|
|
|
return __DIR__ . '/../../.cache'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->_pathUtil->getPhintPath('.cache'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function logging(string $mode = 'start') |
|
82
|
|
|
{ |
|
83
|
|
|
if (!$logFile = $this->_logFile) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ('end' === $mode) { |
|
88
|
|
|
$this->app()->io()->comment("Check detailed logs at $logFile", true); |
|
89
|
|
|
} else { |
|
90
|
|
|
$this->app()->io()->comment("Logging to $logFile", true); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|