作者:雪儿babyqueenpb_446 | 来源:互联网 | 2023-07-18 10:43
在swoole中制作一款仿制laravel的框架首先需要确定一下思路:我希望基于swoole的扩展开发的代码在run起来的时候,在接收到ws或是tcp等消息时,自
在 swoole 中制作一款仿制 laravel 的框架
首先需要确定一下思路:我希望基于 swoole 的扩展开发的代码在 run 起来的时候,在接收到 ws 或是 tcp 等消息时,自动路由到某个类上,同时类可以实现加载类的依赖注入功能。目前市面上占据主流的一款框架 Laravel,其中有一个依赖注入的功能非常的便捷。一般在通常的框架中拉取 Class 是这样做的:
1 2 3 4 5 6 7 8 9 10 11 12 13
| class a {
public $bClassInstance;
public function __construct(Class b) {
$classInstance = new b();
}
public function doSth() {
return $this->bClassInstance->xxx();
}
}
$b = new b();
$a = new a($b)
$a->doSth(); |
而在 Laravel 中则可以省略一些实例化的步骤, 直接通过类型约束的语法在方法的形参上指定某类的命名空间就自动实例化该类进来了。
1 2 3 4 5
| class a {
public function doSth(b $b) {
return $b->xxx();
}
} |
想要实现这一点,必须要了解 php 的反射机制。反射是一个比较冷门的类,他可以做到:使用 namespace 实例化一个类、调用类的方法等,利用这一点,可以构造一个自动装箱的类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| /***
* 依赖注入容器,若要执行依赖注入,请确保类包含构造函数!
*/
namespace App\Server;
class Container
{
public $config;
public $reflection;
public function __construct($namespace)
{
try
{
$this->reflection = new \ReflectionClass($namespace);
}
catch (Exception $e)
{
echo $namespace;
}
}
public function builderController($fn, $server, $frame, $userMessage)
{
//从 route 中得到的 control 名称
$this->reflection->getMethod($fn)->invoke($this->autoBuilder(), $server, $frame, $userMessage);
}
public function builderTask($fn, $server, $userMessage)
{
$this->reflection->getMethod($fn)->invoke($this->autoBuilder(), $server, $userMessage);
}
public function autoBuilder()
{
#对构造函数赋值
return $this->batchInstantiation($this->getPrototypeController($this->reflection)#获得字串
);
}
protected final function getPrototypeController(\ReflectionClass $object)
{
$prototype = false;
//批量从反射类中获取原型字串
foreach ($object->getConstructor()->getParameters() as $parameter)
{
$prototype[] = $parameter->getClass()->name;
}
return $prototype ?: [];
}
protected final function batchInstantiation(array $prototypeArr)
{
foreach ($prototypeArr as $item)
{
$cOntainer= new container($item);
$insArr[] = $container->autoBuilder();//进行递归注入
}
return empty($prototypeArr) ? $this->reflection->newInstance() : $this->reflection->newInstanceArgs($insArr);
}
}
|
有了这个简易的装箱类后,可以着手实现类的路由功能,我们首先创建 composer.json,键入如下内容。
1 2 3 4 5 6 7 8 9 10
| {
"require": {
},
"autoload": {
"psr-4": {
"App\": "App/"
}
}
} |
下一步,我们需要创建一个处理路由的类,这个类在常规的框架中,一般用来映射 http 请求到对应的类的函数上,而在 swoole 里,请求会来自长连接。那么在 route 类中则需要做相应的处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class Route
{
public $websocketServer;
public $model;
public $cache;
public function __construct() {
$this->websocketServer = new \swoole_websocket_server("0.0.0.0", "8002");
}
public function start_ws() {
// 这里设置一些 swoole 的参数 ...
// 最后执行启动 swoole
$this->websocketServer->start();
}
public function ws_onMessage(\swoole_websocket_server $server, $frame)
{
$userMessage = $this->filter_arr(json_decode($frame->data, true));
if (!$userMessage) {
return false;
}
if (!$userMessage['type'] || !$userMessage['action']) {
return $this->call_shell("Type or action not found! ");
}
//使用依赖注入容器做伪路由
$App = new Container('\App\Controller\'.$userMessage['type']);
return $App->builderController($userMessage['action'], $server, $frame,$userMessage);
}
} |
最后一步,创建一个入口文件,引导路由类的执行。
1 2 3 4 5 6 7
| require "vendor/autoload.php";
use App\Server\Route;
$App = new Route();
$App->start_ws();
|
你好,Swoft 可以参考一下