欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在swoole中制作一款仿制laravel的框架的实例代码

程序员文章站 2022-07-06 13:10:53
首先需要确定一下思路:我希望基于swoole的扩展开发的代码在run起来的时候,在接收到ws或是tcp等消息时,自动路由到某个类上,同时类可以实现加载类的依赖注入功能。目前市面上占据主流的一款框架la...

首先需要确定一下思路:我希望基于swoole的扩展开发的代码在run起来的时候,在接收到ws或是tcp等消息时,自动路由到某个类上,同时类可以实现加载类的依赖注入功能。目前市面上占据主流的一款框架laravel,其中有一个依赖注入的功能非常的便捷。一般在通常的框架中拉取class是这样做的:

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中则可以省略一些实例化的步骤, 直接通过类型约束的语法在方法的形参上指定某类的命名空间就自动实例化该类进来了。

class a {
  public function dosth(b $b) {
    return $b->xxx();
  }
}

想要实现这一点,必须要了解php的反射机制。反射是一个比较冷门的类,他可以做到:使用namespace实例化一个类、调用类的方法等,利用这一点,可以构造一个自动装箱的类。

<?php
/***
 * 依赖注入容器,若要执行依赖注入,请确保类包含构造函数!
 */
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,键入如下内容。

{
  "require": {
 
  },
  "autoload": {
    "psr-4": {
    "app\\": "app/"
    }
  }
}

下一步,我们需要创建一个处理路由的类,这个类在常规的框架中,一般用来映射http请求到对应的类的函数上,而在swoole里,请求会来自长连接。那么在route类中则需要做相应的处理。

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);
  }
  
}

最后一步,创建一个入口文件,引导路由类的执行。

<?php
require "vendor/autoload.php";

use app\server\route;

$app = new route();
$app->start_ws();

到此这篇关于在swoole中制作一款仿制laravel的框架的文章就介绍到这了,更多相关swoole laravel框架内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!