Просмотр исходного кода

feat(phpinfo): 添加PHP信息扩展

runphp 7 месяцев назад
Сommit
161c627e5a

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 3 - 0
README.md

@@ -0,0 +1,3 @@
+# phpinfo
+
+https://sixshop.ddev.site/admin/phpinfo

+ 31 - 0
composer.json

@@ -0,0 +1,31 @@
+{
+  "name": "six-shop/phpinfo",
+  "description": "phpinfo",
+  "type": "sixshop-extension",
+  "keywords": [
+    "sixshop",
+    "thinkphp"
+  ],
+  "require": {
+    "php": ">=8.3",
+    "six-shop/core": ">=0.4 <1.0"
+  },
+  "authors": [
+    {
+      "name": "hui he",
+      "email": "runphp@qq.com"
+    }
+  ],
+  "license": "MIT",
+  "autoload": {
+    "psr-4": {
+      "SixShop\\PHPInfo\\": "src"
+    }
+  },
+  "extra": {
+    "sixshop": {
+      "id": "eav",
+      "class": "SixShop\\PHPInfo\\Extension"
+    }
+  }
+}

+ 5 - 0
config.php

@@ -0,0 +1,5 @@
+<?php
+declare(strict_types=1);
+
+return [
+];

+ 16 - 0
info.php

@@ -0,0 +1,16 @@
+<?php
+declare(strict_types=1);
+
+return [
+    'id' => 'phpinfo',
+    'name' => 'PHPInfo Extension',
+    'category' => 'other', # 扩展的分类 core:核心扩展,other:其他扩展
+    'description' => '这是一个示例扩展,用于展示如何创建和使用扩展。',
+    'version' => '1.0.0',
+    'core_version' => '^1.0',
+    'author' => 'runphp', # 作者
+    'email' => 'runphp@qq.com', # 作者的邮箱
+    'website' => '', # 扩展的地址,可以是扩展的仓库地址,帮助用户寻找扩展,安装扩展等网络地址
+    'image' => '', # 扩展的图片,用于展示扩展的图标,或者是扩展的截图等图片地址
+    'license' => 'MIT', # 扩展的开源协议
+];

+ 10 - 0
route/admin.php

@@ -0,0 +1,10 @@
+<?php
+declare(strict_types=1);
+
+use think\facade\Route;
+
+Route::get('', function () {
+    phpinfo();
+})
+    ->option(['name' => 'system:phpinfo', 'description' => 'PHP信息'])
+    ->middleware(['auth']);

+ 9 - 0
src/Controller/IndexController.php

@@ -0,0 +1,9 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\PHPInfo\Controller;
+
+class IndexController
+{
+
+}

+ 23 - 0
src/Extension.php

@@ -0,0 +1,23 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\PHPInfo;
+
+use SixShop\Core\ExtensionAbstract;
+use SixShop\PHPInfo\Hook\PhpinfoHook;
+
+class Extension extends ExtensionAbstract
+{
+
+    public function getHooks(): array
+    {
+        return [
+            PhpinfoHook::class,
+        ];
+    }
+
+    protected function getBaseDir(): string
+    {
+        return dirname(__DIR__);
+    }
+}

+ 23 - 0
src/Hook/PhpinfoHook.php

@@ -0,0 +1,23 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\PHPInfo\Hook;
+
+use SixShop\Core\Attribute\Hook;
+use SixShop\PHPInfo\Middleware\AppendSqlDebugMiddleware;
+use think\App;
+use think\event\HttpRun;
+
+class PhpinfoHook
+{
+
+    public function __construct(private App $app)
+    {
+
+    }
+    #[Hook(HttpRun::class)]
+    public function appendSqlDebug(): void
+    {
+        $this->app->middleware->add(AppendSqlDebugMiddleware::class);
+    }
+}

+ 35 - 0
src/Middleware/AppendSqlDebugMiddleware.php

@@ -0,0 +1,35 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\PHPInfo\Middleware;
+
+use Closure;
+use think\DbManager;
+use think\Request;
+use think\Response;
+
+
+class AppendSqlDebugMiddleware
+{
+    private array $logList = [];
+
+    public function __construct(private readonly DbManager $dbManager)
+    {
+        $this->dbManager->setLog(function (string $type, string $log) {
+            $this->logList[] = $log;
+        });
+    }
+
+    public function handle(Request $request, Closure $next): Response
+    {
+        $response = $next($request);
+        if ($response instanceof Response && is_array($data = $response->getData())) {
+            $data['sql'] = $this->logList;
+            if (!empty($data['sql'])) {
+                $response->data($data);
+            }
+        }
+
+        return $response;
+    }
+}