摘要:前言也许这是我们最关系的一个环节了。一个应用简单来说无非就是请求和相应了。获取你真的该补补协程的相关知识了。
前言
分析 RequestHandler</>复制代码
也许这是我们最关系的一个环节了。一个web应用简单来说无非就是请求和相应了。
获取你真的该补补 协程 的相关知识了。不过。。
不懂协程懂进程~ 那就 当成进程来看 一个请求一个进 (xie) 程.
懂线程~ 那就 当成 线程来看 一个请求一个线 (xie) 程
</>复制代码
vendor/zanphp/http-server/src/RequestHandler.php
</>复制代码
class RequestHandler
{
// ...
// 让 协程来 处理每个 请求
$requestTask = new RequestTask($request, $swooleResponse, $this->context, $this->middleWareManager);
$coroutine = $requestTask->run();
$this->task = new Task($coroutine, $this->context);
$this->task->run();
clear_ob();
// ..
}
分析 RequestTask
</>复制代码
vendor/zanphp/http-server/src/RequestTask.php
</>复制代码
class RequestTask
{
public function run()
{
yield $this->doRun();
}
public function doRun()
{
// 处理 中间件 逻辑
$response = (yield $this->middleWareManager->executeFilters());
if (null !== $response) {
$this->context->set("response", $response);
/** @var ResponseTrait $response */
yield $response->sendBy($this->swooleResponse);
$this->context->getEvent()->fire($this->context->get("request_end_event_name"));
return;
}
// 处理 中间件 放行后的 逻辑
$dispatcher = Di::make(Dispatcher::class);
$response = (yield $dispatcher->dispatch($this->request, $this->context));
if (null === $response) {
$code = BaseResponse::HTTP_INTERNAL_SERVER_ERROR;
$response = new InternalErrorResponse("网络错误($code)", $code);
}
yield $this->middleWareManager->executePostFilters($response);
$this->context->set("response", $response);
yield $response->sendBy($this->swooleResponse);
$this->context->getEvent()->fire($this->context->get("request_end_event_name"));
clear_ob();
}
}
分析 Dispatcher
</>复制代码
vendor/zanphp/http-server/src/Dispatcher.php
</>复制代码
get("controller_name");
$action = $context->get("action_name");
$args = $context->get("action_args");
if ($args == null) {
$args = [];
}
if ($controllerName === "/" && is_callable($action)) {
yield $action(...array_values($args));
} else {
$controller = $this->getControllerClass($controllerName);
if(!class_exists($controller)) {
// 这些错 常见吧
throw new PageNotFoundException("controller:{$controller} not found");
}
$controller = new $controller($request, $context);
if(!is_callable([$controller, $action])) {
// 这些错 常见吧
throw new PageNotFoundException("action:{$action} is not callable in controller:" . get_class($controller));
}
yield $controller->$action(...array_values($args));
}
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/26067.html
摘要:中的容器容器介绍容器中获取一个类的实例容器中注册于获取类的单例函数理解面向接口开发会帮助你更好的理解容器类容器帮助函数获取类的实例注册类的单例占位有待补充 PHP协程与yield 我说不如你查阅相关文档与资料 Iterator(迭代器)接口 生成器总览在PHP中使用协程实现多任务调度 当然 如果你暂时 懒的话 yield 当成 return 关键字就行 zanphp中的命名空间 Za...
前言 本系列源码解读已 http-demo 项目为例 目录说明 showImg(https://segmentfault.com/img/bVX8wy?w=452&h=431); 主要关心 图片箭头指向目录http://zanphpdoc.zanphp.io/we... bin: 服务启动bin文件目录 init: 应用初始化相关 resource: 配置文件目录,具体配置见 项目配置 src...
摘要:前言因为本系列主要解读源码,所以环境采用作者自己搭建的适用系列的环境。 前言 因为本系列主要解读zanphp源码, 所以环境采用作者自己搭建的适用 zan 系列的 docker 环境。 https://github.com/cjeruen/zan-docker 环境相关说明 本系列基础目录都在 ~/zan-code 目录下进行 如有变更 自行 切换目录 安装 docker 与 co...
摘要:前言当然从我们熟悉但不完全熟悉的说起。下面是中的具体逻辑了。这里采用的是的方式。 前言 当然从我们熟悉(但不完全熟悉)的 MVC 说起。简(zhi)单(jie)的描述. 1. MVC 概览 1.1. URL 规则 上篇 目录说明中 提到的,这里不多说 规则就是这样,后面来说其源码 1.2. Controller && Action src/Index/IndexController.p...
摘要:获取应用并启动分析设置应用名称获取本身实例想容器注册单例设置应用基础路径其他初始化工作初始化容器其他初始化工作创建根据前面的知识扫盲可知道返回的真身是位于分析继承这里就把中的函数都放在分析了服务的启动主入口函 获取应用并启动 php bin/httpd
阅读 2120·2021-09-13 10:23
阅读 2446·2021-09-02 09:47
阅读 3965·2021-08-16 11:01
阅读 1317·2021-07-25 21:37
阅读 1716·2019-08-30 15:56
阅读 635·2019-08-30 13:52
阅读 3227·2019-08-26 10:17
阅读 2529·2019-08-23 18:17
极致性价比!云服务器续费无忧!
Tesla A100/A800、Tesla V100S等多种GPU云主机特惠2折起,不限台数,续费同价。
NVIDIA RTX 40系,高性价比推理显卡,满足AI应用场景需要。
乌兰察布+上海青浦,满足东推西训AI场景需要