donjan-deng / hyperf-casbin

适配 Hyperf 的开源访问控制框架Casbin
Apache License 2.0
88 stars 21 forks source link

OnPolicyChangedListener 中的 Undefined array key "type" #37

Closed hjw0968 closed 7 months ago

hjw0968 commented 7 months ago
image

$serverConfig['type'] 没有做判空处理

hjw0968 commented 7 months ago
// hyperf 3.1
// donjan-deng/hyperf-casbin 3.23.1
// autoload/server.php
return [
    'mode' => SWOOLE_PROCESS,
    'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9504,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
            'options' => [
                // Whether to enable request lifecycle event
                'enable_request_lifecycle' => false,
            ],
        ],
        [
            'name' => 'socket-io',
            'type' => Server::SERVER_WEBSOCKET,
            'host' => '0.0.0.0',
            'port' => 9505,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
                Event::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
                Event::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
            ],
        ],
    ],
    'settings' => [
        Constant::OPTION_ENABLE_COROUTINE => true,
        Constant::OPTION_WORKER_NUM => env('APP_ENV', 'dev') == 'dev' ? 2 : swoole_cpu_num(),
        Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid',
        Constant::OPTION_OPEN_TCP_NODELAY => true,
        Constant::OPTION_MAX_COROUTINE => 100000,
        Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,
        Constant::OPTION_MAX_REQUEST => 0,
        Constant::OPTION_PACKAGE_MAX_LENGTH => 5 * 1024 * 1024,
        Constant::OPTION_SOCKET_BUFFER_SIZE => 3 * 1024 * 1024,
        Constant::OPTION_BUFFER_OUTPUT_SIZE => 3 * 1024 * 1024,
        // 静态资源配置
        Constant::OPTION_DOCUMENT_ROOT => env('UPLOAD_PATH', BASE_PATH . '/public'),
        Constant::OPTION_ENABLE_STATIC_HANDLER => true,
        // 安全重启
        //        Constant::OPTION_MAX_WAIT_TIME => 60,
        //        Constant::OPTION_RELOAD_ASYNC => true,
        // Task Worker 数量,根据您的服务器配置而配置适当的数量
        Constant::OPTION_TASK_WORKER_NUM => env('APP_OPTION_TASK_WORKER_NUM', 2),
        // 因为 `Task` 主要处理无法协程化的方法,所以这里推荐设为 `false`,避免协程下出现数据混淆的情况
        Constant::OPTION_TASK_ENABLE_COROUTINE => false,
    ],
    'callbacks' => [
        Event::ON_BEFORE_START => [App\Bootstrap\ServerStart::class, 'beforeStart'],
        Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
        // Task callbacks
        Event::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
        Event::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
    ],
];
// vendor/donjan-deng/hyperf-casbin/src/Listener/OnPolicyChangedListener.php

            $serverConfig = $this->container->get(ConfigInterface::class)->get('server', []);
            if (! $serverConfig) {
                throw new \InvalidArgumentException('At least one server should be defined.');
            }
            if ($serverConfig['type'] == Server::class) { // 此处报错
                $server = $this->container->get(Server::class);
                $workerCount = $server->setting['worker_num'] + ($server->setting['task_worker_num'] ?? 0) - 1;
                if ($workerCount > 0) {
                    for ($workerId = 0; $workerId <= $workerCount; ++$workerId) {
                        if ($server->worker_id > -1 && $server->worker_id != $workerId) {
                            $server->sendMessage(new PipeMessage(PipeMessage::LOAD_POLICY), $workerId);
                        }
                    }
                }
            }
hjw0968 commented 7 months ago

目前启用 casbin.watcher.enabled 跳过下方判断暂时解决 今天给hyperf都更新到3.1后才发现这个事😂