1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace msztorc\LaravelEnv\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use msztorc\LaravelEnv\Commands\Traits\CommandValidator; |
8
|
|
|
use msztorc\LaravelEnv\Env; |
9
|
|
|
|
10
|
|
|
class EnvGetCommand extends Command |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
use CommandValidator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The name and signature of the console command. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'env:get {key?} {--key-value} {--json}'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Get variable value from an environment file'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Environment variable key. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $key; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* key-value format arg |
38
|
|
|
* |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
protected $keyValFormat; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* json format argument |
45
|
|
|
* |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
protected $json; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Env object |
52
|
|
|
* |
53
|
|
|
* @var object |
54
|
|
|
*/ |
55
|
|
|
protected $env; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create a new command instance. |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function __construct() |
63
|
|
|
{ |
64
|
|
|
parent::__construct(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Execute the console command. |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function handle() |
73
|
|
|
{ |
74
|
|
|
$this->key = (string)$this->argument('key'); |
75
|
|
|
|
76
|
|
|
if (strlen($this->key)) |
77
|
|
|
$this->isValidKey($this->key); |
78
|
|
|
|
79
|
|
|
$this->json = (bool)$this->option('json'); |
80
|
|
|
$this->keyValFormat = (bool)$this->option('key-value'); |
81
|
|
|
$this->env = new Env(); |
82
|
|
|
|
83
|
|
|
return $this->_printOutput(); |
|
|
|
|
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function _printAllEnvValues() |
88
|
|
|
{ |
89
|
|
|
$this->line(($this->json) ? json_encode($this->env->getVariables()) : $this->env->getEnvContent()); |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function _printKeyValue() |
94
|
|
|
{ |
95
|
|
|
$value = ($this->json) ? json_encode($this->env->getKeyValue($this->key)) : ($this->keyValFormat ? $this->env->getKeyValue($this->key) : $this->env->getValue($this->key)); |
96
|
|
|
|
97
|
|
|
$this->line(($this->json) |
98
|
|
|
? (string)$value |
99
|
|
|
: ($this->keyValFormat ? "{$this->key}={$value[$this->key]}" : (string)$value) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function _printOutput() |
106
|
|
|
{ |
107
|
|
|
if (!strlen($this->key)) { |
108
|
|
|
$this->_printAllEnvValues(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (strlen($this->key) && $this->env->exists($this->key)) { |
112
|
|
|
$this->_printKeyValue(); |
113
|
|
|
} else { |
114
|
|
|
$this->line("There is no variable '{$this->key}'"); |
115
|
|
|
} |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.