1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /** |
||
6 | * This file is part of Laravel Zero. |
||
7 | * |
||
8 | * (c) Nuno Maduro <[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 LaravelZero\Framework\Commands; |
||
15 | |||
16 | use Illuminate\Support\Facades\File; |
||
17 | use Illuminate\Support\Str; |
||
18 | use function sprintf; |
||
19 | |||
20 | final class RenameCommand extends Command |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | protected $signature = 'app:rename {name? : The new name}'; |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | protected $description = 'Set the application name'; |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | 1 | public function handle() |
|
36 | { |
||
37 | 1 | $this->info('Renaming the application...'); |
|
38 | |||
39 | 1 | $this->rename(); |
|
40 | 1 | } |
|
41 | |||
42 | /** |
||
43 | * Updates the binary name and the application |
||
44 | * name on the composer.json. |
||
45 | */ |
||
46 | 1 | private function rename(): RenameCommand |
|
47 | { |
||
48 | 1 | $name = $this->asksForApplicationName(); |
|
49 | |||
50 | 1 | if (File::exists($this->app->basePath($name))) { |
|
51 | $this->app->abort(403, 'Folder or file already exists.'); |
||
52 | } else { |
||
53 | 1 | $this->renameBinary($name) |
|
54 | 1 | ->updateComposer($name); |
|
55 | } |
||
56 | |||
57 | 1 | return $this; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Asks for the application name. |
||
62 | * |
||
63 | * If there is no interaction, we take the folder basename. |
||
64 | */ |
||
65 | 1 | private function asksForApplicationName(): string |
|
66 | { |
||
67 | 1 | if (empty($name = $this->input->getArgument('name'))) { |
|
68 | $name = $this->ask('What is your application name?'); |
||
69 | } |
||
70 | |||
71 | 1 | if (empty($name)) { |
|
72 | $name = trim(basename($this->app->basePath())); |
||
73 | } |
||
74 | |||
75 | 1 | return Str::lower($name); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Update composer json with related information. |
||
80 | */ |
||
81 | 1 | private function updateComposer(string $name): RenameCommand |
|
82 | { |
||
83 | 1 | $this->task( |
|
84 | 1 | 'Updating config/app.php "name" property', |
|
85 | function () use ($name) { |
||
86 | 1 | $neededLine = "'name' => '".Str::ucfirst($this->getCurrentBinaryName())."'"; |
|
87 | |||
88 | 1 | if (! Str::contains($contents = $this->getConfig(), $neededLine)) { |
|
89 | return false; |
||
90 | } |
||
91 | 1 | File::put( |
|
92 | 1 | $this->app->configPath('app.php'), |
|
93 | 1 | Str::replaceFirst( |
|
94 | 1 | $neededLine, |
|
95 | 1 | "'name' => '".Str::ucfirst($name)."'", |
|
96 | 1 | $contents |
|
97 | ) |
||
98 | ); |
||
99 | 1 | } |
|
100 | ); |
||
101 | |||
102 | 1 | $this->task( |
|
103 | 1 | 'Updating composer "bin"', |
|
104 | function () use ($name) { |
||
105 | 1 | $neededLine = '"bin": ["'.$this->getCurrentBinaryName().'"]'; |
|
106 | |||
107 | 1 | if (! Str::contains($contents = $this->getComposer(), $neededLine)) { |
|
108 | return false; |
||
109 | } |
||
110 | |||
111 | 1 | File::put( |
|
112 | 1 | $this->app->basePath('composer.json'), |
|
113 | 1 | Str::replaceFirst( |
|
114 | 1 | $neededLine, |
|
115 | 1 | '"bin": ["'.$name.'"]', |
|
116 | 1 | $contents |
|
117 | ) |
||
118 | ); |
||
119 | 1 | } |
|
120 | ); |
||
121 | |||
122 | 1 | return $this; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Renames the application binary. |
||
127 | */ |
||
128 | 1 | private function renameBinary(string $name): RenameCommand |
|
129 | { |
||
130 | 1 | $this->task( |
|
131 | 1 | sprintf('Renaming application to "%s"', $name), |
|
132 | function () use ($name) { |
||
133 | 1 | return File::move($this->app->basePath($this->getCurrentBinaryName()), $this->app->basePath($name)); |
|
134 | 1 | } |
|
135 | ); |
||
136 | |||
137 | 1 | return $this; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Returns the current binary name. |
||
142 | */ |
||
143 | 1 | private function getCurrentBinaryName(): string |
|
144 | { |
||
145 | 1 | $composer = $this->getComposer(); |
|
146 | |||
147 | 1 | return current(@json_decode($composer)->bin); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Returns the composer.json file contents. |
||
152 | */ |
||
153 | 1 | private function getComposer(): string |
|
154 | { |
||
155 | 1 | $filePath = $this->app->basePath('composer.json'); |
|
156 | |||
157 | 1 | if (! File::exists($filePath)) { |
|
158 | $this->app->abort(400, 'The file composer.json not found'); |
||
159 | } |
||
160 | |||
161 | 1 | return File::get($filePath); |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * Returns the config file contents. |
||
166 | */ |
||
167 | 1 | private function getConfig(): string |
|
168 | { |
||
169 | 1 | $filePath = $this->app->configPath('app.php'); |
|
170 | |||
171 | 1 | if (! File::exists($filePath)) { |
|
172 | $this->app->abort(400, 'The file config/app.php not found'); |
||
173 | } |
||
174 | |||
175 | 1 | return File::get($filePath); |
|
176 | } |
||
177 | } |
||
178 |