Config.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/4/27
  6. * Time: 下午4:07
  7. */
  8. namespace common\modules\config\components;
  9. use Yii;
  10. use common\modules\config\models\Config as ConfigModel;
  11. use yii\base\Component;
  12. use yii\caching\TagDependency;
  13. use yii\helpers\VarDumper;
  14. class Config extends Component
  15. {
  16. public $cacheKey = 'allSystemConfigs';
  17. public $cacheTag = 'systemConfig';
  18. public $localConfigFile = '@common/config/main-local.php';
  19. public $envFile = '@root/.env';
  20. public function init()
  21. {
  22. parent::init();
  23. $this->localConfigFile = \Yii::getAlias($this->localConfigFile);
  24. }
  25. public function get($name, $default = '')
  26. {
  27. $configs = \Yii::$app->cache->get($this->cacheKey);
  28. if ($configs === false) {
  29. $configs = ConfigModel::find()->indexBy('name')->all();
  30. \Yii::$app->cache->set($this->cacheKey, $configs, 60 * 60, new TagDependency(['tags' => $this->cacheTag]));
  31. }
  32. if (isset($configs[$name])) {
  33. $config = $configs[$name];
  34. return self::_parse($config->type, $config->value);
  35. } else {
  36. return env($name, $default);
  37. }
  38. }
  39. public function set($name, $value)
  40. {
  41. if (ConfigModel::findOne(['name' => $name]) != null) {
  42. $result = ConfigModel::updateAll(['value' => $value], ['name' => $name]);
  43. if ($result === false) {
  44. return false;
  45. }
  46. TagDependency::invalidate(\Yii::$app->cache, $this->cacheTag);
  47. } else {
  48. $this->setEnv($name, $value);
  49. }
  50. return true;
  51. }
  52. public function has($name)
  53. {
  54. return !is_null(ConfigModel::find()->where(['name' => $name])->one()) || getenv($name) !== false;
  55. }
  56. /**
  57. * 解析数组类型配置
  58. * @param $type
  59. * @param $value
  60. * @return array
  61. */
  62. private static function _parse($type, $value)
  63. {
  64. switch ($type) {
  65. case ConfigModel::TYPE_ARRAY:
  66. $return = [];
  67. $value = trim($value, "\r\n");
  68. foreach (explode("\r\n", $value) as $val) {
  69. if (strpos($val, '=>') !== false) {
  70. list($k, $v) = explode('=>', $val);
  71. $return[$k] = $v;
  72. } else {
  73. $return[] = $val;
  74. }
  75. }
  76. $value = $return;
  77. break;
  78. }
  79. return $value;
  80. }
  81. public function getConfigFromLocal()
  82. {
  83. $config = require ($this->localConfigFile);
  84. if (! is_array($config))
  85. return [];
  86. return $config;
  87. }
  88. /**
  89. * Sets configuration into the file
  90. *
  91. * @param array $config
  92. */
  93. public function setConfigToLocal($config = [])
  94. {
  95. $content = "<" . "?php return ";
  96. $content .= VarDumper::export($config);
  97. $content .= ";";
  98. file_put_contents($this->localConfigFile, $content);
  99. }
  100. public function setEnv($name, $value)
  101. {
  102. $file = Yii::getAlias($this->envFile);
  103. $content = preg_replace("/({$name}\s*=)\s*(.*)/", "\${1}$value", file_get_contents($file));// \${1}修复后边跟数字的bug
  104. file_put_contents($file, $content);
  105. }
  106. }