Skip to content

Commit 00c81ce

Browse files
committed
优化写配置文件
1 parent 276dbed commit 00c81ce

File tree

5 files changed

+26
-33
lines changed

5 files changed

+26
-33
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "addons package for fastadmin",
44
"homepage": "https://github.com/karsonzhang/fastadmin-addons",
55
"license": "Apache-2.0",
6-
"version": "1.2.10",
6+
"version": "1.2.11",
77
"authors": [
88
{
99
"name": "Karson",

src/Addons.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ abstract class Addons
2020
protected $error;
2121
// 插件目录
2222
public $addons_path = '';
23+
public $addonPath = '';
24+
2325
// 插件标识
2426
protected $addonName = '';
2527
// 插件配置作用域
@@ -39,10 +41,11 @@ public function __construct($name = null)
3941
$this->addonName = $name;
4042

4143
// 获取当前插件目录
42-
$this->addons_path = ADDON_PATH . $name . DS;
44+
$this->addonPath = ADDON_PATH . $name . DS;
45+
$this->addons_path = $this->addonPath;
4346

4447
// 初始化视图模型
45-
$config = ['view_path' => $this->addons_path];
48+
$config = ['view_path' => $this->addonPath];
4649
$config = array_merge(Config::get('template'), $config);
4750
$this->view = new View($config, Config::get('view_replace_str'));
4851

@@ -69,9 +72,9 @@ final public function getInfo($name = '', $force = false)
6972
}
7073
}
7174
$info = [];
72-
$info_file = $this->addons_path . 'info.ini';
73-
if (is_file($info_file)) {
74-
$info = Config::parse($info_file, '', $name, $this->infoRange);
75+
$infoFile = $this->addonPath . 'info.ini';
76+
if (is_file($infoFile)) {
77+
$info = Config::parse($infoFile, '', $name, $this->infoRange);
7578
$info['url'] = addon_url($name);
7679
}
7780
Config::set($name, $info, $this->infoRange);
@@ -96,13 +99,15 @@ final public function getConfig($name = '', $force = false)
9699
}
97100
}
98101
$config = [];
99-
$config_file = $this->addons_path . 'config.php';
100-
if (is_file($config_file)) {
101-
$temp_arr = include $config_file;
102-
foreach ($temp_arr as $key => $value) {
103-
$config[$value['name']] = $value['value'];
102+
$configFile = $this->addonPath . 'config.php';
103+
if (is_file($configFile)) {
104+
$configArr = include $configFile;
105+
if (is_array($configArr)) {
106+
foreach ($configArr as $key => $value) {
107+
$config[$value['name']] = $value['value'];
108+
}
109+
unset($configArr);
104110
}
105-
unset($temp_arr);
106111
}
107112
Config::set($name, $config, $this->configRange);
108113

@@ -154,9 +159,9 @@ final public function getFullConfig($name = '')
154159
if (empty($name)) {
155160
$name = $this->getName();
156161
}
157-
$config_file = $this->addons_path . 'config.php';
158-
if (is_file($config_file)) {
159-
$fullConfigArr = include $config_file;
162+
$configFile = $this->addonPath . 'config.php';
163+
if (is_file($configFile)) {
164+
$fullConfigArr = include $configFile;
160165
}
161166
return $fullConfigArr;
162167
}

src/addons/Route.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function execute($addon = null, $controller = null, $action = null)
4040
}
4141
$dispatch = $request->dispatch();
4242
if (isset($dispatch['var']) && $dispatch['var']) {
43-
//$request->route($dispatch['var']);
43+
$request->route(array_diff_key($dispatch['var'], array_flip(['addon', 'controller', 'action'])));
4444
}
4545

4646
// 设置当前请求的控制器、操作

src/addons/Service.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,7 @@ public static function refresh()
387387
throw new Exception(__("Unable to open file '%s' for writing", "addons.php"));
388388
}
389389

390-
if ($handle = fopen($file, 'w')) {
391-
fwrite($handle, "<?php\n\n" . "return " . VarExporter::export($config) . ";\n");
392-
fclose($handle);
393-
} else {
394-
throw new Exception(__("Unable to open file '%s' for writing", "addons.php"));
395-
}
390+
file_put_contents($file, "<?php\n\n" . "return " . VarExporter::export($config) . ";\n", LOCK_EX);
396391
return true;
397392
}
398393

src/common.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,7 @@ function set_addon_info($name, $array)
456456
$res[] = "$key = " . (is_numeric($val) ? $val : $val);
457457
}
458458
}
459-
if ($handle = fopen($file, 'w')) {
460-
fwrite($handle, implode("\n", $res) . "\n");
461-
fclose($handle);
459+
if (file_put_contents($file, implode("\n", $res) . "\n", LOCK_EX)) {
462460
//清空当前配置缓存
463461
Config::set($name, null, 'addoninfo');
464462
} else {
@@ -504,14 +502,9 @@ function set_addon_config($name, $config, $writefile = true)
504502
function set_addon_fullconfig($name, $array)
505503
{
506504
$file = ADDON_PATH . $name . DS . 'config.php';
507-
if (!is_really_writable($file)) {
508-
throw new Exception("文件没有写入权限");
509-
}
510-
if ($handle = fopen($file, 'w')) {
511-
fwrite($handle, "<?php\n\n" . "return " . VarExporter::export($array) . ";\n");
512-
fclose($handle);
513-
} else {
514-
throw new Exception("文件没有写入权限");
505+
$ret = file_put_contents($file, "<?php\n\n" . "return " . VarExporter::export($array) . ";\n", LOCK_EX);
506+
if (!$ret) {
507+
throw new Exception("配置写入失败");
515508
}
516509
return true;
517510
}

0 commit comments

Comments
 (0)