1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CommandUtilTrait. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Bmatovu\MtnMomo\Traits; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Console commands utilities. |
10
|
|
|
*/ |
11
|
|
|
trait CommandUtilTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Write a string as standard output. |
15
|
|
|
* |
16
|
|
|
* @see \Illuminate\Console\Concerns\InteractsWithIO::line |
17
|
|
|
* |
18
|
|
|
* @param string $string |
19
|
|
|
* @param string|null $style |
20
|
|
|
* @param int|string|null $verbosity |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
abstract public function line($string, $style = null, $verbosity = null); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Warn user running command in production. |
28
|
|
|
* |
29
|
|
|
* @param string $warning |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
14 |
|
protected function runInProduction($warning = 'Application In Production!') |
34
|
|
|
{ |
35
|
14 |
|
if ($this->getLaravel()->environment() != 'production') { |
|
|
|
|
36
|
11 |
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
if ($this->option('force')) { |
|
|
|
|
40
|
1 |
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
$this->comment(str_repeat('*', strlen($warning) + 12)); |
|
|
|
|
44
|
2 |
|
$this->comment('* '.$warning.' *'); |
45
|
2 |
|
$this->comment(str_repeat('*', strlen($warning) + 12)); |
46
|
2 |
|
$this->output->writeln(''); |
47
|
|
|
|
48
|
2 |
|
$confirmed = $this->confirm('Do you really wish to proceed?'); |
|
|
|
|
49
|
|
|
|
50
|
2 |
|
if (! $confirmed) { |
51
|
1 |
|
$this->comment('Command Cancelled!'); |
52
|
|
|
|
53
|
1 |
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Print formatted labels. |
61
|
|
|
* |
62
|
|
|
* @param string $title |
63
|
|
|
* @param array|string $body |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
9 |
|
protected function printLabels($title, $body = null) |
68
|
|
|
{ |
69
|
9 |
|
$this->line("<options=bold>{$title}</>"); |
70
|
|
|
|
71
|
9 |
|
if (is_null($body)) { |
72
|
9 |
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
7 |
|
$body = is_array($body) ? $body : [$body]; |
76
|
|
|
|
77
|
7 |
|
foreach ($body as $content) { |
78
|
7 |
|
$this->line("{$content}"); |
79
|
|
|
} |
80
|
|
|
|
81
|
7 |
|
$this->output->writeln(''); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Write | replace setting in .env file. |
86
|
|
|
* |
87
|
|
|
* @param string $name ENV_VALUE, like; `APP_NAME` |
88
|
|
|
* @param string $key Compose setting name, like `app.name` |
89
|
|
|
* @param string $value Setting value |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
12 |
|
protected function updateSetting($name, $key, $value) |
94
|
|
|
{ |
95
|
|
|
// Update in memory. |
96
|
12 |
|
$this->laravel['config']->set([$key => $value]); |
97
|
|
|
|
98
|
12 |
|
if ($this->option('no-write')) { |
99
|
1 |
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Update in file. |
103
|
11 |
|
$env = environment_file_path(); |
104
|
|
|
|
105
|
11 |
|
$name = strtoupper($name); |
106
|
|
|
|
107
|
11 |
|
$pattern = "/^{$name}=[\"']?.*/m"; |
108
|
|
|
|
109
|
11 |
|
if (preg_match($pattern, file_get_contents($env))) { |
110
|
1 |
|
file_put_contents($env, preg_replace($pattern, "{$name}=\"{$value}\"", file_get_contents($env))); |
111
|
|
|
} else { |
112
|
11 |
|
$setting = "\r\n{$name}=\"{$value}\""; |
113
|
11 |
|
file_put_contents($env, file_get_contents($env).$setting); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|