当前位置:首页 > PHP教程 > php函数 > 列表

分享几个 Hyperf 常用助手函数

发布:smiling 来源: PHP粉丝网  添加日期:2022-07-01 08:36:01 浏览: 评论:0 

使用 hyperf 已经有一段时间了,下面是一些常用的助手函数,分享一下~~~

  1. <?php 
  2.  
  3.  
  4.  
  5. use Hyperf\Contract\StdoutLoggerInterface; 
  6.  
  7. use Hyperf\HttpServer\Contract\ResponseInterface; 
  8.  
  9. use Hyperf\Logger\LoggerFactory; 
  10.  
  11. use Hyperf\Server\ServerFactory; 
  12.  
  13. use Hyperf\Utils\ApplicationContext; 
  14.  
  15. use Psr\Http\Message\ServerRequestInterface; 
  16.  
  17. use Swoole\Websocket\Frame; 
  18.  
  19. use Swoole\WebSocket\Server as WebSocketServer; 
  20.  
  21.  
  22.  
  23. /** 
  24.  
  25.  * 容器实例 
  26.  
  27.  */ 
  28.  
  29. if (!function_exists('container')) { 
  30.  
  31.     function container() 
  32.  
  33.     { 
  34.  
  35.         return ApplicationContext::getContainer(); 
  36.  
  37.     } 
  38.  
  39. } 
  40.  
  41.  
  42.  
  43. /** 
  44.  
  45.  * redis 客户端实例 
  46.  
  47.  */ 
  48.  
  49. if (!function_exists('redis')) { 
  50.  
  51.     function redis() 
  52.  
  53.     { 
  54.  
  55.         return container()->get(Redis::class); 
  56.  
  57.     } 
  58.  
  59. } 
  60.  
  61.  
  62.  
  63. /** 
  64.  
  65.  * server 实例 基于 swoole server 
  66.  
  67.  */ 
  68.  
  69. if (!function_exists('server')) { 
  70.  
  71.     function server() 
  72.  
  73.     { 
  74.  
  75.         return container()->get(ServerFactory::class)->getServer()->getServer(); 
  76.  
  77.     } 
  78.  
  79. } 
  80.  
  81.  
  82.  
  83. /** 
  84.  
  85.  * websocket frame 实例 
  86.  
  87.  */ 
  88.  
  89. if (!function_exists('frame')) { 
  90.  
  91.     function frame() 
  92.  
  93.     { 
  94.  
  95.         return container()->get(Frame::class); 
  96.  
  97.     } 
  98.  
  99. } 
  100.  
  101.  
  102.  
  103. /** 
  104.  
  105.  * websocket 实例 
  106.  
  107.  */ 
  108.  
  109. if (!function_exists('websocket')) { 
  110.  
  111.     function websocket() 
  112.  
  113.     { 
  114.  
  115.         return container()->get(WebSocketServer::class); 
  116.  
  117.     } 
  118.  
  119. } 
  120.  
  121.  
  122.  
  123. /** 
  124.  
  125.  * 缓存实例 简单的缓存 
  126.  
  127.  */ 
  128.  
  129. if (!function_exists('cache')) { 
  130.  
  131.     function cache() 
  132.  
  133.     { 
  134.  
  135.         return container()->get(Psr\SimpleCache\CacheInterface::class); 
  136.  
  137.     } 
  138.  
  139. } 
  140.  
  141.  
  142.  
  143. /** 
  144.  
  145.  * 控制台日志 
  146.  
  147.  */ 
  148.  
  149. if (!function_exists('stdLog')) { 
  150.  
  151.     function stdLog() 
  152.  
  153.     { 
  154.  
  155.         return container()->get(StdoutLoggerInterface::class); 
  156.  
  157.     } 
  158.  
  159. } 
  160.  
  161.  
  162.  
  163. /** 
  164.  
  165.  * 文件日志 
  166.  
  167.  */ 
  168.  
  169. if (!function_exists('logger')) { 
  170.  
  171.     function logger() 
  172.  
  173.     { 
  174.  
  175.         return container()->get(LoggerFactory::class)->make(); 
  176.  
  177.     } 
  178.  
  179. } 
  180.  
  181.  
  182.  
  183. /** 
  184.  
  185.  * 
  186.  
  187.  */ 
  188.  
  189. if (!function_exists('request')) { 
  190.  
  191.     function request() 
  192.  
  193.     { 
  194.  
  195.         return container()->get(ServerRequestInterface::class); 
  196.  
  197.     } 
  198.  
  199. } 
  200.  
  201.  
  202.  
  203. /** 
  204.  
  205.  * 
  206.  
  207.  */ 
  208.  
  209. if (!function_exists('response')) { 
  210.  
  211.     function response() 
  212.  
  213.     { 
  214.  
  215.         return container()->get(ResponseInterface::class); 
  216.  
  217.     } 
  218.  
  219. }

Tags: Hyperf

分享到: