Skip to content

Commit 024354f

Browse files
author
qiang.sun
committed
编写高级Artisan命令
1 parent bf521b8 commit 024354f

File tree

15 files changed

+113732
-178
lines changed

15 files changed

+113732
-178
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class WelcomeMessage extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'welcome:message';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = '打印欢迎信息';
22+
23+
/**
24+
* Create a new command instance.
25+
*
26+
* @return void
27+
*/
28+
public function __construct()
29+
{
30+
parent::__construct();
31+
}
32+
33+
/**
34+
* Execute the console command.
35+
*
36+
* @return mixed
37+
*/
38+
public function handle()
39+
{
40+
$name = $this->ask('你叫什么名字');
41+
$city = $this->choice('你来自哪个城市', [
42+
'北京', '杭州', '深圳'
43+
], 0);
44+
$password = $this->secret('输入密码才能执行此命令');
45+
if ($password != '123') {
46+
$this->error('密码错误');
47+
exit(-1);
48+
}
49+
if ($this->confirm('确定要执行此命令吗?')) {
50+
$this->info('欢迎来自' . $city . '' . $name . '访问 Laravel 学院');
51+
} else {
52+
exit(0);
53+
}
54+
}
55+
}

app/Console/Kernel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Console;
44

5+
use App\Console\Commands\WelcomeMessage;
56
use Illuminate\Console\Scheduling\Schedule;
67
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
78

@@ -13,7 +14,7 @@ class Kernel extends ConsoleKernel
1314
* @var array
1415
*/
1516
protected $commands = [
16-
//
17+
WelcomeMessage::class
1718
];
1819

1920
/**

app/Providers/AppServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Services\SmsBillingNotifier;
1111
use App\Services\StripeBiller;
1212
use Illuminate\Support\ServiceProvider;
13+
use Laravel\Telescope\TelescopeServiceProvider;
1314

1415
class AppServiceProvider extends ServiceProvider
1516
{
@@ -31,6 +32,10 @@ public function boot()
3132
*/
3233
public function register()
3334
{
35+
if ($this->app->isLocal()) {
36+
$this->app->register(TelescopeServiceProvider::class);
37+
}
38+
3439
$this->app->bind(UserRepositoryInterface::class, function ($app) {
3540
return new UserRepository();
3641
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Laravel\Telescope\Telescope;
6+
use Illuminate\Support\Facades\Gate;
7+
use Laravel\Telescope\IncomingEntry;
8+
use Laravel\Telescope\TelescopeApplicationServiceProvider;
9+
10+
class TelescopeServiceProvider extends TelescopeApplicationServiceProvider
11+
{
12+
/**
13+
* Register any application services.
14+
*
15+
* @return void
16+
*/
17+
public function register()
18+
{
19+
// Telescope::night();
20+
21+
$this->hideSensitiveRequestDetails();
22+
23+
Telescope::filter(function (IncomingEntry $entry) {
24+
if ($this->app->isLocal()) {
25+
return true;
26+
}
27+
28+
return $entry->isReportableException() ||
29+
$entry->isFailedJob() ||
30+
$entry->isScheduledTask() ||
31+
$entry->hasMonitoredTag();
32+
});
33+
}
34+
35+
/**
36+
* Prevent sensitive request details from being logged by Telescope.
37+
*
38+
* @return void
39+
*/
40+
protected function hideSensitiveRequestDetails()
41+
{
42+
if ($this->app->isLocal()) {
43+
return;
44+
}
45+
46+
Telescope::hideRequestParameters(['_token']);
47+
48+
Telescope::hideRequestHeaders([
49+
'cookie',
50+
'x-csrf-token',
51+
'x-xsrf-token',
52+
]);
53+
}
54+
55+
/**
56+
* Register the Telescope gate.
57+
*
58+
* This gate determines who can access Telescope in non-local environments.
59+
*
60+
* @return void
61+
*/
62+
protected function gate()
63+
{
64+
Gate::define('viewTelescope', function ($user) {
65+
return in_array($user->email, [
66+
//
67+
]);
68+
});
69+
}
70+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"beyondcode/laravel-dump-server": "^1.0",
1515
"filp/whoops": "^2.0",
1616
"fzaninotto/faker": "^1.4",
17+
"laravel/telescope": "^1.0",
1718
"mockery/mockery": "^1.0",
1819
"nunomaduro/collision": "^2.0",
1920
"phpunit/phpunit": "^7.0"

0 commit comments

Comments
 (0)