1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jumilla\Addomnipot\Laravel\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
use Jumilla\Addomnipot\Laravel\Environment as AddonEnvironment; |
9
|
|
|
use Jumilla\Addomnipot\Laravel\Addon; |
10
|
|
|
use UnexpectedValueException; |
11
|
|
|
|
12
|
|
|
class AddonNameCommand extends Command |
13
|
|
|
{ |
14
|
|
|
use Functions; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command signature. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'addon:name |
22
|
|
|
{addon : The desired addon.} |
23
|
|
|
{namespace : The desired namespace.} |
24
|
|
|
{--force : Force remove.} |
25
|
|
|
'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Set the addon PHP namespace'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
36
|
|
|
*/ |
37
|
|
|
protected $filesystem; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Jumilla\Addomnipot\Laravel\Addons\Addon |
41
|
|
|
*/ |
42
|
|
|
protected $addon; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $currentNamespace; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $newNamespace; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Execute the console command. |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
3 |
|
public function handle(Filesystem $filesystem, AddonEnvironment $env) |
60
|
|
|
{ |
61
|
3 |
|
$this->filesystem = $filesystem; |
62
|
|
|
|
63
|
3 |
|
$addon_name = $this->argument('addon'); |
64
|
|
|
|
65
|
3 |
|
$this->addon = $env->addon($addon_name); |
66
|
|
|
|
67
|
|
|
// check addon |
68
|
3 |
|
if ($this->addon === null) { |
69
|
1 |
|
throw new UnexpectedValueException("Addon '$addon_name' is not found."); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
$this->currentNamespace = trim($this->addon->phpNamespace(), '\\'); |
73
|
|
|
|
74
|
2 |
|
$this->newNamespace = str_replace('/', '\\', $this->argument('namespace')); |
75
|
|
|
|
76
|
|
|
// check namespace |
77
|
2 |
|
if (! $this->validPhpNamespace($this->newNamespace)) { |
78
|
|
|
throw new UnexpectedValueException("PHP namespace '{$this->newNamespace}' is invalid."); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// confirm |
82
|
2 |
|
$this->line('Addon name: '.$addon_name); |
83
|
2 |
|
$this->line('Addon path: '.$this->addon->relativePath($this->laravel)); |
84
|
2 |
|
$this->line('PHP namespace: '.$this->newNamespace); |
85
|
|
|
|
86
|
2 |
|
if (!$this->option('force')) { |
87
|
1 |
|
if (!$this->confirm('Are you sure? [y/N]', false)) { |
88
|
1 |
|
$this->comment('canceled'); |
89
|
|
|
|
90
|
1 |
|
return; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$this->setAddonNamespaces(); |
95
|
|
|
|
96
|
1 |
|
$this->setComposerNamespace(); |
97
|
|
|
|
98
|
1 |
|
$this->setClassNamespace(); |
99
|
|
|
|
100
|
1 |
|
$this->setConfigNamespaces(); |
101
|
|
|
|
102
|
1 |
|
$this->info('Addon namespace set!'); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the namespace in addon.php, adon.json file. |
107
|
|
|
*/ |
108
|
1 |
|
protected function setAddonNamespaces() |
109
|
|
|
{ |
110
|
1 |
|
$this->setAddonConfigNamespaces(); |
111
|
1 |
|
$this->setAddonJsonNamespaces(); |
112
|
1 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the namespace in addon.php file. |
116
|
|
|
*/ |
117
|
1 |
|
protected function setAddonConfigNamespaces() |
118
|
|
|
{ |
119
|
1 |
|
if (file_exists($this->addon->path('addon.php'))) { |
120
|
|
|
$search = [ |
121
|
1 |
|
"namespace {$this->currentNamespace}", |
122
|
1 |
|
"'namespace' => '{$this->currentNamespace}'", |
123
|
1 |
|
"'{$this->currentNamespace}\\", |
124
|
1 |
|
"\"{$this->currentNamespace}\\", |
125
|
1 |
|
"\\{$this->currentNamespace}\\", |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
$replace = [ |
129
|
1 |
|
"namespace {$this->newNamespace}", |
130
|
1 |
|
"'namespace' => '{$this->newNamespace}'", |
131
|
1 |
|
"'{$this->newNamespace}\\", |
132
|
1 |
|
"\"{$this->newNamespace}\\", |
133
|
1 |
|
"\\{$this->newNamespace}\\", |
134
|
|
|
]; |
135
|
|
|
|
136
|
1 |
|
$this->replaceIn($this->addon->path('addon.php'), $search, $replace); |
137
|
|
|
} |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set the namespace in addon.json file. |
142
|
|
|
*/ |
143
|
1 |
|
protected function setAddonJsonNamespaces() |
144
|
|
|
{ |
145
|
1 |
|
if (file_exists($this->addon->path('addon.json'))) { |
146
|
|
|
$currentNamespace = str_replace('\\', '\\\\', $this->currentNamespace); |
147
|
|
|
$newNamespace = str_replace('\\', '\\\\', $this->newNamespace); |
148
|
|
|
|
149
|
|
|
$search = [ |
150
|
|
|
"\"namespace\": \"{$currentNamespace}\"", |
151
|
|
|
"\"{$currentNamespace}\\\\", |
152
|
|
|
"\\\\{$currentNamespace}\\\\", |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
$replace = [ |
156
|
|
|
"\"namespace\": \"{$newNamespace}\"", |
157
|
|
|
"\"{$newNamespace}\\\\", |
158
|
|
|
"\\\\{$newNamespace}\\\\", |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
$this->replaceIn($this->addon->path('addon.json'), $search, $replace); |
162
|
|
|
} |
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set the PSR-4 namespace in the Composer file. |
167
|
|
|
*/ |
168
|
1 |
|
protected function setComposerNamespace() |
169
|
|
|
{ |
170
|
1 |
|
if (file_exists($this->addon->path('composer.json'))) { |
171
|
|
|
$this->replaceIn( |
172
|
|
|
$this->addon->path('composer.json'), $this->currentNamespace.'\\\\', str_replace('\\', '\\\\', $this->newNamespace).'\\\\' |
173
|
|
|
); |
174
|
|
|
} |
175
|
1 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Set the namespace on the files in the class directory. |
179
|
|
|
*/ |
180
|
1 |
|
protected function setClassNamespace() |
181
|
|
|
{ |
182
|
1 |
|
$files = Finder::create(); |
183
|
|
|
|
184
|
1 |
|
foreach ($this->addon->config('addon.directories') as $path) { |
185
|
1 |
|
$files->in($this->addon->path($path)); |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
$files->name('*.php'); |
189
|
|
|
|
190
|
|
|
$search = [ |
191
|
1 |
|
$this->currentNamespace.'\\', |
192
|
1 |
|
'namespace '.$this->currentNamespace.';', |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
$replace = [ |
196
|
1 |
|
$this->newNamespace.'\\', |
197
|
1 |
|
'namespace '.$this->newNamespace.';', |
198
|
|
|
]; |
199
|
|
|
|
200
|
1 |
|
foreach ($files as $file) { |
201
|
1 |
|
$this->replaceIn($file, $search, $replace); |
202
|
|
|
} |
203
|
1 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Set the namespace in the appropriate configuration files. |
207
|
|
|
*/ |
208
|
1 |
|
protected function setConfigNamespaces() |
209
|
|
|
{ |
210
|
1 |
|
$configPath = $this->addon->path($this->addon->config('paths.config', 'config')); |
211
|
|
|
|
212
|
1 |
|
if ($this->filesystem->isDirectory($configPath)) { |
213
|
1 |
|
$files = Finder::create() |
214
|
1 |
|
->in($configPath) |
215
|
1 |
|
->name('*.php'); |
216
|
|
|
|
217
|
1 |
|
foreach ($files as $file) { |
218
|
|
|
$this->replaceConfigNamespaces($file->getRealPath()); |
219
|
|
|
} |
220
|
|
|
} |
221
|
1 |
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Replace the namespace in PHP configuration file. |
225
|
|
|
* |
226
|
|
|
* @param string $path |
227
|
|
|
*/ |
228
|
|
|
protected function replaceConfigNamespaces($path) |
229
|
|
|
{ |
230
|
|
|
$search = [ |
231
|
|
|
"'{$this->currentNamespace}\\", |
232
|
|
|
"\"{$this->currentNamespace}\\", |
233
|
|
|
"\\{$this->currentNamespace}\\", |
234
|
|
|
]; |
235
|
|
|
|
236
|
|
|
$replace = [ |
237
|
|
|
"'{$this->newNamespace}\\", |
238
|
|
|
"\"{$this->newNamespace}\\", |
239
|
|
|
"\\{$this->newNamespace}\\", |
240
|
|
|
]; |
241
|
|
|
|
242
|
|
|
$this->replaceIn($path, $search, $replace); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Replace the given string in the given file. |
247
|
|
|
* |
248
|
|
|
* @param string $path |
249
|
|
|
* @param string | array $search |
250
|
|
|
* @param string | array $replace |
251
|
|
|
*/ |
252
|
1 |
|
protected function replaceIn($path, $search, $replace) |
253
|
|
|
{ |
254
|
1 |
|
if ($this->output->isVerbose()) { |
255
|
|
|
$this->line("{$path} ..."); |
256
|
|
|
} |
257
|
|
|
|
258
|
1 |
|
$this->filesystem->put($path, str_replace($search, $replace, $this->filesystem->get($path))); |
259
|
1 |
|
} |
260
|
|
|
} |
261
|
|
|
|