Skip to content

Commit

Permalink
refactor: refactor requestId
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed Jul 27, 2020
1 parent 595075a commit 808f56e
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 76 deletions.
25 changes: 25 additions & 0 deletions _ide_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/*
* Yii2 Ide Helper
* https://github.com/takashiki/yii2-ide-helper
*/

class Yii extends \yii\BaseYii
{
/**
* @var BaseApplication
*/
public static $app;
}

/**
* @property Mis\IdeHelper\IdeHelper $ideHelper
* @property yiier\helpers\RequestId $requestId
* @property yii\caching\FileCache $cache
* @property yii\db\Connection $db
* @property yii\web\Response $response
* @property sizeg\jwt\Jwt $jwt
* @property yii\web\User $user
* @property yii\swiftmailer\Mailer $mailer
*/
abstract class BaseApplication extends \yii\base\Application {}
3 changes: 1 addition & 2 deletions commands/HelloController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace app\commands;

use app\core\behaviors\LoggerBehavior;
use yii\console\Controller;
use yii\console\ExitCode;

Expand All @@ -31,7 +30,7 @@ public function actionIndex($message = 'hello world')
{
echo $message . "\n";

\Yii::error(['request_id' => LoggerBehavior::getRequestId(), 'test_request_id']);
\Yii::error(['request_id' => \Yii::$app->requestId->id, 'test_request_id']);
return ExitCode::OK;
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"codeception/module-rest": "^1.0.0",
"codeception/module-phpbrowser": "^1.0.0",
"squizlabs/php_codesniffer": "^3.5.5",
"phpro/grumphp": "^0.19.1"
"phpro/grumphp": "^0.19.1",
"mis/yii2-ide-helper": "^1.0"
},
"config": {
"process-timeout": 1800,
Expand Down
69 changes: 61 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions config/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@
'timeZone' => env('APP_TIME_ZONE'),
'language' => env('APP_LANGUAGE'),
'name' => env('APP_NAME'),
'bootstrap' => ['log', 'ideHelper', \app\core\EventBootstrap::class],
'components' => [
'ideHelper' => [
'class' => 'Mis\IdeHelper\IdeHelper',
'configFiles' => [
'config/web.php',
'config/common.php',
'config/console.php',
],
],
'requestId' => [
'class' => \yiier\helpers\RequestId::class,
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
Expand Down
1 change: 0 additions & 1 deletion config/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
$config = [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'app\commands',
'aliases' => [
'@bower' => '@vendor/bower-asset',
Expand Down
4 changes: 0 additions & 4 deletions config/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => [
'log',
\app\core\EventBootstrap::class,
],
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
Expand Down
3 changes: 1 addition & 2 deletions controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace app\controllers;

use app\core\behaviors\LoggerBehavior;
use Yii;
use yii\rest\Controller;

Expand Down Expand Up @@ -34,7 +33,7 @@ public function actionError(): array
$exception = Yii::$app->errorHandler->exception;
if ($exception !== null) {
Yii::error([
'request_id' => LoggerBehavior::getRequestId(),
'request_id' => Yii::$app->requestId->id,
'exception' => $exception->getMessage(),
'line' => $exception->getLine(),
'file' => $exception->getFile(),
Expand Down
58 changes: 2 additions & 56 deletions core/behaviors/LoggerBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,9 @@
use yii\helpers\Json;
use yii\web\Controller;
use yii\web\Response;
use yiier\helpers\Security;

class LoggerBehavior extends Behavior
{
/**
* @var string
*/
public static $delimiter = '-';

/**
* @var string
*/
public static $requestIdParamName = 'X_REQUEST_ID';

/**
* @var string
*/
public static $requestIdHeaderName = 'X-Request-ID';

public function events()
{
return [
Expand All @@ -43,7 +27,7 @@ public function beforeSend($event)
{
$response = $event->sender;
$request = \Yii::$app->request;
$requestId = self::getRequestId();
$requestId = Yii::$app->requestId->id;
$code = ArrayHelper::getValue($response->data, 'code');
$message = [
'request_id' => $requestId,
Expand All @@ -59,44 +43,6 @@ public function beforeSend($event)

public function beforeAction()
{
return self::getRequestId();
}

/**
* @return string
*/
public static function getRequestId()
{
try {
$console = Yii::$app instanceof \yii\console\Application;
if (
(!$console) &&
$requestId = ArrayHelper::getValue(Yii::$app->request->getHeaders(), self::$requestIdHeaderName)
) {
$tmp = explode(self::$delimiter, $requestId);
if (count($tmp) < 2) {
$tmp = self::genRequestId();
}
$tmp[1] = (int)$tmp[1] + 1;
$requestId = sprintf('%s%s%04d', $tmp[0], self::$delimiter, $tmp[1]);
} elseif (!$requestId = ArrayHelper::getValue(Yii::$app->params, self::$requestIdParamName)) {
$requestId = self::genRequestId();
;
}
\Yii::$app->params[self::$requestIdParamName] = $requestId;
} catch (\Exception $e) {
Yii::error($e, __FUNCTION__);
$requestId = null;
}
return $requestId;
}

/**
* @return string
* @throws \Exception
*/
private static function genRequestId()
{
return sprintf('%s%s%04d', Security::generateRealUniqId(20), self::$delimiter, 0);
return Yii::$app->requestId->id;
}
}
3 changes: 1 addition & 2 deletions core/services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace app\core\services;

use app\core\behaviors\LoggerBehavior;
use app\core\exceptions\InternalException;
use app\core\models\User;
use app\core\requests\JoinRequest;
Expand Down Expand Up @@ -33,7 +32,7 @@ public function createUser(JoinRequest $request): User
}
} catch (Exception $e) {
Yii::error(
['request_id' => LoggerBehavior::getRequestId(), $user->attributes, $user->errors, (string)$e],
['request_id' => Yii::$app->requestId->id, $user->attributes, $user->errors, (string)$e],
__FUNCTION__
);
throw new InternalException($e->getMessage());
Expand Down
1 change: 1 addition & 0 deletions grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ grumphp:
- ./vendor/*
- ./runtime/*
- ./requirements.php
- ./_ide_helper.php

0 comments on commit 808f56e

Please sign in to comment.