当前位置:首页 > CMS教程 > 其它CMS > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-04-18 10:23:41 浏览: 评论:0 

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

  1. class a { 
  2.   public $bClassInstance; 
  3.   public function __construct(Class b) { 
  4.     $classInstance = new b(); 
  5.   } 
  6.   public function doSth() { 
  7.     return $this->bClassInstance->xxx(); 
  8.   } 
  9. } 
  10.  
  11. $b = new b(); 
  12. $a = new a($b) 
  13. $a->doSth(); 

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

  1. class a { 
  2.   public function doSth(b $b) { 
  3.     return $b->xxx(); 
  4.   } 
  5. } 

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

  1. <?php 
  2. /*** 
  3.  * 依赖注入容器,若要执行依赖注入,请确保类包含构造函数! 
  4.  */ 
  5. namespace App\Server; 
  6.  
  7. class Container 
  8. { 
  9.   public $config; 
  10.   public $reflection; 
  11.   public function __construct($namespace) 
  12.   { 
  13.     try 
  14.     { 
  15.       $this->reflection = new \ReflectionClass($namespace); 
  16.     } 
  17.     catch (Exception $e) 
  18.     { 
  19.       echo $namespace; 
  20.     } 
  21.   } 
  22.   public function builderController($fn, $server, $frame, $userMessage) 
  23.   { 
  24.     //从route中得到的control名称 
  25.     $this->reflection->getMethod($fn)->invoke($this->autoBuilder(), $server, $frame, $userMessage); 
  26.   } 
  27.  
  28.   public function builderTask($fn, $server, $userMessage) 
  29.   { 
  30.     $this->reflection->getMethod($fn)->invoke($this->autoBuilder(), $server, $userMessage); 
  31.   } 
  32.  
  33.   public function autoBuilder() 
  34.   { 
  35.     #对构造函数赋值 
  36.     return $this->batchInstantiation($this->getPrototypeController($this->reflection)#获得字串 
  37.     ); 
  38.   } 
  39.  
  40.   protected final function getPrototypeController(\ReflectionClass $object) 
  41.   { 
  42.     $prototype = false; 
  43.     //批量从反射类中获取原型字串 
  44.     foreach ($object->getConstructor()->getParameters() as $parameter) 
  45.     { 
  46.       $prototype[] = $parameter->getClass()->name; 
  47.     } 
  48.  
  49.     return $prototype ?: []; 
  50.   } 
  51.  
  52.   protected final function batchInstantiation(array $prototypeArr) 
  53.   { 
  54.     foreach ($prototypeArr as $item) 
  55.     { 
  56.       $container = new container($item); 
  57.       $insArr[] = $container->autoBuilder();//进行递归注入 
  58.     } 
  59.  
  60.     return emptyempty($prototypeArr) ? $this->reflection->newInstance() : $this->reflection->newInstanceArgs($insArr); 
  61.   } 
  62. } 

有了这个简易的装箱类后,可以着手实现类的路由功能,我们首先创建composer.json,键入如下内容。

  1. { 
  2.   "require": { 
  3.    
  4.   }, 
  5.   "autoload": { 
  6.     "psr-4": { 
  7.     "App\\": "App/" 
  8.     } 
  9.   } 
  10. } 

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

  1. class Route 
  2. { 
  3.   public $websocketServer; 
  4.   public $model; 
  5.   public $cache; 
  6.   public function __construct() { 
  7.     $this->websocketServer = new \swoole_websocket_server("0.0.0.0", "8002"); 
  8.   } 
  9.   public function start_ws() { 
  10.     // 这里设置一些swoole的参数 ... 
  11.     // 最后执行启动swoole 
  12.     $this->websocketServer->start(); 
  13.   } 
  14.     
  15.   public function ws_onMessage(\swoole_websocket_server $server, $frame) 
  16.   { 
  17.     $userMessage = $this->filter_arr(json_decode($frame->data, true)); 
  18.     if (!$userMessage) { 
  19.       return false; 
  20.     } 
  21.       
  22.     if (!$userMessage['type'] || !$userMessage['action']) { 
  23.       return $this->call_shell("Type or action not found! "); 
  24.     } 
  25.     //使用依赖注入容器做伪路由 
  26.     $App = new Container('\App\Controller\\'.$userMessage['type']); 
  27.     return $App->builderController($userMessage['action'], $server, $frame,$userMessage); 
  28.   } 
  29.     
  30. } 

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

  1. <?php 
  2. require "vendor/autoload.php"; 
  3.  
  4. use App\Server\Route; 
  5.  
  6. $App = new Route(); 
  7. $App->start_ws();

Tags: swoole laravel

分享到: