使用 hyperf 已經有一段時間了,下面是一些常用的助手函式,分享一下~~~
<?php
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Server\ServerFactory;
use Hyperf\Utils\ApplicationContext;
use Psr\Http\Message\ServerRequestInterface;
use Swoole\Websocket\Frame;
use Swoole\WebSocket\Server as WebSocketServer;
/**
* 容器例項
*/
if (!function_exists('container')) {
function container()
{
return ApplicationContext::getContainer();
}
}
/**
* redis 客戶端例項
*/
if (!function_exists('redis')) {
function redis()
{
return container()->get(Redis::class);
}
}
/**
* server 例項 基於 swoole server
*/
if (!function_exists('server')) {
function server()
{
return container()->get(ServerFactory::class)->getServer()->getServer();
}
}
/**
* websocket frame 例項
*/
if (!function_exists('frame')) {
function frame()
{
return container()->get(Frame::class);
}
}
/**
* websocket 例項
*/
if (!function_exists('websocket')) {
function websocket()
{
return container()->get(WebSocketServer::class);
}
}
/**
* 快取例項 簡單的快取
*/
if (!function_exists('cache')) {
function cache()
{
return container()->get(Psr\SimpleCache\CacheInterface::class);
}
}
/**
* 控制檯日誌
*/
if (!function_exists('stdLog')) {
function stdLog()
{
return container()->get(StdoutLoggerInterface::class);
}
}
/**
* 檔案日誌
*/
if (!function_exists('logger')) {
function logger()
{
return container()->get(LoggerFactory::class)->make();
}
}
/**
*
*/
if (!function_exists('request')) {
function request()
{
return container()->get(ServerRequestInterface::class);
}
}
/**
*
*/
if (!function_exists('response')) {
function response()
{
return container()->get(ResponseInterface::class);
}
}
By: Laravel-China 寧澤林
MyBlog: nizer.in