| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 | 
							- <?php
 
- /**
 
-  * Created by PhpStorm.
 
-  * User:  NODELOG
 
-  * Date: 16/4/27
 
-  * Time: 下午4:07
 
-  */
 
- namespace common\modules\config\components;
 
- use Yii;
 
- use common\modules\config\models\Config as ConfigModel;
 
- use yii\base\Component;
 
- use yii\caching\TagDependency;
 
- use yii\helpers\VarDumper;
 
- class Config extends Component
 
- {
 
-     public $cacheKey = 'allSystemConfigs';
 
-     public $cacheTag = 'systemConfig';
 
-     public $localConfigFile = '@common/config/main-local.php';
 
-     public $envFile = '@root/.env';
 
-     public function init()
 
-     {
 
-         parent::init();
 
-         $this->localConfigFile = \Yii::getAlias($this->localConfigFile);
 
-     }
 
-     public function get($name, $default = '')
 
-     {
 
-         $configs = \Yii::$app->cache->get($this->cacheKey);
 
-         if ($configs === false) {
 
-             $configs = ConfigModel::find()->indexBy('name')->all();
 
-             \Yii::$app->cache->set($this->cacheKey, $configs, 60 * 60, new TagDependency(['tags' => $this->cacheTag]));
 
-         }
 
-         if (isset($configs[$name])) {
 
-             $config = $configs[$name];
 
-             return self::_parse($config->type, $config->value);
 
-         } else {
 
-             return env($name, $default);
 
-         }
 
-     }
 
-     public function set($name, $value)
 
-     {
 
-         if (ConfigModel::findOne(['name' => $name]) != null) {
 
-             $result = ConfigModel::updateAll(['value' => $value], ['name' => $name]);
 
-             if ($result === false) {
 
-                 return false;
 
-             }
 
-             TagDependency::invalidate(\Yii::$app->cache, $this->cacheTag);
 
-         } else {
 
-             $this->setEnv($name, $value);
 
-         }
 
-         return true;
 
-     }
 
-     public function has($name)
 
-     {
 
-         return !is_null(ConfigModel::find()->where(['name' => $name])->one()) || getenv($name) !== false;
 
-      }
 
-     /**
 
-      * 解析数组类型配置
 
-      * @param $type
 
-      * @param $value
 
-      * @return array
 
-      */
 
-     private static function _parse($type, $value)
 
-     {
 
-         switch ($type) {
 
-             case ConfigModel::TYPE_ARRAY:
 
-                 $return = [];
 
-                 $value = trim($value, "\r\n");
 
-                 foreach (explode("\r\n", $value) as $val) {
 
-                     if (strpos($val, '=>') !== false) {
 
-                         list($k, $v) = explode('=>', $val);
 
-                         $return[$k] = $v;
 
-                     } else {
 
-                         $return[] = $val;
 
-                     }
 
-                 }
 
-                 $value = $return;
 
-                 break;
 
-         }
 
-         return $value;
 
-     }
 
-     public function getConfigFromLocal()
 
-     {
 
-         $config = require ($this->localConfigFile);
 
-         if (! is_array($config))
 
-             return [];
 
-         return $config;
 
-     }
 
-     /**
 
-      * Sets configuration into the file
 
-      *
 
-      * @param array $config
 
-      */
 
-     public function setConfigToLocal($config = [])
 
-     {
 
-         $content = "<" . "?php return ";
 
-         $content .= VarDumper::export($config);
 
-         $content .= ";";
 
-         file_put_contents($this->localConfigFile, $content);
 
-     }
 
-     public function setEnv($name, $value)
 
-     {
 
-         $file = Yii::getAlias($this->envFile);
 
-         $content = preg_replace("/({$name}\s*=)\s*(.*)/", "\${1}$value", file_get_contents($file));// \${1}修复后边跟数字的bug
 
-         file_put_contents($file, $content);
 
-     }
 
- }
 
 
  |