forked from bilfeldt/laravel-route-statistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaravelRouteStatisticsServiceProvider.php
105 lines (90 loc) · 3.14 KB
/
LaravelRouteStatisticsServiceProvider.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Bilfeldt\LaravelRouteStatistics;
use Bilfeldt\LaravelRouteStatistics\Commands\LaravelRouteStatisticsCommand;
use Bilfeldt\LaravelRouteStatistics\Commands\LaravelRouteTruncateCommand;
use Bilfeldt\LaravelRouteStatistics\Commands\LaravelRouteUnusedCommand;
use Bilfeldt\LaravelRouteStatistics\Http\Middleware\RouteStatisticsMiddleware;
use Bilfeldt\RequestLogger\RequestLoggerFacade;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\ServiceProvider;
class LaravelRouteStatisticsServiceProvider extends ServiceProvider
{
/*
public function configurePackage(Package $package): void
{
//
// This class is a Package Service Provider
//
// More info: https://github.com/spatie/laravel-package-tools
//
$package
->name('laravel-route-statistics')
->hasConfigFile()
->hasMigration('create_route_statistics_table')
->hasCommand(LaravelRouteStatisticsCommand::class);
}
*/
public function register()
{
$this->mergeConfig();
}
public function boot()
{
$this->publishConfig();
$this->publishMigrations();
$this->bootMiddleware();
$this->bootMacros();
$this->bootCommands();
$this->bootLogger();
}
private function mergeConfig()
{
$this->mergeConfigFrom(__DIR__.'/../config/route-statistics.php', 'route-statistics');
}
private function publishConfig()
{
$this->publishes([
__DIR__.'/../config/route-statistics.php' => config_path('route-statistics.php'),
], 'config');
}
private function publishMigrations()
{
$this->publishes([
__DIR__.'/../database/migrations/create_route_statistics_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_route_statistics_table.php'),
__DIR__.'/../database/migrations/add_parameters_to_route_statistics_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_add_parameters_to_route_statistics_table.php'),
// you can add any number of migrations here
], 'migrations');
}
private function bootCommands()
{
if ($this->app->runningInConsole()) {
$this->commands([
LaravelRouteStatisticsCommand::class,
LaravelRouteTruncateCommand::class,
LaravelRouteUnusedCommand::class,
]);
}
}
private function bootMiddleware()
{
$router = $this->app->make(Router::class);
$router->aliasMiddleware(RouteStatisticsMiddleware::ALIAS, RouteStatisticsMiddleware::class);
}
private function bootMacros()
{
Request::macro('routeStatistics', function () {
if (config('route-statistics.enabled')) {
$this->enableLog('routestat');
}
return $this;
});
}
private function bootLogger()
{
RequestLoggerFacade::extend('routestat', function ($app) {
$model = config('route-statistics.model');
return new $model();
});
}
}