|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace luya\dev; |
|
4
|
|
|
|
|
5
|
|
|
use luya\console\Command; |
|
6
|
|
|
use luya\helpers\FileHelper; |
|
7
|
|
|
use yii\helpers\Json; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* BaseDevCommand Controller. |
|
11
|
|
|
* |
|
12
|
|
|
* Provides the IO for the configuration storage. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Basil Suter <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class BaseDevCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string The location of the devconfig json where data is stored. |
|
20
|
|
|
*/ |
|
21
|
|
|
public $configFile = '@appFolder/devconfig.json'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Display config data and location. |
|
25
|
|
|
* |
|
26
|
|
|
* @return boolean|void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function actionConfigInfo() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->outputInfo("dev config file: " . Yii::getAlias($this->configFile)); |
|
31
|
|
|
|
|
32
|
|
|
$config = $this->readConfig(); |
|
33
|
|
|
|
|
34
|
|
|
if (!$config) { |
|
35
|
|
|
return $this->outputError("Unable to open config file."); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
foreach ($config as $key => $value) { |
|
|
|
|
|
|
39
|
|
|
$this->output("{$key} => {$value}"); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Read entire config and return as array. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array|boolean |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function readConfig() |
|
49
|
|
|
{ |
|
50
|
|
|
$data = FileHelper::getFileContent($this->configFile); |
|
51
|
|
|
|
|
52
|
|
|
if ($data) { |
|
|
|
|
|
|
53
|
|
|
return Json::decode($data); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get a specific value for a given key. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $key |
|
63
|
|
|
* @return boolean |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function getConfig($key) |
|
66
|
|
|
{ |
|
67
|
|
|
$config = $this->readConfig(); |
|
68
|
|
|
|
|
69
|
|
|
return isset($config[$key]) ? $config[$key] : false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Save a value in the config for a given key. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $key |
|
76
|
|
|
* @param mixed $value |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function saveConfig($key, $value) |
|
80
|
|
|
{ |
|
81
|
|
|
$content = $this->readConfig(); |
|
82
|
|
|
|
|
83
|
|
|
if (!$content) { |
|
84
|
|
|
$content = []; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$content[$key] = $value; |
|
88
|
|
|
|
|
89
|
|
|
$save = FileHelper::writeFile($this->configFile, Json::encode($content)); |
|
90
|
|
|
|
|
91
|
|
|
if (!$save) { |
|
92
|
|
|
return $this->outputError("Unable to find config file " . $this->configFile. ". Please create and provide Permissions."); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $value; |
|
96
|
|
|
} |
|
97
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.