1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Providers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler; |
6
|
|
|
use Illuminate\Foundation\AliasLoader; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use Laracasts\Utilities\JavaScript\JavaScriptServiceProvider; |
9
|
|
|
use Sco\ActionLog\LaravelServiceProvider; |
10
|
|
|
use Sco\Admin\Admin; |
11
|
|
|
use Sco\Admin\Exceptions\Handler; |
12
|
|
|
use Sco\Admin\Facades\AdminFacade; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class AdminServiceProvider extends ServiceProvider |
18
|
|
|
{ |
19
|
|
|
protected $commands = [ |
20
|
|
|
\Sco\Admin\Console\InstallCommand::class, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
protected $middlewares = [ |
24
|
|
|
'admin.guest' => \Sco\Admin\Http\Middleware\RedirectIfAuthenticated::class, |
25
|
|
|
'admin.phptojs' => \Sco\Admin\Http\Middleware\PHPVarToJavaScript::class, |
26
|
|
|
'admin.can.route' => \Sco\Admin\Http\Middleware\RouteAuthorize::class, |
27
|
|
|
'admin.can.model' => \Sco\Admin\Http\Middleware\ModelAuthorize::class, |
28
|
|
|
'admin.role' => \Sco\Admin\Http\Middleware\EntrustRole::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
protected $providers = [ |
32
|
|
|
LaravelServiceProvider::class, |
33
|
|
|
JavaScriptServiceProvider::class, |
34
|
|
|
PublishServiceProvider::class, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
protected $aliases = [ |
38
|
|
|
'Admin' => AdminFacade::class, |
39
|
|
|
//'AdminConfig' => ConfigFacade::class, |
|
|
|
|
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
public function getBasePath() |
43
|
|
|
{ |
44
|
|
|
return dirname(dirname(__DIR__)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Bootstrap the application services. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function boot() |
53
|
|
|
{ |
54
|
|
|
// 路由文件 |
55
|
|
|
$this->loadRoutes(); |
56
|
|
|
|
57
|
|
|
// 后台模板目录 |
58
|
|
|
$this->loadViewsFrom( |
59
|
|
|
$this->getBasePath() . '/resources/views', |
60
|
|
|
'admin' |
61
|
|
|
); |
62
|
|
|
// 后台语言包目录 |
63
|
|
|
$this->loadTranslationsFrom( |
64
|
|
|
$this->getBasePath() . '/resources/lang', |
65
|
|
|
'admin' |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
if ($this->app->runningInConsole()) { |
69
|
|
|
$this->loadMigrationsFrom($this->getBasePath() . '/database/migrations'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function loadRoutes() |
74
|
|
|
{ |
75
|
|
|
$routesFile = $this->getBasePath() . '/routes/admin.php'; |
76
|
|
|
if (file_exists(base_path('routes/admin.php'))) { |
77
|
|
|
$routesFile = base_path('routes/admin.php'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->loadRoutesFrom($routesFile); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Register the application services. |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function register() |
89
|
|
|
{ |
90
|
|
|
$this->mergeConfigFrom( |
91
|
|
|
$this->getBasePath() . '/config/admin.php', |
92
|
|
|
'admin' |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$this->registerExceptionHandler(); |
96
|
|
|
$this->registerAdmin(); |
97
|
|
|
$this->registerAliases(); |
98
|
|
|
$this->registerMiddleware(); |
99
|
|
|
$this->bindRouteModel(); |
100
|
|
|
|
101
|
|
|
$this->commands($this->commands); |
102
|
|
|
|
103
|
|
|
$this->registerProviders(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function registerMiddleware() |
107
|
|
|
{ |
108
|
|
|
foreach ($this->middlewares as $key => $middleware) { |
109
|
|
|
$this->app['router']->aliasMiddleware($key, $middleware); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function registerAliases() |
114
|
|
|
{ |
115
|
|
|
AliasLoader::getInstance($this->aliases); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function registerProviders() |
119
|
|
|
{ |
120
|
|
|
foreach ($this->providers as $provider) { |
121
|
|
|
$this->app->register($provider); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function registerExceptionHandler() |
126
|
|
|
{ |
127
|
|
|
$exceptHandler = app(ExceptionHandler::class); |
128
|
|
|
$this->app->singleton( |
129
|
|
|
ExceptionHandler::class, |
130
|
|
|
function () use ($exceptHandler) { |
131
|
|
|
return new Handler($exceptHandler); |
132
|
|
|
} |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function registerAdmin() |
137
|
|
|
{ |
138
|
|
|
$this->app->instance('admin.instance', new Admin($this->app)); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function bindRouteModel() |
142
|
|
|
{ |
143
|
|
|
$this->app['router']->bind('model', function ($value) { |
144
|
|
|
return $this->app['admin.instance']->getConfig($value); |
145
|
|
|
}); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.