swoole-framwork/index.php

39 lines
1.3 KiB
PHP
Raw Normal View History

2024-05-20 11:51:07 +08:00
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Swoole\Http\Request;
use Swoole\Http\Response;
require_once __DIR__."/app/config/define.php";
\Core\BeanFactory::init();
$dispatcher = \Core\BeanFactory::getBean('RouterCollector')->getDispatcher();
$http = new Swoole\Http\Server("0.0.0.0",8085);
$http->on('request',function (Request $request,Response $response) use ($dispatcher) {
$myRequest = Core\http\Request::init($request);
$myResponse = \Core\http\Response::init($response);
$routeInfo = $dispatcher->dispatch($myRequest->getMethod(),$myRequest->getUri());
//var_dump($routeInfo);
switch($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
$response->status(404);
$response->end();
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$response->status(405);
$response->end();
break;
case FastRoute\Dispatcher::FOUND:
$handler = $routeInfo[1];
$vars = $routeInfo[2];//参数
$extVars = [$myRequest, $myResponse];
//$response->end($handler($vars, $extVars));
$myResponse->setBody($handler($vars,$extVars));
$myResponse->end();
break;
}
});
$http->start();