溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在swoole中制作一款仿制laravel的框架

發(fā)布時(shí)間:2021-03-16 09:19:37 來(lái)源:億速云 閱讀:165 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何在swoole中制作一款仿制laravel的框架,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

首先需要確定一下思路:我希望基于swoole的擴(kuò)展開(kāi)發(fā)的代碼在run起來(lái)的時(shí)候,在接收到ws或是tcp等消息時(shí),自動(dòng)路由到某個(gè)類上,同時(shí)類可以實(shí)現(xiàn)加載類的依賴注入功能。目前市面上占據(jù)主流的一款框架Laravel,其中有一個(gè)依賴注入的功能非常的便捷。一般在通常的框架中拉取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中則可以省略一些實(shí)例化的步驟, 直接通過(guò)類型約束的語(yǔ)法在方法的形參上指定某類的命名空間就自動(dòng)實(shí)例化該類進(jìn)來(lái)了。

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

想要實(shí)現(xiàn)這一點(diǎn),必須要了解php的反射機(jī)制。反射是一個(gè)比較冷門的類,他可以做到:使用namespace實(shí)例化一個(gè)類、調(diào)用類的方法等,利用這一點(diǎn),可以構(gòu)造一個(gè)自動(dòng)裝箱的類。

<?php
/***
 * 依賴注入容器,若要執(zhí)行依賴注入,請(qǐng)確保類包含構(gòu)造函數(shù)!
 */
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()
  {
    #對(duì)構(gòu)造函數(shù)賦值
    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();//進(jìn)行遞歸注入
    }

    return empty($prototypeArr) ? $this->reflection->newInstance() : $this->reflection->newInstanceArgs($insArr);
  }
}

有了這個(gè)簡(jiǎn)易的裝箱類后,可以著手實(shí)現(xiàn)類的路由功能,我們首先創(chuàng)建composer.json,鍵入如下內(nèi)容。

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

下一步,我們需要?jiǎng)?chuàng)建一個(gè)處理路由的類,這個(gè)類在常規(guī)的框架中,一般用來(lái)映射http請(qǐng)求到對(duì)應(yīng)的類的函數(shù)上,而在swoole里,請(qǐng)求會(huì)來(lái)自長(zhǎng)連接。那么在route類中則需要做相應(yīng)的處理。

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() {
    // 這里設(shè)置一些swoole的參數(shù) ...
    // 最后執(zhí)行啟動(dòng)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);
  }
  
}

最后一步,創(chuàng)建一個(gè)入口文件,引導(dǎo)路由類的執(zhí)行。

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

use App\Server\Route;

$App = new Route();
$App->start_ws();

關(guān)于“如何在swoole中制作一款仿制laravel的框架”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI