Gowhich

Durban's Blog

P -> 按照CPU排序

M -> 按照Memory排序

z -> 很酷的视觉颜色

x -> 当前正在排序的高亮列

laravel版本:5.2.*

一、创建监听器

1
php artisan make:listener QueryListener --event=Illuminate\\Database\\Events\\QueryExecuted

or

1
sudo /usr/local/bin/php artisan make:listener QueryListener --event=Illuminate\\Database\\Events\\QueryExecuted

会自动生成文件 app/Listeners/QueryListener.php

二、注册事件

打开 app/Providers/EventServiceProvider.php,在 $listen 中添加 Illuminate\Database\Events\QueryExecuted 事件的监听器为 QueryListener

1
2
3
4
5
protected $listen = [  
'Illuminate\Database\Events\QueryExecuted' => [
'App\Listeners\QueryListener',
],
];

最终代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
],
'Illuminate\Database\Events\QueryExecuted' => [
'App\Listeners\QueryListener',
],
];
/**
* Register any other events for your application.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
//
}
}

三、添加逻辑

打开 app/Listeners/QueryListener.php

光有一个空的监听器是不够的,我们需要自己实现如何把 $sql 记录到日志中。为此,对 QueryListener 进行改造,完善其 handle 方法如下:

1
2
3
$sql = str_replace("?", "'%s'", $event->sql);
$log = vsprintf($sql, $event->bindings);
Log::info($log);

最终代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace App\Listeners;
use Log;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class QueryListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param QueryExecuted $event
* @return void
*/
public function handle(QueryExecuted $event)
{
$sql = str_replace("?", "'%s'", $event->sql);
$log = vsprintf($sql, $event->bindings);
Log::info($log);
}
}

之前有写过类似的一篇文章,有位同学突然找来解惑,发现自己采用了另外的一个方法,这里也分享下,希望对使用reactjs的同学有帮助。

逻辑思路是这样子的,在componentDidMount中实现更新dom的操作,异步加载需要的资源文件,然后在加载完后实现qiniu的初始化操作。这里就不需要在webpack或者其他打包工具中去引入qiniu的包文件,导致打完包的文件过大了。

我这里使用了nodejs的库scriptjs,

1
const $S = require('scriptjs');

可以实现异步的加载文件,当然你也可以使用你认为更好的,当然也别忘记告诉我下。以下为代码实现部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
async componentDidMount() {

let uploadToken = await this.getUploadToken();

$S([
'https://dn-kdjz.qbox.me/js/plupload/2.1.1/plupload.full.min.js',
'https://dn-kdjz.qbox.me/js/qiniu-js-sdk/1.0.17.2/qiniu.min.js'
], 'uploadBundle');

$S.ready('uploadBundle', () => {

// 证件合影
let options1 = {
runtimes: 'html5,flash,html4',
browse_button: 'photoId',
uptoken: uploadToken,
get_new_uptoken: false,
domain: 'https://xxxx.xxxxxx', // bucket域名,下载资源时用到,必需
container: 'photoIdContainer', // 上传区域DOM ID,默认是browser_button的父元素
max_file_size: '100mb', // 最大文件体积限制
flash_swf_url: '/js/plupload/2.2.1/Moxie.swf', //引入flash,相对路径
max_retries: 3, // 上传失败最大重试次数
dragdrop: true, // 开启可拖曳上传
drop_element: 'photoIdContainer', // 拖曳上传区域元素的ID,拖曳文件或文件夹后可触发上传
chunk_size: '4mb', // 分块上传时,每块的体积
auto_start: true, // 选择文件后自动上传,若关闭需要自己绑定事件触发上传
init: {
'FilesAdded': (up, files) => {
plupload.each(files, function(file) {
// 文件添加进队列后,处理相关的事情
});
},
'BeforeUpload': (up, file) => {
// 每个文件上传前,处理相关的事情
},
'UploadProgress': (up, file) => {
// 每个文件上传时,处理相关的事情
},
'FileUploaded': async(up, file, info) => {
// 查看简单反馈
let domain = up.getOption('domain');
let res = JSON.parse(info);
let sourceLink = await this.getDownloadUrl(res.key);
this.setState({
photoIdKey: res.key,
photoId: sourceLink
})
},
'Error': (up, err, errTip) => {
//上传出错时,处理相关的事情
console.log(err);
},
'UploadComplete': () => {
//队列文件处理完毕后,处理相关的事情
console.log('上传完成');
},
'Key': (up, file) => {
let timestamp = parseInt((new Date().valueOf() / 1000));
// 若想在前端对每个文件的key进行个性化处理,可以配置该函数
// 该配置必须要在unique_names: false,save_key: false时才生效
let key = `idcard/${timestamp}_${file.name}`;
return key
}
}
};
// 第一个按钮
const uploader1 = Qiniu.uploader(options1);
})
}

这里有个getUploadToken方法,这个方法是根据官方文档的策略实现了一个获取上传token的方法,此方法是通过访问服务端的接口来获取token。具体实现过程可以参考官方,不明白的也可以加群讨论。

PHP7 的新特性大概浏览下,还是能在工作的效率上有很大益处的。

1,性能提升

这个我就不做测试了,哈哈

2,类型声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Student
{
public function __construct()
{
$this->name = 'durban';
}
}

$student = new Student();

function enroll(Student $student, array $classes)
{
foreach ($classes as $class) {
echo "Enrolling " . $student->name . " in " . $class . "\n";
}
}

// enroll("name", ["class 1", "class 2"]);// Fatal error: Uncaught TypeError: Argument 1 passed to enroll() must be an instance of Student, string given
// enroll($student, "class"); // Fatal error: Uncaught TypeError: Argument 2 passed to enroll() must be of the type array, string given
enroll($student, array("class 1", "class 2"));

function stringTest(string $string)
{
echo $string . "\n";
}
stringTest("a string");

3,可以声明严格类型校验模式 , 此声明必须第一个声明

1
declare (strict_types = 1);

4, 标量类型提示

1
2
3
4
5
6
7
function getTotal(float $a, float $b)
{
return $a + $b;
}
// getTotal('a', 2); //Argument 1 passed to getTotal() must be of the type float, string given,
$total = getTotal(3, 2);
echo $total . "\n";

5, 返回类型声明

1
2
3
4
5
6
7
function getSum(float $a, float $b): int
{
// return $a + $b; // Fatal error: Uncaught TypeError: Return value of getTotal() must be of the type integer, float returned
return (int) ($a + $b); // truncate float like non-strict
}
$sum = getSum(3, 6);
echo $sum . "\n";

6, 错误处理

新的继承如下

|- Exception implements Throwable

|- …

|- Error implements Throwable

|- TypeError extends Error

|- ParseError extends Error

|- ArithmeticError extends Error

  |- DivisionByZeroError extends ArithmeticError

|- AssertionError extends Error

1
2
3
4
5
6
7
try {
// Code that may throw an Exception or Error.
} catch (Throwable $t) {
// Executed only in PHP 7, will not match in PHP 5
} catch (Exception $e) {
// Executed only in PHP 5, will not be reached in PHP 7
}

7, Null Coalesce Operator

1
$name = $firstName ??  "Guest";

等同于

1
2
3
4
5
if (!empty($firstName)) {
$name = $firstName;
} else {
$name = "Guest";
}

还可以像下面这样使用

1
$name = $firstName ?? $username ?? $placeholder ?? “Guest”;

8, Spaceship Operator

1
$compare = 2 <=> 1

等同于下面

1
2
3
2 < 1? return -1
2 = 1? return 0
2 > 1? return 1

9,Easy User-land CSPRNG: random_int and random_bytes.

1
2
3
4
5
$int = random_int(1, 2);
var_dump($int);

$bytes = random_bytes(5);
var_dump(bin2hex($bytes));

举例子如下:

1
2
3
4
5
6
7
8
9
$models = static::where('user_id', $userId)->leftJoin('product_gift', function ($join) {
$join->on('product_gift_exchange_order.prd_id', '=', 'product_gift.prd_id');
$join->on('product_gift_exchange_order.gift_code', '=', 'product_gift.gift_code');
})->orderBy('product_gift_exchange_order.autokid', 'DESC')->get([
'product_gift_exchange_order.autokid',
'product_gift_exchange_order.ctime',
'product_gift_exchange_order.exchange_amount',
'product_gift.gift_name',
]);

记录分享

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{
"always_show_minimap_viewport": true,
"bold_folder_labels": true,
"color_scheme": "Packages/Material Theme/schemes/Material-Theme.tmTheme",
"folder_exclude_patterns":
[
"node_modules"
],
"font_face": "Source Code Pro For Powerline",
"font_options":
[
"subpixel_antialias"
],
"font_size": 16,
"ignored_packages":
[
"Vintage"
],
"indent_guide_options":
[
"draw_normal",
"draw_active"
],
"line_padding_bottom": 0,
"line_padding_top": 0,
"material_theme_accent_scrollbars": true,
"material_theme_arrow_folders": true,
"material_theme_big_fileicons": true,
"material_theme_bold_tab": true,
"material_theme_bright_scrollbars": false,
"material_theme_bullet_tree_indicator": true,
"material_theme_compact_panel": false,
"material_theme_compact_sidebar": true,
"material_theme_contrast_mode": false,
"material_theme_disable_fileicons": true,
"material_theme_disable_folder_animation": true,
"material_theme_disable_tree_indicator": true,
"material_theme_panel_separator": true,
"material_theme_small_statusbar": true,
"material_theme_small_tab": true,
"material_theme_tabs_autowidth": true,
"material_theme_tabs_separator": true,
"material_theme_tree_headings": true,
"overlay_scroll_bars": "enabled",
"theme": "Material-Theme.sublime-theme"
}

配置分享,好像很难找到标准的

1
2
3
4
5
{
"psr2": true,
"enable_auto_align": true,
"php_bin":"/usr/local/opt/php71/bin/php",
}

就这么简单。但是还有个保存自动格式化的,这个在默认的配置里面加就好了。

Sublime text 3 配置分享

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{
"always_show_minimap_viewport": true,
"font_face":"Source Code Pro for Powerline",
"font_size":14,
"bold_folder_labels": true,
"color_scheme": "Packages/Material Theme/schemes/Material-Theme.tmTheme",
"ignored_packages":
[
"Vintage"
],
"indent_guide_options":
[
"draw_normal",
"draw_active"
],
"translate_tabs_to_spaces": true,
"material_theme_accent_acid-lime": true,
"material_theme_accent_blue": true,
"material_theme_accent_brba": true,
"material_theme_accent_bright-teal": true,
"material_theme_accent_cyan": true,
"material_theme_accent_graphite": true,
"material_theme_accent_indigo": true,
"material_theme_accent_lime": true,
"material_theme_accent_orange": true,
"material_theme_accent_pink": true,
"material_theme_accent_purple": true,
"material_theme_accent_red": true,
"material_theme_accent_scrollbars": true,
"material_theme_accent_sky": true,
"material_theme_accent_tomato": true,
"material_theme_accent_yellow": true,
"material_theme_arrow_folders": true,
"material_theme_big_fileicons": true,
"material_theme_bold_tab": true,
"material_theme_bright_scrollbars": true,
"material_theme_bullet_tree_indicator": true,
"material_theme_compact_panel": true,
"material_theme_compact_sidebar": true,
"material_theme_contrast_mode": true,
"material_theme_disable_fileicons": true,
"material_theme_disable_folder_animation": true,
"material_theme_disable_tree_indicator": true,
"material_theme_panel_separator": true,
"material_theme_small_statusbar": true,
"material_theme_small_tab": true,
"material_theme_tabs_autowidth": true,
"material_theme_tabs_separator": true,
"material_theme_tree_headings": true,
"overlay_scroll_bars": "enabled",
"theme": "Material-Theme.sublime-theme"
}

需要插件或者包

  • Material Theme

  • Material Theme - Appbar

  • powerline

代码记录如下

1
2
3
4
5
6
7
8
9
10
Host github.com
User git
Hostname ssh.github.com
Port 443
ProxyCommand connect -S 127.0.0.1:1180 %h %p
Host bitbucket.org
User git
Hostname altssh.bitbucket.org
Port 443
ProxyCommand connect -S 127.0.0.1:1180 %h %p

操作方式是这里用到了connect 这个功能,所以为了为了使用这个功能,前提是安装connect

1
brew install connect

需要使用github,但是国内访问很慢,往往会发生connection refused的事情发生,那就自己去弄个vpn吧。前提不要做扰乱国家安稳的事情

下面记录下git的配置方法

1
2
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'
0%