Skip to content

Commit

Permalink
optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
derek committed Nov 24, 2019
1 parent 67614b7 commit 011bde1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
32 changes: 28 additions & 4 deletions app/Kernel/Functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of Hyperf.
*
Expand All @@ -17,7 +18,10 @@
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
use Hyperf\Utils\ApplicationContext;

if (! function_exists('di')) {
/**
* 获取Container
*/
if (!function_exists('di')) {
/**
* Finds an entry of the container by its identifier and returns it.
* @param null|mixed $id
Expand All @@ -34,7 +38,27 @@ function di($id = null)
}
}

if (! function_exists('format_throwable')) {
/**
* redis 客户端实例
*/
if (!function_exists('redis')) {
function redis()
{
return di()->get(\Redis::class);
}
}

/**
* 缓存实例 简单的缓存
*/
if (!function_exists('cache')) {
function cache()
{
return di()->get(\Psr\SimpleCache\CacheInterface::class);
}
}

if (!function_exists('format_throwable')) {
/**
* Format a throwable to string.
* @param Throwable $throwable
Expand All @@ -46,7 +70,7 @@ function format_throwable(Throwable $throwable): string
}
}

if (! function_exists('queue_push')) {
if (!function_exists('queue_push')) {
/**
* Push a job to async queue.
*/
Expand All @@ -57,7 +81,7 @@ function queue_push(JobInterface $job, int $delay = 0, string $key = 'default'):
}
}

if (! function_exists('amqp_produce')) {
if (!function_exists('amqp_produce')) {
/**
* Produce a amqp message.
*/
Expand Down
18 changes: 16 additions & 2 deletions app/Kernel/Log/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@

namespace App\Kernel\Log;

use Hyperf\Utils\ApplicationContext;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Logger\LoggerFactory;

class Log
{
/**
* 文件日志
* @param string $name
* @return \Psr\Log\LoggerInterface
*/
public static function get(string $name = 'app')
{
return ApplicationContext::getContainer()->get(\Hyperf\Logger\LoggerFactory::class)->get($name);
return di()->get(LoggerFactory::class)->get($name);
}

/**
* 控制台日志
*/
public static function stdLog()
{
return di()->get(StdoutLoggerInterface::class);
}
}

0 comments on commit 011bde1

Please sign in to comment.