[邪修] 应用别名映射的简单解决方法

cola

修改文件:
vendor/workerman/webman-framework/src/App.php

在文件中增加方法:

    /**
     * 应用别名
     *
     * @param string $app 应用名
     * @param string|array|null $bindName 别名
     * @return array|string|null
     */
    public static function alias($app, $bindName = null)
    {
        static $appNames = [];
        if(empty($app) && empty($bindName)) return $appNames;
        if (empty($app) && !empty($bindName) && is_string($bindName)) {
            if(isset($appNames[$bindName])) return $bindName . '_alias';
            foreach ($appNames as $key => $names) {
                if (in_array($bindName, (array)$names, true)) {
                    return $key;
                }
            }
            return null;
        }
        if(!empty($app) && empty($bindName)) {
            return $appNames[$app] ?? $app;
        }
        if(!empty($app) && !empty($bindName)) $appNames[$app] = array_merge($appNames[$app] ?? [], is_string($bindName) ? [$bindName] : $bindName);
        return null;
    }

修改 parseControllerAction 方法:

...
$relativePath = trim(substr($path, strlen($pathPrefix)), '/');
$pathExplode = $relativePath ? explode('/', $relativePath) : [];
// 此处增加
if (count($pathExplode) >= 1) $pathExplode[0] = static::alias('', $pathExplode[0]) ?? $pathExplode[0];

访问效果:

/ => /index/index => app\controller\Index::Index
/testadmin/user/login => /admin/user/login => app\admin\controller\User::login
/admin/user/login => 404

使用方法:

\Webman\App::alias();  // 读取所有
\Webman\App::alias('admin'); // 读取指定应用的所有别名
\Webman\App::alias('admin', string | array[string] ); // 设置应用的别名
\Webman\App::alias('', 'testadmin'); // 查找别名对应的应用名

例:

use Webman\App;

// 设置 admin 应用的别名为 testadmin
App::alias('admin', 'testadmin');
App::alias('admin', ['testadmin']);

注意:
此方法没有实现修改和删除功能, 需要自行实现

165 1 0
1个评论

efnic

作者仅供抛砖引玉,正确做法是,使用继承重写方法。

如:类 \app\process\Http 已继承 Webman\App,无侵入。

<?php

namespace app\process;

use ReflectionException;
use Webman\App;
use Webman\Config;

class Http extends App
{
    /**
     * 应用别名
     *
     * @param string $app 应用名
     * @param array|string|null $bindName 别名
     * @return array|string|null
     */
    public static function alias(string $app, array|string|null $bindName = null): array|string|null
    {
        static $appNames = [];
        if(empty($app) && empty($bindName)) return $appNames;
        if (empty($app) && !empty($bindName) && is_string($bindName)) {
            if(isset($appNames[$bindName])) return $bindName . '_alias';
            foreach ($appNames as $key => $names) {
                if (in_array($bindName, (array)$names, true)) {
                    return $key;
                }
            }
            return null;
        }
        if(!empty($app) && empty($bindName)) {
            return $appNames[$app] ?? $app;
        }
        if(!empty($app) && !empty($bindName)) $appNames[$app] = array_merge($appNames[$app] ?? [], is_string($bindName) ? [$bindName] : $bindName);
        return null;
    }

    /**
     * ParseControllerAction.
     * @param string $path
     * @return array|false
     * @throws ReflectionException
     */
    protected static function parseControllerAction(string $path): false|array
    {
        $path = str_replace(['-', '//'], ['', '/'], $path);
        static $cache = [];
        if (isset($cache[$path])) {
            return $cache[$path];
        }
        $pathExplode = explode('/', trim($path, '/'));
        $isPlugin = isset($pathExplode[1]) && $pathExplode[0] === 'app';
        $configPrefix = $isPlugin ? "plugin.$pathExplode[1]." : '';
        $pathPrefix = $isPlugin ? "/app/$pathExplode[1]" : '';
        $classPrefix = $isPlugin ? "plugin\\$pathExplode[1]" : '';
        $suffix = Config::get("{$configPrefix}app.controller_suffix", '');
        $relativePath = trim(substr($path, strlen($pathPrefix)), '/');
        $pathExplode = $relativePath ? explode('/', $relativePath) : [];
        if (count($pathExplode) >= 1) $pathExplode[0] = static::alias('', $pathExplode[0]) ?? $pathExplode[0];

        $action = 'index';
        if (!$controllerAction = static::guessControllerAction($pathExplode, $action, $suffix, $classPrefix)) {
            if (count($pathExplode) <= 1) {
                return false;
            }
            $action = end($pathExplode);
            unset($pathExplode[count($pathExplode) - 1]);
            $controllerAction = static::guessControllerAction($pathExplode, $action, $suffix, $classPrefix);
        }
        if ($controllerAction && !isset($path[256])) {
            $cache[$path] = $controllerAction;
            if (count($cache) > 1024) {
                unset($cache[key($cache)]);
            }
        }
        return $controllerAction;
    }
}

cola

360
积分
0
获赞数
0
粉丝数
2024-11-26 加入
🔝