1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alkhwlani\XssMiddleware; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Http\Kernel; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\ServiceProvider as ServiceProviderAlias; |
8
|
|
|
|
9
|
|
|
class ServiceProvider extends ServiceProviderAlias |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Bootstrap any application services. |
13
|
|
|
* |
14
|
|
|
* @return void |
15
|
|
|
*/ |
16
|
3 |
|
public function boot() |
17
|
|
|
{ |
18
|
3 |
|
$this->registerMiddleware(); |
19
|
3 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* auto append middleware to router. |
23
|
|
|
*/ |
24
|
3 |
|
protected function registerMiddleware() |
25
|
|
|
{ |
26
|
3 |
|
$middlewareClass = $this->app['config']->get('xss-middleware.middleware', XSSFilterMiddleware::class); |
27
|
|
|
|
28
|
3 |
|
$this->app['router']->aliasMiddleware('xss-filter', $middlewareClass); |
29
|
|
|
|
30
|
3 |
|
$registerType = $this->app['config']->get('xss-middleware.auto_register_middleware', false); |
31
|
|
|
|
32
|
3 |
|
if ($registerType === false) { |
33
|
2 |
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
if ($registerType === true) { // Register middleware as global Middleware |
37
|
1 |
|
$this->app->make(Kernel::class)->pushMiddleware($middlewareClass); |
38
|
|
|
|
39
|
1 |
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Register Middleware for route group |
43
|
|
|
foreach (Arr::wrap($registerType) as $group) { |
44
|
|
|
$this->app['router']->pushMiddlewareToGroup($group, $middlewareClass); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Register any application services. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
3 |
|
public function register() |
54
|
|
|
{ |
55
|
3 |
|
$this->registerConfig(); |
56
|
3 |
|
} |
57
|
|
|
|
58
|
3 |
|
public function registerConfig() |
59
|
|
|
{ |
60
|
3 |
|
if ($this->app->runningInConsole()) { |
61
|
3 |
|
$this->publishes([ |
62
|
3 |
|
__DIR__.'/../config/xss-middleware.php' => config_path('xss-middleware.php'), |
63
|
3 |
|
], 'config'); |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
$this->mergeConfigFrom(__DIR__.'/../config/xss-middleware.php', 'xss-middleware'); |
67
|
3 |
|
} |
68
|
|
|
} |
69
|
|
|
|