|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Zero Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Nuno Maduro <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace NunoMaduro\ZeroFramework\Commands; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Support\Str; |
|
15
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The is the Zero Framework rename command class. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Nuno Maduro <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Renamer extends AbstractCommand |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The name and signature of the console command. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $signature = 'install'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The console command description. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $description = 'Rename your application'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Holds an instance of SymfonyStyle. |
|
41
|
|
|
* |
|
42
|
|
|
* @var \Symfony\Component\Console\Style\SymfonyStyle; |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $style; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Execute the console command. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function handle(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->style = new SymfonyStyle($this->input, $this->output); |
|
52
|
|
|
|
|
53
|
|
|
$this->displayWelcomeMessage() |
|
54
|
|
|
->install(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Configure the command options. |
|
59
|
|
|
* |
|
60
|
|
|
* Ask for the name of the build. |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function configure(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->addArgument('name', InputArgument::OPTIONAL); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Perform project modifications in order to apply the |
|
69
|
|
|
* application name on the composer and on the binary. |
|
70
|
|
|
* |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function install(): Rename |
|
74
|
|
|
{ |
|
75
|
|
|
$name = $this->asksForApplicationName(); |
|
76
|
|
|
|
|
77
|
|
|
return $this->rename($name) |
|
78
|
|
|
->updateComposer($name); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Display an welcome message. |
|
83
|
|
|
* |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function displayWelcomeMessage(): Rename |
|
87
|
|
|
{ |
|
88
|
|
|
$this->style->title('Crafting application...'); |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Asks for the application name. |
|
95
|
|
|
* |
|
96
|
|
|
* If there is no interaction, we take the folder basename. |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function asksForApplicationName(): string |
|
101
|
|
|
{ |
|
102
|
|
|
if (empty($name = $this->input->getArgument('name'))) { |
|
103
|
|
|
$name = $this->ask('What is your application name?'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (empty($name)) { |
|
107
|
|
|
$name = trim(basename(BASE_PATH)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return Str::lower($name); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Update composer json with related information. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $name |
|
117
|
|
|
* |
|
118
|
|
|
* @return $this |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function updateComposer(string $name): Rename |
|
121
|
|
|
{ |
|
122
|
|
|
$this->setComposer( |
|
123
|
|
|
Str::replaceFirst( |
|
124
|
|
|
'"bin": ["'.$this->getCurrentBinaryName().'"]', |
|
125
|
|
|
'"bin": ["'.$name.'"]', |
|
126
|
|
|
$this->getComposer() |
|
127
|
|
|
) |
|
128
|
|
|
); |
|
129
|
|
|
|
|
130
|
|
|
$this->output->writeln('Updating composer: <info>✔</info>'); |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Renames the application binary. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $name |
|
139
|
|
|
* |
|
140
|
|
|
* @return $this |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function rename(string $name): Rename |
|
143
|
|
|
{ |
|
144
|
|
|
rename(BASE_PATH.'/'.$this->getCurrentBinaryName(), BASE_PATH.'/'.$name); |
|
145
|
|
|
|
|
146
|
|
|
$this->output->writeln('Renaming application: <info>✔</info>'); |
|
147
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Set composer file. |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $composer |
|
155
|
|
|
* |
|
156
|
|
|
* @return $this |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function setComposer(string $composer): Rename |
|
159
|
|
|
{ |
|
160
|
|
|
file_put_contents(BASE_PATH.'/composer.json', $composer); |
|
161
|
|
|
|
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Returns the current binary name. |
|
167
|
|
|
* |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function getCurrentBinaryName(): string |
|
171
|
|
|
{ |
|
172
|
|
|
$composer = $this->getComposer(); |
|
173
|
|
|
|
|
174
|
|
|
return current(@json_decode($composer)->bin); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get composer file. |
|
179
|
|
|
* |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function getComposer(): string |
|
183
|
|
|
{ |
|
184
|
|
|
$file = BASE_PATH.'/composer.json'; |
|
185
|
|
|
|
|
186
|
|
|
if (! file_exists($file)) { |
|
187
|
|
|
$this->error("You can't perform a install."); |
|
188
|
|
|
exit(0); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return file_get_contents($file); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|