Skip to content

Commit

Permalink
重写yii2-debugger组件,修复yii2-debuuger工具栏显示的时间问题
Browse files Browse the repository at this point in the history
  • Loading branch information
liufee committed Dec 23, 2017
1 parent f80c7b6 commit e361a04
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 26 deletions.
71 changes: 48 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ yii2 swoole

这里用作比较的demo是采用yii2框架开发的一款cms系统[FeehiCMS](http://www.github.com/liufee/cms),因为FeehiCMS只开发基础cms功能,未对yii框架做任何封装、改造,故选择此作为体验demo。


yii2-swoole和php-fpm下的FeehiCMS
---------------
使用yii2自带的yii2-debugger,比较后端响应时间。

因yii2使用YII_BEGIN_TIME常量和register_shutdown_function函数实现yii2-debugger,而swoole常住内存,所以此处yii2-swoole复写了这两个组件少数方法。swoole开始时间从onRequest开始算起。
* FeehiCMS前台
- php-fpm:
![php-fpm](docs/feehicms_frontend_index_php-fpm.png)
- yii2-swoole
![php-fpm](docs/feehicms_frontend_index_yii2-swoole.png)


* FeehiCMS后台
- php-fpm:
![php-fpm](docs/feehicms_backend_php-fpm.png)
- yii2-swoole
![php-fpm](docs/feehicms_backend_yii2-swoole.png)

安装
---------------
1. 使用composer
Expand Down Expand Up @@ -50,6 +69,9 @@ yii2 swoole
'app' => 'frontend',//app目录地址
'host' => '127.0.0.1',//监听地址
'port' => 9999,//监听端口
'web' => 'web',//默认为web。rootDir app web目的是拼接yii2的根目录,如果你的应用为basic,那么frontend为空即可。
'debug' => true,//默认开启debug,上线应置为false
'env' => 'dev',//默认为dev,上线应置为prod
'swooleConfig' => [//标准的swoole配置项都可以再此加入
'reactor_num' => 2,
'worker_num' => 4,
Expand All @@ -65,14 +87,17 @@ yii2 swoole
'app' => 'backend',
'host' => '127.0.0.1',
'port' => 9998,
'web' => 'web',//默认为web。rootDir app web目的是拼接yii2的根目录,如果你的应用为basic,那么frontend为空即可。
'debug' => true,//默认开启debug,上线应置为false
'env' => 'dev',//默认为dev,上线应置为prod
'swooleConfig' => [
'reactor_num' => 2,
'worker_num' => 4,
'daemonize' => false,
'log_file' => __DIR__ . '/../../backend/runtime/logs/swoole.log',
'log_level' => 0,
'pid_file' => __DIR__ . '/../../backend/runtime/server.pid',
],
'reactor_num' => 2,
'worker_num' => 4,
'daemonize' => false,
'log_file' => __DIR__ . '/../../backend/runtime/logs/swoole.log',
'log_level' => 0,
'pid_file' => __DIR__ . '/../../backend/runtime/server.pid',
],
]
...//其他配置
]
Expand All @@ -94,28 +119,33 @@ yii2 swoole
* 关闭 /path/to/php /path/to/yii swoole-backend/stop
* 重启 /path/to/php /path/to/yii swoole-backend/restart

使用systemd启动和开机自动启动

使用systemd管理yii2-swoole的启动关闭
---------------------------
* 使用systemd来管理服务
像管理apache一样使用service httpd start和service httpd stop以及service httpd restart来启动、关闭、重启yii2 swoole服务了。

1. 复制feehi.service和feehi-backend.service到/etc/systemd/system目录
2. 分别修改feehi.service和feehi-backend.service中[Service]部分的 /path/to/yii2app为你的目录,/path/to/php为你的php命令绝对路径
3. 运行systemctl daemon-reload

现在可以使用 serice feehi start和service feehi stop以及service feehi restart启动、关闭、重启前台。
serice feehi-backend start和service feehi-backend stop以及service feehi-backend restart启动、关闭、重启后台

现在可以像管理apache一样使用service httpd start和service httpd stop以及service httpd restart来启动、关闭、重启yii2 swoole服务了。serice feehi start和service feehi stop以及service feehi restart启动、关闭、重启前台。
serice feehi-backend start和service feehi-backend stop以及service feehi-backend restart启动、关闭、重启后台


* 加入开机自动启动

加入开机自动启动
---------------------------

方法一

1. 使用systemd管理服务
2. 运行systemctl enable feehi以及systemctl enable feehi-backend设置开机自动启动
方法二

在/etc/rc.local中加入/path/to/php /path/to/yii2app/yii swoole/start和/path/to/php /path/to/yii2app/yii swoole-backend/start两行
在/etc/rc.local中加入
/path/to/php /path/to/yii2app/yii swoole/start
/path/to/php /path/to/yii2app/yii swoole-backend/start


Nginx配置
Expand All @@ -131,7 +161,7 @@ Nginx配置
root $web;
server_name swoole.cms.test.docker;

location ~* .(ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3) {
location ~* .(ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3)$ {
root $web;
}

Expand Down Expand Up @@ -159,7 +189,7 @@ Nginx配置
root $web;
server_name swoole-admin.cms.test.docker;

location ~* .(ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3) {
location ~* .(ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3)$ {
root $web;
}

Expand All @@ -176,9 +206,4 @@ Nginx配置

调试
-------------
var_dump、echo都是输出到控制台,不方便调试。可以使用\feehi\swoole\Util::dump(),输出数组、对象、字符串、布尔值到浏览器


其他
-------------
以上是把swoole启动/关闭/重启命令集成到了yii2 console里面,如果你并不想使用集成到yii2 console的swoole,可以复制vendor/feehi/yii2-swoole下的backend.php和frontend.php,修改$rootDir = "/path/to/project"为真正的yii2项目根目录,然后执行php backend.php以及php frontend.php启动swoole
var_dump、echo都是输出到控制台,不方便调试。可以使用\feehi\swoole\Util::dump(),输出数组、对象、字符串、布尔值到浏览器
Binary file added docs/feehicms_backend_php-fpm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/feehicms_backend_yii2-swoole.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/feehicms_frontend_index_php-fpm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/feehicms_frontend_index_yii2-swoole.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions src/console/SwooleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

namespace feehi\console;

use feehi\web\Logger;
use yii;
use feehi\debug\panels\ProfilingPanel;
use feehi\debug\panels\TimelinePanel;
use feehi\debug\Module;
use feehi\web\Dispatcher;
use feehi\web\ErrorHandler;
use yii\base\ExitException;
use yii\helpers\ArrayHelper;
Expand Down Expand Up @@ -112,8 +117,23 @@ public function actionStart()

$config['components']['errorHandler'] = isset($config['components']['errorHandler']) ? array_merge($config['components']['errorHandler'], ["class" => ErrorHandler::className()]) : ["class" => ErrorHandler::className()];

if( isset($config['components']['log']) ){
$config['components']['log'] = array_merge($config['components']['log'], ["class" => Dispatcher::className(), 'logger' => Logger::className()]);
}

if( isset($config['modules']['debug']) ){
$config['modules']['debug'] = array_merge($config['modules']['debug'], [
"class" => Module::className(),
'panels' => [
'profiling' => ['class' => ProfilingPanel::className()],
'timeline' => ['class' => TimelinePanel::className()],
]
]);
}

try {
$application = new Application($config);
yii::$app->getLog()->yiiBeginAt = microtime(true);
yii::$app->setAliases($aliases);
try {
$application->state = Application::STATE_BEFORE_REQUEST;
Expand Down
36 changes: 36 additions & 0 deletions src/debug/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Author: lf
* Blog: https://blog.feehi.com
* Email: job@feehi.com
* Created at: 2017-12-23 12:40
*/

namespace feehi\debug;


use yii;
use yii\helpers\Url;

class Module extends \yii\debug\Module
{
public function init()
{
parent::init();
$this->setViewPath('@vendor/yiisoft/yii2-debug/views');
}

public function setDebugHeaders($event)
{
if (!$this->checkAccess()) {
return;
}
$url = Url::toRoute(['/' . $this->id . '/default/view',
'tag' => $this->logTarget->tag,
]);
$event->sender->getHeaders()
->set('X-Debug-Tag', $this->logTarget->tag)
->set('X-Debug-Duration', number_format((microtime(true) - yii::$app->getLog()->yiiBeginAt) * 1000 + 1))
->set('X-Debug-Link', $url);
}
}
25 changes: 25 additions & 0 deletions src/debug/panels/ProfilingPanel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Author: lf
* Blog: https://blog.feehi.com
* Email: job@feehi.com
* Created at: 2017-12-23 12:51
*/
namespace feehi\debug\panels;

use yii;
use yii\log\Logger;

class ProfilingPanel extends \yii\debug\panels\ProfilingPanel
{
public function save()
{
$target = $this->module->logTarget;
$messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE);
return [
'memory' => memory_get_peak_usage(),
'time' => microtime(true) - yii::$app->getLog()->yiiBeginAt,
'messages' => $messages,
];
}
}
24 changes: 24 additions & 0 deletions src/debug/panels/TimelinePanel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Author: lf
* Blog: https://blog.feehi.com
* Email: job@feehi.com
* Created at: 2017-12-23 12:53
*/

namespace feehi\debug\panels;


use yii;

class TimelinePanel extends \yii\debug\panels\TimelinePanel
{
public function save()
{
return [
'start' => yii::$app->getLog()->yiiBeginAt,
'end' => microtime(true),
'memory' => memory_get_peak_usage(),
];
}
}
3 changes: 1 addition & 2 deletions src/swoole/SwooleServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use feehi\web\Session;
use swoole_http_server;

class SwooleServer extends \yii\base\BaseObject
class SwooleServer
{
public $swoole;

Expand All @@ -29,7 +29,6 @@ public function __construct($host, $port, $mode, $socketType, $swooleConfig=[],
$this->swoole->set($swooleConfig);
$this->swoole->on('request', [$this, 'onRequest']);
$this->swoole->on('WorkerStart', [$this, 'onWorkerStart']);
parent::__construct();
}

public function run()
Expand Down
19 changes: 19 additions & 0 deletions src/web/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Author: lf
* Blog: https://blog.feehi.com
* Email: job@feehi.com
* Created at: 2017-12-23 12:24
*/

namespace feehi\web;

class Dispatcher extends \yii\log\Dispatcher
{
public $yiiBeginAt;

public function getElapsedTime()
{
return microtime(true) - $this->yiiBeginAt;
}
}
17 changes: 17 additions & 0 deletions src/web/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Author: lf
* Blog: https://blog.feehi.com
* Email: job@feehi.com
* Created at: 2017-12-23 13:48
*/

namespace feehi\web;


class Logger extends \yii\log\Logger
{
public function init()
{
}
}
7 changes: 6 additions & 1 deletion src/web/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,12 @@ public function redirect($url, $statusCode = 302, $checkAjax = true)
}
$url = Url::to($url);
if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) {
$url = ( yii::$app->getRequest()->getIsSecureConnection() ? "https://" : "http://" ) . Yii::$app->getRequest()->getHostInfo() . $url;
$hostInfo = Yii::$app->getRequest()->getHostInfo();
if( strpos($hostInfo, 'http://') === 0 || strpos($hostInfo, 'https://') === 0 ){
$url = $hostInfo . $url;
}else{
$url = ( yii::$app->getRequest()->getIsSecureConnection() ? "https://" : "http://" ) . $hostInfo . $url;
}
}

if ($checkAjax) {
Expand Down

0 comments on commit e361a04

Please sign in to comment.