1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\ui; |
3
|
|
|
|
4
|
|
|
use keeko\tools\helpers\InitCommandHelperTrait; |
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Question\Question; |
7
|
|
|
|
8
|
|
|
class InitUI extends AbstractUI { |
9
|
|
|
|
10
|
|
|
use InitCommandHelperTrait; |
11
|
|
|
|
12
|
|
|
public function show() { |
13
|
|
|
$input = $this->io->getInput(); |
14
|
|
|
$output = $this->io->getOutput(); |
15
|
|
|
$formatter = $this->command->getHelperSet()->get('formatter'); |
16
|
|
|
|
17
|
|
|
// send welcome |
18
|
|
|
$output->writeln([ |
19
|
|
|
'', |
20
|
|
|
$formatter->formatBlock('Welcome to the Keeko initializer', 'bg=blue;fg=white', true), |
21
|
|
|
'' |
22
|
|
|
]); |
23
|
|
|
$output->writeln([ |
24
|
|
|
'', |
25
|
|
|
'This command will guide you through creating your Keeko composer package.', |
26
|
|
|
'', |
27
|
|
|
]); |
28
|
|
|
|
29
|
|
|
// asking for a options |
30
|
|
|
$name = $this->getName(); |
31
|
|
|
$package = $this->getService()->getPackageService()->getPackage(); |
32
|
|
|
$package->setFullName($name); |
33
|
|
|
|
34
|
|
|
$input->setOption('name', $name); |
35
|
|
|
$input->setOption('description', $this->getDescription()); |
36
|
|
|
$input->setOption('author', $this->getAuthor()); |
37
|
|
|
$input->setOption('license', $this->getLicense()); |
38
|
|
|
|
39
|
|
|
$type = $this->getType(); |
40
|
|
|
$input->setOption('type', $type); |
41
|
|
|
|
42
|
|
|
// KEEKO values |
43
|
|
|
$output->writeln([ |
44
|
|
|
'', |
45
|
|
|
'Information for Keeko ' . ucfirst($type), |
46
|
|
|
'' |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
$input->setOption('title', $this->getTitle()); |
50
|
|
|
$input->setOption('classname', $this->getClass()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function getName() { |
|
|
|
|
54
|
|
|
$input = $this->io->getInput(); |
55
|
|
|
$force = $input->getOption('force'); |
56
|
|
|
$package = $this->getService()->getPackageService()->getPackage(); |
57
|
|
|
|
58
|
|
|
$name = $this->getPackageName(); |
59
|
|
|
$askName = $name === null; |
60
|
|
|
if ($name === null) { |
61
|
|
|
$git = $this->getGitConfig(); |
62
|
|
|
$cwd = realpath('.'); |
63
|
|
|
$name = basename($cwd); |
64
|
|
|
$name = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $name); |
65
|
|
|
$name = strtolower($name); |
66
|
|
|
$localName = $package->getFullName(); |
67
|
|
|
if (!empty($localName)) { |
68
|
|
|
$name = $package->getFullName(); |
69
|
|
|
} else if (isset($git['github.user'])) { |
70
|
|
|
$name = $git['github.user'] . '/' . $name; |
71
|
|
|
} elseif (!empty($_SERVER['USERNAME'])) { |
72
|
|
|
$name = $_SERVER['USERNAME'] . '/' . $name; |
73
|
|
|
} elseif (get_current_user()) { |
74
|
|
|
$name = get_current_user() . '/' . $name; |
75
|
|
|
} else { |
76
|
|
|
// package names must be in the format foo/bar |
77
|
|
|
$name = $name . '/' . $name; |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
|
|
$this->validateName($name); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// asking for the name |
84
|
|
|
if ($askName || $force) { |
85
|
|
|
$name = $this->askQuestion(new Question('Package name (<vendor>/<name>)', $name)); |
86
|
|
|
$this->validateName($name); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $name; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function getDescription() { |
93
|
|
|
$force = $this->io->getInput()->getOption('force'); |
94
|
|
|
$desc = $this->getPackageDescription(); |
95
|
|
|
if ($desc === null || $force) { |
96
|
|
|
$desc = $this->askQuestion(new Question('Description', $desc)); |
97
|
|
|
} |
98
|
|
|
return $desc; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function getAuthor() { |
102
|
|
|
$input = $this->io->getInput(); |
103
|
|
|
$force = $input->getOption('force'); |
104
|
|
|
$package = $this->getService()->getPackageService()->getPackage(); |
105
|
|
|
$git = $this->getGitConfig(); |
106
|
|
|
$author = null; |
107
|
|
|
if ($package->getAuthors()->isEmpty() || $force) { |
108
|
|
|
$author = $input->getOption('author'); |
109
|
|
|
if ($author === null && isset($git['user.name'])) { |
110
|
|
|
$author = $git['user.name']; |
111
|
|
|
|
112
|
|
|
if (isset($git['user.email'])) { |
113
|
|
|
$author = sprintf('%s <%s>', $git['user.name'], $git['user.email']); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$author = $this->askQuestion(new Question('Author', $author)); |
118
|
|
|
} |
119
|
|
|
return $author; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function getType() { |
123
|
|
|
$force = $this->io->getInput()->getOption('force'); |
124
|
|
|
$type = $this->getPackageType(); |
125
|
|
|
if ($type === null || $force) { |
126
|
|
|
$types = ['module', 'app']; |
127
|
|
|
$question = new Question('Package type (module|app)', $type); |
128
|
|
|
$question->setAutocompleterValues($types); |
129
|
|
|
$question->setValidator(function($answer) use ($types) { |
130
|
|
|
if (!in_array($answer, $types)) { |
131
|
|
|
throw new \RuntimeException('The name of the type should be one of: ' . |
132
|
|
|
implode(',', $types)); |
133
|
|
|
} |
134
|
|
|
return $answer; |
135
|
|
|
}); |
136
|
|
|
$question->setMaxAttempts(2); |
137
|
|
|
$type = $this->askQuestion($question); |
138
|
|
|
} |
139
|
|
|
return $type; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function getLicense() { |
143
|
|
|
$force = $this->io->getInput()->getOption('force'); |
144
|
|
|
$license = $this->getPackageLicense(); |
145
|
|
|
if ($license === null || $force) { |
146
|
|
|
$license = $this->askQuestion(new Question('License', $license)); |
147
|
|
|
} |
148
|
|
|
return $license; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
private function getTitle() { |
152
|
|
|
$force = $this->io->getInput()->getOption('force'); |
153
|
|
|
$title = $this->getPackageTitle(); |
154
|
|
|
if ($title === null || $force) { |
155
|
|
|
$title = $this->askQuestion(new Question('Title', $title)); |
156
|
|
|
} |
157
|
|
|
return $title; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
private function getClass() { |
161
|
|
|
$force = $this->io->getInput()->getOption('force'); |
162
|
|
|
$classname = $this->getPackageClass(); |
163
|
|
|
if ($classname === null || $force) { |
164
|
|
|
$classname = $this->askQuestion(new Question('Class', $classname)); |
165
|
|
|
} |
166
|
|
|
return $classname; |
167
|
|
|
} |
168
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: