1 | <?php |
||
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() |
|
113 | |||
114 | /** |
||
115 | * Set the namespace in addon.php file. |
||
116 | */ |
||
117 | 1 | protected function setAddonConfigNamespaces() |
|
139 | |||
140 | /** |
||
141 | * Set the namespace in addon.json file. |
||
142 | */ |
||
143 | 1 | protected function setAddonJsonNamespaces() |
|
164 | |||
165 | /** |
||
166 | * Set the PSR-4 namespace in the Composer file. |
||
167 | */ |
||
168 | 1 | protected function setComposerNamespace() |
|
176 | |||
177 | /** |
||
178 | * Set the namespace on the files in the class directory. |
||
179 | */ |
||
180 | 1 | protected function setClassNamespace() |
|
204 | |||
205 | /** |
||
206 | * Set the namespace in the appropriate configuration files. |
||
207 | */ |
||
208 | 1 | protected function setConfigNamespaces() |
|
222 | |||
223 | /** |
||
224 | * Replace the namespace in PHP configuration file. |
||
225 | * |
||
226 | * @param string $path |
||
227 | */ |
||
228 | protected function replaceConfigNamespaces($path) |
||
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) |
|
260 | } |
||
261 |