在swoole中制作一款仿制laravel的框架的實(shí)例代碼
首先需要確定一下思路:我希望基于swoole的擴(kuò)展開發(fā)的代碼在run起來的時(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í)例化的步驟, 直接通過類型約束的語法在方法的形參上指定某類的命名空間就自動(dòng)實(shí)例化該類進(jìn)來了。
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í)行依賴注入,請確保類包含構(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() { #對構(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è)簡易的裝箱類后,可以著手實(shí)現(xiàn)類的路由功能,我們首先創(chuàng)建composer.json,鍵入如下內(nèi)容。
{ "require": { }, "autoload": { "psr-4": { "App\\": "App/" } } }
下一步,我們需要?jiǎng)?chuàng)建一個(gè)處理路由的類,這個(gè)類在常規(guī)的框架中,一般用來映射http請求到對應(yīng)的類的函數(shù)上,而在swoole里,請求會來自長連接。那么在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的框架的文章就介紹到這了,更多相關(guān)swoole laravel框架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
laravel validate 設(shè)置為中文的例子(驗(yàn)證提示為中文)
今天小編就為大家分享一篇laravel validate 設(shè)置為中文的例子(驗(yàn)證提示為中文),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09php 將bmp圖片轉(zhuǎn)為jpg等其他任意格式的圖片
有時(shí)候我們需要將bmp格式的圖片轉(zhuǎn)換為jpg或gif等格式,大家就可以參考下下面的代碼。2009-06-06php中pcntl_fork創(chuàng)建子進(jìn)程的方法實(shí)例
這篇文章主要介紹了php中pcntl_fork創(chuàng)建子進(jìn)程的方法實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03復(fù)現(xiàn)WordPress?xmlrpc.php漏洞和SSRF的詳細(xì)步驟
這篇文章主要介紹了復(fù)現(xiàn)WordPress?xmlrpc.php漏洞和SSRF的相關(guān)資料,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04PHP+MYSQL實(shí)現(xiàn)讀寫分離簡單實(shí)戰(zhàn)
本篇文章主要介紹了PHP+MYSQL實(shí)現(xiàn)讀寫分離,實(shí)例分析了讀寫分離的技巧,從而提高數(shù)據(jù)庫的負(fù)載能力,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03ThinkPHP權(quán)限認(rèn)證Auth實(shí)例詳解
這篇文章主要介紹了ThinkPHP權(quán)限認(rèn)證Auth實(shí)例,需要的朋友可以參考下2014-07-07