1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acacha\Llum\Console; |
4
|
|
|
|
5
|
|
|
use Acacha\Llum\Compiler\RCFileCompiler; |
6
|
|
|
use Acacha\Llum\Filesystem\Filesystem; |
7
|
|
|
use Acacha\Llum\Github\GithubAPI; |
8
|
|
|
use Acacha\Llum\LlumRCFile; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
12
|
|
|
use Symfony\Component\Console\Question\Question; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class InitCommand |
16
|
|
|
* @package Acacha\Llum\Console |
17
|
|
|
*/ |
18
|
|
|
class InitCommand extends LlumCommand |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Github api service class. |
23
|
|
|
* |
24
|
|
|
* @var GithubAPI |
25
|
|
|
*/ |
26
|
|
|
protected $api; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Filesystem. |
30
|
|
|
* |
31
|
|
|
* @var Filesystem |
32
|
|
|
*/ |
33
|
|
|
protected $filesystem; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Compiler for llumrc file. |
37
|
|
|
* |
38
|
|
|
* @var RCFileCompiler |
39
|
|
|
*/ |
40
|
|
|
protected $compiler; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Command name. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $commandName = 'init'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Command description. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $commandDescription = 'Create ~/.llumrc file with llum configuration data'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Method to execute. |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $method = 'init'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Github username |
65
|
|
|
* |
66
|
|
|
* @var |
67
|
|
|
*/ |
68
|
|
|
protected $github_username; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Github token. |
72
|
|
|
* |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $github_token = ""; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get path to stub. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function getStubPath() { |
83
|
|
|
return __DIR__ . '/stubs/llumrc.stub'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* InitCommand constructor. |
89
|
|
|
* |
90
|
|
|
* @param Filesystem $filesystem |
91
|
|
|
* @param RCFileCompiler $compiler |
92
|
|
|
* @param GithubAPI $api |
93
|
|
|
*/ |
94
|
|
|
public function __construct(Filesystem $filesystem, RCFileCompiler $compiler, GithubAPI $api) |
95
|
|
|
{ |
96
|
|
|
parent::__construct(); |
97
|
|
|
$this->filesystem = $filesystem; |
98
|
|
|
$this->compiler = $compiler; |
99
|
|
|
$this->api = $api; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* init command. |
104
|
|
|
*/ |
105
|
|
|
public function init(InputInterface $input, OutputInterface $output) |
106
|
|
|
{ |
107
|
|
|
try { |
108
|
|
|
$this->executeWizard($input,$output); |
109
|
|
|
$this->filesystem->overwrite( |
110
|
|
|
(new LlumRCFile())->path(), |
111
|
|
|
$this->compiler->compile( |
112
|
|
|
$this->filesystem->get($this->getStubPath()), |
113
|
|
|
$this->data)); |
|
|
|
|
114
|
|
|
} catch (\Exception $e) { |
115
|
|
|
print_r($e->xdebug_message); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Executes wizard. |
121
|
|
|
* |
122
|
|
|
* @param InputInterface $input |
123
|
|
|
* @param OutputInterface $output |
124
|
|
|
*/ |
125
|
|
|
protected function executeWizard(InputInterface $input, OutputInterface $output){ |
126
|
|
|
|
127
|
|
|
$this->askGithubUsername($input,$output); |
128
|
|
|
$this->askGithubToken($input,$output); |
129
|
|
|
|
130
|
|
|
$this->data = [ |
131
|
|
|
"GITHUB_USERNAME" => $this->github_username, |
132
|
|
|
"GITHUB_TOKEN" => $this->github_token, |
133
|
|
|
"GITHUB_TOKEN_NAME" => $this->api->tokenName(), |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param InputInterface $input |
139
|
|
|
* @param OutputInterface $output |
140
|
|
|
*/ |
141
|
|
|
public function askGithubUsername(InputInterface $input, OutputInterface $output) |
142
|
|
|
{ |
143
|
|
|
$defaultusername = $this->defaultUsername(); |
144
|
|
|
$question = new Question('<info>Please enter your github username (' . $defaultusername . ') ? </info>', $defaultusername); |
145
|
|
|
$this->github_username = $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
protected function defaultUsername() |
152
|
|
|
{ |
153
|
|
|
return get_current_user(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param InputInterface $input |
158
|
|
|
* @param OutputInterface $output |
159
|
|
|
*/ |
160
|
|
|
public function askGithubToken(InputInterface $input, OutputInterface $output) |
161
|
|
|
{ |
162
|
|
|
$question = new ConfirmationQuestion('<info>Do you want to use our assistant to obtain token via Github API (Y/n)? </info>', true); |
163
|
|
|
if ($this->getHelper('question')->ask($input, $output, $question)) { |
|
|
|
|
164
|
|
|
$this->github_token =$this->api->getPersonalToken( |
165
|
|
|
$this->github_username, |
166
|
|
|
$this->askGithubPassword($input,$output)); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param InputInterface $input |
172
|
|
|
* @param OutputInterface $output |
173
|
|
|
* @return mixed |
174
|
|
|
*/ |
175
|
|
|
protected function askGithubPassword(InputInterface $input, OutputInterface $output) { |
176
|
|
|
$question = new Question('<info>Github password? </info>'); |
177
|
|
|
$question->setHidden(true); |
178
|
|
|
$question->setHiddenFallback(false); |
179
|
|
|
|
180
|
|
|
return $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: