From aee7fa332a2881c0ee158c642fe883cff9c02cfc Mon Sep 17 00:00:00 2001 From: sunxyw Date: Sat, 31 Dec 2022 20:12:20 +0800 Subject: [PATCH 1/3] refactor config config --- captainhook.json | 2 +- config/config.php | 28 ++++++++++++++++++++ src/ZM/Config/ZMConfig.php | 53 +++++++++++++++++++++++++------------- 3 files changed, 64 insertions(+), 19 deletions(-) create mode 100644 config/config.php diff --git a/captainhook.json b/captainhook.json index 5d26d0f9..d352ee62 100644 --- a/captainhook.json +++ b/captainhook.json @@ -14,7 +14,7 @@ "enabled": true, "actions": [ { - "action": "composer cs-fix -- {$STAGED_FILES|of-type:php} --dry-run", + "action": "composer cs-fix -- --config=.php-cs-fixer.php --dry-run --diff {$STAGED_FILES|of-type:php}", "conditions": [ { "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\OfType", diff --git a/config/config.php b/config/config.php new file mode 100644 index 00000000..03764231 --- /dev/null +++ b/config/config.php @@ -0,0 +1,28 @@ + [ + \OneBot\Config\Repository::class, // 配置仓库,须实现 \OneBot\Config\RepositoryInterface 接口 + [], // 传入的参数,依序传入构造函数 + ], + 'loader' => [ + \OneBot\Config\Loader\DelegateLoader::class, // 配置加载器,须实现 \OneBot\Config\LoaderInterface 接口 + [], // 传入的参数,依序传入构造函数 + ], + 'source' => [ + 'extensions' => ['php', 'yaml', 'yml', 'json', 'toml'], // 配置文件扩展名 + 'paths' => [ + SOURCE_ROOT_DIR . '/config', // 配置文件所在目录 + // 可以添加多个配置文件目录 + ], + ], +]; diff --git a/src/ZM/Config/ZMConfig.php b/src/ZM/Config/ZMConfig.php index 7efb409a..e3cd3fe4 100644 --- a/src/ZM/Config/ZMConfig.php +++ b/src/ZM/Config/ZMConfig.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace ZM\Config; use OneBot\Config\Config; +use OneBot\Config\Loader\LoaderInterface; use OneBot\Util\Singleton; use ZM\Exception\ConfigException; use ZM\Framework; @@ -13,21 +14,11 @@ class ZMConfig { use Singleton; - /** - * @var array 支持的文件扩展名 - */ - public const ALLOWED_FILE_EXTENSIONS = ['php', 'yaml', 'yml', 'json', 'toml']; - /** * @var array 配置文件加载顺序,后覆盖前 */ public const LOAD_ORDER = ['default', 'environment', 'patch']; - /** - * @var string 默认配置文件路径 - */ - public const DEFAULT_CONFIG_PATH = SOURCE_ROOT_DIR . '/config'; - /** * @var string[] 环境别名 */ @@ -42,6 +33,11 @@ class ZMConfig */ private array $loaded_files = []; + /** + * @var array 配置文件扩展名 + */ + private array $file_extensions = []; + /** * @var array 配置文件路径 */ @@ -62,6 +58,9 @@ class ZMConfig */ private ?ConfigTracer $tracer = null; + /** @var LoaderInterface 配置加载器 */ + private LoaderInterface $loader; + /** * 构造配置实例 * @@ -70,16 +69,29 @@ class ZMConfig * * @throws ConfigException 配置文件加载出错 */ - public function __construct(array $config_paths = [], string $environment = 'uninitiated') + public function __construct(string $environment = 'uninitiated') { - $this->config_paths = $config_paths ?: [self::DEFAULT_CONFIG_PATH]; + $conf = $this->loadInitConfig(); + $this->file_extensions = $conf['source']['extensions']; + $this->config_paths = $conf['source']['paths']; + $this->environment = self::$environment_alias[$environment] ?? $environment; - $this->holder = new Config([]); + + // 初始化配置容器 + $this->holder = new Config( + new ($conf['repository'][0])(...$conf['repository'][1]), + ); + + // 初始化配置加载器 + $this->loader = new ($conf['loader'][0])(...$conf['loader'][1]); + + // 调试模式下启用配置跟踪器 if (Framework::getInstance()->getArgv()['debug'] ?? false) { $this->tracer = new ConfigTracer(); } else { $this->tracer = null; } + if ($environment !== 'uninitiated') { $this->loadFiles(); } @@ -104,7 +116,7 @@ class ZMConfig foreach ($files as $file) { [, $ext, $load_type] = $this->getFileMeta($file); // 略过不支持的文件 - if (!in_array($ext, self::ALLOWED_FILE_EXTENSIONS, true)) { + if (!in_array($ext, $this->file_extensions, true)) { continue; } @@ -347,12 +359,14 @@ class ZMConfig // 判断文件格式是否支持 [$group, $ext, $load_type, $env] = $this->getFileMeta($path); - if (!in_array($ext, self::ALLOWED_FILE_EXTENSIONS, true)) { + if (!in_array($ext, $this->file_extensions, true)) { throw ConfigException::unsupportedFileType($path); } // 读取并解析配置 $content = file_get_contents($path); + // TODO: 使用 Loader 替代 +// $config = $this->loader->load($path); $config = []; switch ($ext) { case 'php': @@ -396,8 +410,11 @@ class ZMConfig $this->merge($group, $config); logger()->debug("已载入配置文件:{$path}"); - if ($this->tracer !== null) { - $this->tracer->addTracesOf($group, $config, $path); - } + $this->tracer?->addTracesOf($group, $config, $path); + } + + private function loadInitConfig(): array + { + return require SOURCE_ROOT_DIR . '/config/config.php'; } } From a62e950870aeb84dfaf19d7cdad5bc7ca8a1c740 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Sat, 31 Dec 2022 20:12:45 +0800 Subject: [PATCH 2/3] add more path to cs-fix --- .gitignore | 3 +++ .php-cs-fixer.php | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a712e595..b83bf6a9 100644 --- a/.gitignore +++ b/.gitignore @@ -82,3 +82,6 @@ package-lock.json /.tool-version .DS_Store + +### PHP CS Fixer ### +.php-cs-fixer.cache diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 7f9f3f20..b336ed07 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -71,5 +71,6 @@ return (new PhpCsFixer\Config()) PhpCsFixer\Finder::create() ->in(__DIR__ . '/src') ->in(__DIR__ . '/tests') - ) - ->setUsingCache(false); + ->in(__DIR__ . '/config') + ->in(__DIR__ . '/bin') + ); From 3f26648a3cda4c32309889e23fa70a946e502abb Mon Sep 17 00:00:00 2001 From: sunxyw Date: Sat, 31 Dec 2022 20:22:55 +0800 Subject: [PATCH 3/3] fix ZMRequestTest wrong assertion --- src/ZM/Config/ZMConfig.php | 12 +++++++----- tests/ZM/Config/ZMConfigTest.php | 6 +++--- tests/ZM/Utils/ZMRequestTest.php | 6 ++++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/ZM/Config/ZMConfig.php b/src/ZM/Config/ZMConfig.php index e3cd3fe4..d9cdf390 100644 --- a/src/ZM/Config/ZMConfig.php +++ b/src/ZM/Config/ZMConfig.php @@ -58,20 +58,22 @@ class ZMConfig */ private ?ConfigTracer $tracer = null; - /** @var LoaderInterface 配置加载器 */ + /** + * @var LoaderInterface 配置加载器 + * @phpstan-ignore-next-line We will use this property in the future. + */ private LoaderInterface $loader; /** * 构造配置实例 * - * @param array $config_paths 配置文件路径 - * @param string $environment 环境 + * @param string $environment 环境 * * @throws ConfigException 配置文件加载出错 */ - public function __construct(string $environment = 'uninitiated') + public function __construct(string $environment = 'uninitiated', array $init_config = null) { - $conf = $this->loadInitConfig(); + $conf = $init_config ?: $this->loadInitConfig(); $this->file_extensions = $conf['source']['extensions']; $this->config_paths = $conf['source']['paths']; diff --git a/tests/ZM/Config/ZMConfigTest.php b/tests/ZM/Config/ZMConfigTest.php index fb2bde5b..fe32a653 100644 --- a/tests/ZM/Config/ZMConfigTest.php +++ b/tests/ZM/Config/ZMConfigTest.php @@ -56,9 +56,9 @@ class ZMConfigTest extends TestCase ]); try { - $config = new ZMConfig([ - $this->vfs->url(), - ], 'development'); + $init_conf = require SOURCE_ROOT_DIR . '/config/config.php'; + $init_conf['source']['paths'] = [$this->vfs->url()]; + $config = new ZMConfig('development', $init_conf); } catch (ConfigException $e) { $this->fail($e->getMessage()); } diff --git a/tests/ZM/Utils/ZMRequestTest.php b/tests/ZM/Utils/ZMRequestTest.php index e27658b6..da23004f 100644 --- a/tests/ZM/Utils/ZMRequestTest.php +++ b/tests/ZM/Utils/ZMRequestTest.php @@ -24,7 +24,9 @@ class ZMRequestTest extends TestCase public function testGet() { - $r = ZMRequest::get('http://ip.zhamao.xin'); - $this->assertStringContainsString('114', $r); + $r = ZMRequest::get('http://httpbin.org/get', [ + 'X-Test' => '123', + ]); + $this->assertStringContainsString('123', $r); } }