Browse Source

关键字过滤,自动生成配置文件

lizhen_gitee 3 years ago
parent
commit
fa6590f26e
1 changed files with 31 additions and 5 deletions
  1. 31 5
      application/admin/model/Keyworldfilter.php

+ 31 - 5
application/admin/model/Keyworldfilter.php

@@ -29,11 +29,37 @@ class Keyworldfilter extends Model
     ];
     
 
-    
-
-
-
-
+    protected static function init()
+    {
+        self::afterUpdate(function ($row) {
+            self::refreshFile();
+        });
+
+        self::afterInsert(function ($row){
+            self::refreshFile();
+        });
+
+        self::afterDelete(function ($row){
+            self::refreshFile();
+        });
+    }
+
+    //刷新文件缓存
+    public static function refreshFile()
+    {
+        $config = [];
+        $configList = self::all();
+        foreach ($configList as $k => $v) {
+            $value = $v->toArray();
+            $config[] = $value['keyworld'];
+        }
+
+        file_put_contents(
+            CONF_PATH . 'extra' . DS . 'keyworld.php',
+            '<?php' . "\n\nreturn " . var_export_short($config) . ";\n"
+        );
+        return true;
+    }