Skip to content

Commit

Permalink
翻译队列化
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Sep 20, 2019
1 parent d7f9c70 commit 22dc097
Show file tree
Hide file tree
Showing 14 changed files with 1,277 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/Handlers/SlugTranslateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function translate($text)

// 尝试获取获取翻译结果
if (isset($result['trans_result'][0]['dst'])) {
return str_slug($result['trans_result'][0]['dst']);
return \Str::slug($result['trans_result'][0]['dst']);
} else {
// 如果百度翻译没有结果,使用拼音作为后备计划。
return $this->pinyin($text);
Expand All @@ -70,6 +70,6 @@ public function translate($text)

public function pinyin($text)
{
return str_slug(app(Pinyin::class)->permalink($text));
return \Str::slug(app(Pinyin::class)->permalink($text));
}
}
34 changes: 34 additions & 0 deletions app/Jobs/TranslateSlug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

use App\Models\Topic;
use App\Handlers\SlugTranslateHandler;

class TranslateSlug implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $topic;

public function __construct(Topic $topic)
{
// 队列任务构造器中接收了 Eloquent 模型,将会只序列化模型的 ID
$this->topic = $topic;
}

public function handle()
{
// 请求百度 API 接口进行翻译
$slug = app(SlugTranslateHandler::class)->translate($this->topic->title);

// 为了避免模型监控器死循环调用,我们使用 DB 类直接对数据库进行操作
\DB::table('topics')->where('id', $this->topic->id)->update(['slug' => $slug]);
}
}
9 changes: 7 additions & 2 deletions app/Observers/TopicObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Observers;

use App\Models\Topic;
use App\Handlers\SlugTranslateHandler;
use App\Jobs\TranslateSlug;

// creating, created, updating, updated, saving,
// saved, deleting, deleted, restoring, restored
Expand All @@ -17,10 +17,15 @@ public function saving(Topic $topic)

// 生成话题摘录
$topic->excerpt = make_excerpt($topic->body);
}

public function saved(Topic $topic)
{
// 如 slug 字段无内容,即使用翻译器对 title 进行翻译
if ( ! $topic->slug) {
$topic->slug = app(SlugTranslateHandler::class)->translate($topic->title);

// 推送任务到队列
dispatch(new TranslateSlug($topic));
}
}
}
42 changes: 42 additions & 0 deletions app/Providers/HorizonServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Providers;

use Laravel\Horizon\Horizon;
use Illuminate\Support\Facades\Gate;
use Laravel\Horizon\HorizonApplicationServiceProvider;

class HorizonServiceProvider extends HorizonApplicationServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
parent::boot();

// Horizon::routeSmsNotificationsTo('15556667777');
// Horizon::routeMailNotificationsTo('[email protected]');
// Horizon::routeSlackNotificationsTo('slack-webhook-url', '#channel');

// Horizon::night();
}

/**
* Register the Horizon gate.
*
* This gate determines who can access Horizon in non-local environments.
*
* @return void
*/
protected function gate()
{
Gate::define('viewHorizon', function ($user) {
return in_array($user->email, [
//
]);
});
}
}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"guzzlehttp/guzzle": "~6.3",
"intervention/image": "^2.5",
"laravel/framework": "^6.0",
"laravel/horizon": "~3.1",
"laravel/tinker": "^1.0",
"mews/captcha": "~3.0",
"mews/purifier": "~3.0",
"overtrue/laravel-lang": "~3.0",
"overtrue/pinyin": "~4.0",
"predis/predis": "~1.1",
"summerblue/laravel-active": "6.*"
},
"require-dev": {
Expand Down
196 changes: 195 additions & 1 deletion composer.lock

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

Loading

0 comments on commit 22dc097

Please sign in to comment.