1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acacha\Llum\Traits; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class LaravelConfigFile. |
10
|
|
|
* @property string $laravel_config_file |
11
|
|
|
* @property string $laravel_services_file |
12
|
|
|
* @property OutputInterface $output |
13
|
|
|
*/ |
14
|
|
|
trait LaravelConfigFile |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Avoids using bash using stubs instead to modify config/app.php file. |
18
|
|
|
* |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
protected $noBash = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param InputInterface $input |
25
|
|
|
* @param OutputInterface $output |
26
|
|
|
*/ |
27
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
28
|
|
|
{ |
29
|
|
|
parent::initialize($input, $output); |
30
|
|
|
if ($input->hasOption('no-bash')) { |
31
|
|
|
$this->noBash = $input->getOption('no-bash'); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Check is --no-bash option is active. |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
private function isNoBashActive() |
41
|
|
|
{ |
42
|
|
|
return $this->noBash; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add Laravel IDE Helper provider to config/app.php file. |
47
|
|
|
* |
48
|
|
|
* @return int|null |
49
|
|
|
*/ |
50
|
|
|
protected function addLaravelIdeHelperProvider() |
51
|
|
|
{ |
52
|
|
|
return $this->addProvider('Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Add provider to config/app.php file. |
57
|
|
|
* |
58
|
|
|
* @param $provider |
59
|
|
|
* |
60
|
|
|
* @return int|null |
61
|
|
|
*/ |
62
|
|
|
private function addProvider($provider) |
63
|
|
|
{ |
64
|
|
|
return $this->addTextIntoMountPoint('#llum_providers', $provider); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Add service from file to config/services.php file. |
69
|
|
|
* |
70
|
|
|
* @param $file |
71
|
|
|
* |
72
|
|
|
* @return int|null |
73
|
|
|
*/ |
74
|
|
|
private function addService($file) |
75
|
|
|
{ |
76
|
|
|
$result = $this->addFileIntoMountPoint('#llum_services', $file); |
77
|
|
|
if ($result == 0) { |
|
|
|
|
78
|
|
|
$this->output->writeln('<info>File '.$this->laravel_services_file.' updated.</info>'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Add alias to config/app.php file. |
86
|
|
|
* |
87
|
|
|
* @param string $alias |
88
|
|
|
* |
89
|
|
|
* @return int|null |
90
|
|
|
*/ |
91
|
|
|
private function addAlias($alias) |
92
|
|
|
{ |
93
|
|
|
return $this->addTextIntoMountPoint('#llum_aliases', $alias); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Insert text into file using mountpoint. Mountpoint is maintained at file. |
98
|
|
|
* |
99
|
|
|
* @param string $mountpoint |
100
|
|
|
* @param $textToAdd |
101
|
|
|
* |
102
|
|
|
* @return int|null |
103
|
|
|
*/ |
104
|
|
|
private function addTextIntoMountPoint($mountpoint, $textToAdd) |
105
|
|
|
{ |
106
|
|
|
passthru( |
107
|
|
|
'sed -i \'s/.*'.$mountpoint.'.*/ \ \ \ \ \ \ \ '.$this->scapeSingleQuotes(preg_quote($textToAdd)).',\n \ \ \ \ \ \ \ '.$mountpoint.'/\' '.$this->laravel_config_file, $error); |
108
|
|
|
|
109
|
|
|
return $error; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Insert file into file using mountpoint. |
114
|
|
|
* |
115
|
|
|
* @param $mountpoint |
116
|
|
|
* @param $fileToInsert |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
private function addFileIntoMountPoint($mountpoint, $fileToInsert) |
120
|
|
|
{ |
121
|
|
|
passthru( |
122
|
|
|
'sed -i \'/'.$mountpoint.'/r'.$fileToInsert.'\' '.$this->laravel_services_file, $error); |
123
|
|
|
|
124
|
|
|
return $error; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* scape single quotes for sed using \x27. |
129
|
|
|
* |
130
|
|
|
* @param string $str |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
private function scapeSingleQuotes($str) |
135
|
|
|
{ |
136
|
|
|
return str_replace("'", '\\x27', $str); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Installs provider in laravel config/app.php file. |
141
|
|
|
* |
142
|
|
|
* @param $provider |
143
|
|
|
*/ |
144
|
|
|
protected function provider($provider) |
145
|
|
|
{ |
146
|
|
|
if ($this->installConfigFile() == -1) { |
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
$this->addProvider($provider); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add service/s from file to Laravel config/services.php. |
154
|
|
|
* |
155
|
|
|
* @param $file |
156
|
|
|
*/ |
157
|
|
|
protected function service($file) |
158
|
|
|
{ |
159
|
|
|
if ($this->installConfigFile() == -1) { |
160
|
|
|
return; |
161
|
|
|
} |
162
|
|
|
$this->addService($file); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Setup laravel config file adding providers and aliases. |
167
|
|
|
* |
168
|
|
|
* @param $providers |
169
|
|
|
* @param $aliases |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
|
private function setupLaravelConfigFile($providers, $aliases) |
173
|
|
|
{ |
174
|
|
|
if ($this->installConfigFile() == -1) { |
175
|
|
|
return -1; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->addProviders($providers); |
179
|
|
|
|
180
|
|
|
$this->addAliases($aliases); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Installs alias/facade in laravel config/app.php file. |
185
|
|
|
* |
186
|
|
|
* @param $aliasName |
187
|
|
|
* @param $aliasClass |
188
|
|
|
*/ |
189
|
|
|
protected function alias($aliasName, $aliasClass) |
190
|
|
|
{ |
191
|
|
|
if ($this->installConfigFile() == -1) { |
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
$this->addAlias("'".$aliasName."' => ".$aliasClass); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Install /config/app.php file using bash script. |
199
|
|
|
*/ |
200
|
|
|
protected function installConfigFileWithBash() |
201
|
|
|
{ |
202
|
|
|
passthru(__DIR__.'/../bash_scripts/iluminar.sh '.$this->laravel_config_file.' ' |
203
|
|
|
.$this->laravel_services_file); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Install /stubs/app.php into /config/app.php. |
208
|
|
|
*/ |
209
|
|
|
protected function installConfigFileWithStubs() |
210
|
|
|
{ |
211
|
|
|
copy(__DIR__.'/stubs/app.php', $this->laravel_config_file); |
212
|
|
|
copy(__DIR__.'/stubs/services.php', $this->laravel_services_file); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Check if Laravel config file exists. |
217
|
|
|
* |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
|
|
protected function checkIfLaravelConfigFileExists() |
221
|
|
|
{ |
222
|
|
|
return file_exists($this->laravel_config_file); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Install llum custom config/app.php file. |
227
|
|
|
* |
228
|
|
|
* @return int |
229
|
|
|
*/ |
230
|
|
|
protected function installConfigFile() |
231
|
|
|
{ |
232
|
|
|
if ($this->testLaravelConfigFileExists() == -1) { |
233
|
|
|
return; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$this->showWarningIfLaravelConfigAlreadySupportsLlum(); |
237
|
|
|
|
238
|
|
|
if ($this->isNoBashActive()) { |
239
|
|
|
$this->installConfigFileWithStubs(); |
240
|
|
|
$this->output->writeln('<info>File '.$this->laravel_config_file.' overwrited correctly with and stub.</info>'); |
241
|
|
|
} else { |
242
|
|
|
$this->installConfigFileWithBash(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return 0; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Test Laravel config file exists. |
250
|
|
|
* |
251
|
|
|
* @return int |
252
|
|
|
*/ |
253
|
|
|
private function testLaravelConfigFileExists() |
254
|
|
|
{ |
255
|
|
|
if (! $this->checkIfLaravelConfigFileExists()) { |
256
|
|
|
$this->output->writeln('<error>File '.$this->laravel_config_file.' doesn\'t exists'); |
257
|
|
|
|
258
|
|
|
return -1; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Show warning if Laravel config file already supports llum. |
264
|
|
|
* |
265
|
|
|
* @return int |
266
|
|
|
*/ |
267
|
|
|
private function showWarningIfLaravelConfigAlreadySupportsLlum() |
268
|
|
|
{ |
269
|
|
|
if ($this->configAppFileAlreadyInstalled()) { |
270
|
|
|
$this->output->writeln('<info>File '.$this->laravel_config_file.' already supports llum.</info>'); |
271
|
|
|
|
272
|
|
|
return 0; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Add providers to Laravel config file. |
278
|
|
|
* |
279
|
|
|
* @param $providers |
280
|
|
|
*/ |
281
|
|
|
protected function addProviders($providers) |
282
|
|
|
{ |
283
|
|
|
foreach ($providers as $provider) { |
284
|
|
|
$this->output->writeln('<info>Adding '.$provider.' to Laravel config app.php file</info>'); |
285
|
|
|
$this->addProvider($provider); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Add aliases to Laravel config file. |
291
|
|
|
* |
292
|
|
|
* @param $aliases |
293
|
|
|
*/ |
294
|
|
|
protected function addAliases($aliases) |
295
|
|
|
{ |
296
|
|
|
if ($aliases == null) { |
297
|
|
|
return; |
298
|
|
|
} |
299
|
|
|
foreach ($aliases as $alias => $aliasClass) { |
300
|
|
|
$this->output->writeln('<info>Adding '.$aliasClass.' to Laravel config app.php file</info>'); |
301
|
|
|
$this->addAlias("'$alias' => ".$aliasClass); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Check if config/app.php stub file is already installed. |
307
|
|
|
* |
308
|
|
|
* @return bool |
309
|
|
|
*/ |
310
|
|
|
protected function configAppFileAlreadyInstalled() |
311
|
|
|
{ |
312
|
|
|
if (strpos(file_get_contents($this->laravel_config_file), '#llum_providers') !== false) { |
313
|
|
|
return true; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return false; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|