1
+ <?php
2
+
3
+ namespace Illuminate \Foundation \Console ;
4
+
5
+ use Illuminate \Console \Command ;
6
+ use Illuminate \Support \Composer ;
7
+ use Symfony \Component \Finder \Finder ;
8
+ use Illuminate \Filesystem \Filesystem ;
9
+ use Symfony \Component \Console \Input \InputArgument ;
10
+
11
+ class AppNameCommand extends Command
12
+ {
13
+ /**
14
+ * The console command name.
15
+ *
16
+ * @var string
17
+ */
18
+ protected $ name = 'app:name ' ;
19
+
20
+ /**
21
+ * The console command description.
22
+ *
23
+ * @var string
24
+ */
25
+ protected $ description = 'Set the application namespace ' ;
26
+
27
+ /**
28
+ * The Composer class instance.
29
+ *
30
+ * @var \Illuminate\Support\Composer
31
+ */
32
+ protected $ composer ;
33
+
34
+ /**
35
+ * The filesystem instance.
36
+ *
37
+ * @var \Illuminate\Filesystem\Filesystem
38
+ */
39
+ protected $ files ;
40
+
41
+ /**
42
+ * Current root application namespace.
43
+ *
44
+ * @var string
45
+ */
46
+ protected $ currentRoot ;
47
+
48
+ /**
49
+ * Create a new key generator command.
50
+ *
51
+ * @param \Illuminate\Support\Composer $composer
52
+ * @param \Illuminate\Filesystem\Filesystem $files
53
+ * @return void
54
+ */
55
+ public function __construct (Composer $ composer , Filesystem $ files )
56
+ {
57
+ parent ::__construct ();
58
+
59
+ $ this ->files = $ files ;
60
+ $ this ->composer = $ composer ;
61
+ }
62
+
63
+ /**
64
+ * Execute the console command.
65
+ *
66
+ * @return void
67
+ */
68
+ public function handle ()
69
+ {
70
+ $ this ->currentRoot = trim ($ this ->laravel ->getNamespace (), '\\' );
71
+
72
+ $ this ->setAppDirectoryNamespace ();
73
+ $ this ->setBootstrapNamespaces ();
74
+ $ this ->setConfigNamespaces ();
75
+ $ this ->setComposerNamespace ();
76
+ $ this ->setDatabaseFactoryNamespaces ();
77
+
78
+ $ this ->info ('Application namespace set! ' );
79
+
80
+ $ this ->composer ->dumpAutoloads ();
81
+
82
+ $ this ->call ('optimize:clear ' );
83
+ }
84
+
85
+ /**
86
+ * Set the namespace on the files in the app directory.
87
+ *
88
+ * @return void
89
+ */
90
+ protected function setAppDirectoryNamespace ()
91
+ {
92
+ $ files = Finder::create ()
93
+ ->in ($ this ->laravel ['path ' ])
94
+ ->contains ($ this ->currentRoot )
95
+ ->name ('*.php ' );
96
+
97
+ foreach ($ files as $ file ) {
98
+ $ this ->replaceNamespace ($ file ->getRealPath ());
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Replace the App namespace at the given path.
104
+ *
105
+ * @param string $path
106
+ * @return void
107
+ */
108
+ protected function replaceNamespace ($ path )
109
+ {
110
+ $ search = [
111
+ 'namespace ' .$ this ->currentRoot .'; ' ,
112
+ $ this ->currentRoot .'\\' ,
113
+ ];
114
+
115
+ $ replace = [
116
+ 'namespace ' .$ this ->argument ('name ' ).'; ' ,
117
+ $ this ->argument ('name ' ).'\\' ,
118
+ ];
119
+
120
+ $ this ->replaceIn ($ path , $ search , $ replace );
121
+ }
122
+
123
+ /**
124
+ * Set the bootstrap namespaces.
125
+ *
126
+ * @return void
127
+ */
128
+ protected function setBootstrapNamespaces ()
129
+ {
130
+ $ search = [
131
+ $ this ->currentRoot .'\\Http ' ,
132
+ $ this ->currentRoot .'\\Console ' ,
133
+ $ this ->currentRoot .'\\Exceptions ' ,
134
+ ];
135
+
136
+ $ replace = [
137
+ $ this ->argument ('name ' ).'\\Http ' ,
138
+ $ this ->argument ('name ' ).'\\Console ' ,
139
+ $ this ->argument ('name ' ).'\\Exceptions ' ,
140
+ ];
141
+
142
+ $ this ->replaceIn ($ this ->getBootstrapPath (), $ search , $ replace );
143
+ }
144
+
145
+ /**
146
+ * Set the namespace in the appropriate configuration files.
147
+ *
148
+ * @return void
149
+ */
150
+ protected function setConfigNamespaces ()
151
+ {
152
+ $ this ->setAppConfigNamespaces ();
153
+ $ this ->setAuthConfigNamespace ();
154
+ $ this ->setServicesConfigNamespace ();
155
+ }
156
+
157
+ /**
158
+ * Set the application provider namespaces.
159
+ *
160
+ * @return void
161
+ */
162
+ protected function setAppConfigNamespaces ()
163
+ {
164
+ $ search = [
165
+ $ this ->currentRoot .'\\Providers ' ,
166
+ $ this ->currentRoot .'\\Http \\Controllers \\' ,
167
+ ];
168
+
169
+ $ replace = [
170
+ $ this ->argument ('name ' ).'\\Providers ' ,
171
+ $ this ->argument ('name ' ).'\\Http \\Controllers \\' ,
172
+ ];
173
+
174
+ $ this ->replaceIn ($ this ->getConfigPath ('app ' ), $ search , $ replace );
175
+ }
176
+
177
+ /**
178
+ * Set the authentication User namespace.
179
+ *
180
+ * @return void
181
+ */
182
+ protected function setAuthConfigNamespace ()
183
+ {
184
+ $ this ->replaceIn (
185
+ $ this ->getConfigPath ('auth ' ),
186
+ $ this ->currentRoot .'\\User ' ,
187
+ $ this ->argument ('name ' ).'\\User '
188
+ );
189
+ }
190
+
191
+ /**
192
+ * Set the services User namespace.
193
+ *
194
+ * @return void
195
+ */
196
+ protected function setServicesConfigNamespace ()
197
+ {
198
+ $ this ->replaceIn (
199
+ $ this ->getConfigPath ('services ' ),
200
+ $ this ->currentRoot .'\\User ' ,
201
+ $ this ->argument ('name ' ).'\\User '
202
+ );
203
+ }
204
+
205
+ /**
206
+ * Set the PSR-4 namespace in the Composer file.
207
+ *
208
+ * @return void
209
+ */
210
+ protected function setComposerNamespace ()
211
+ {
212
+ $ this ->replaceIn (
213
+ $ this ->getComposerPath (),
214
+ str_replace ('\\' , '\\\\' , $ this ->currentRoot ).'\\\\' ,
215
+ str_replace ('\\' , '\\\\' , $ this ->argument ('name ' )).'\\\\'
216
+ );
217
+ }
218
+
219
+ /**
220
+ * Set the namespace in database factory files.
221
+ *
222
+ * @return void
223
+ */
224
+ protected function setDatabaseFactoryNamespaces ()
225
+ {
226
+ $ files = Finder::create ()
227
+ ->in (database_path ('factories ' ))
228
+ ->contains ($ this ->currentRoot )
229
+ ->name ('*.php ' );
230
+
231
+ foreach ($ files as $ file ) {
232
+ $ this ->replaceIn (
233
+ $ file ->getRealPath (),
234
+ $ this ->currentRoot , $ this ->argument ('name ' )
235
+ );
236
+ }
237
+ }
238
+
239
+ /**
240
+ * Replace the given string in the given file.
241
+ *
242
+ * @param string $path
243
+ * @param string|array $search
244
+ * @param string|array $replace
245
+ * @return void
246
+ */
247
+ protected function replaceIn ($ path , $ search , $ replace )
248
+ {
249
+ if ($ this ->files ->exists ($ path )) {
250
+ $ this ->files ->put ($ path , str_replace ($ search , $ replace , $ this ->files ->get ($ path )));
251
+ }
252
+ }
253
+
254
+ /**
255
+ * Get the path to the bootstrap/app.php file.
256
+ *
257
+ * @return string
258
+ */
259
+ protected function getBootstrapPath ()
260
+ {
261
+ return $ this ->laravel ->bootstrapPath ().'/app.php ' ;
262
+ }
263
+
264
+ /**
265
+ * Get the path to the Composer.json file.
266
+ *
267
+ * @return string
268
+ */
269
+ protected function getComposerPath ()
270
+ {
271
+ return base_path ('composer.json ' );
272
+ }
273
+
274
+ /**
275
+ * Get the path to the given configuration file.
276
+ *
277
+ * @param string $name
278
+ * @return string
279
+ */
280
+ protected function getConfigPath ($ name )
281
+ {
282
+ return $ this ->laravel ['path.config ' ].'/ ' .$ name .'.php ' ;
283
+ }
284
+
285
+ /**
286
+ * Get the console command arguments.
287
+ *
288
+ * @return array
289
+ */
290
+ protected function getArguments ()
291
+ {
292
+ return [
293
+ ['name ' , InputArgument::REQUIRED , 'The desired namespace ' ],
294
+ ];
295
+ }
296
+ }
0 commit comments