1 | <?php |
||
20 | class ImpersonateServiceProvider extends \Illuminate\Support\ServiceProvider |
||
21 | { |
||
22 | /** @var string $configName */ |
||
23 | protected $configName = 'laravel-impersonate'; |
||
24 | |||
25 | /** |
||
26 | * Register the service provider. |
||
27 | * |
||
28 | * @return void |
||
29 | */ |
||
30 | public function register() |
||
31 | { |
||
32 | $this->mergeConfig(); |
||
33 | |||
34 | $this->app->bind(ImpersonateManager::class, ImpersonateManager::class); |
||
35 | |||
36 | $this->app->singleton(ImpersonateManager::class, function ($app) { |
||
37 | return new ImpersonateManager($app); |
||
38 | }); |
||
39 | |||
40 | $this->app->alias(ImpersonateManager::class, 'impersonate'); |
||
41 | |||
42 | $this->registerRoutesMacro(); |
||
43 | $this->registerBladeDirectives(); |
||
44 | $this->registerMiddleware(); |
||
45 | $this->registerAuthDriver(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Bootstrap the application events. |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | public function boot() |
||
54 | { |
||
55 | $this->publishConfig(); |
||
56 | |||
57 | // We want to remove data from storage on real login and logout |
||
58 | Event::listen(Login::class, function ($event) { |
||
|
|||
59 | app('impersonate')->clear(); |
||
60 | }); |
||
61 | Event::listen(Logout::class, function ($event) { |
||
62 | app('impersonate')->clear(); |
||
63 | }); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Register plugin blade directives. |
||
68 | * |
||
69 | * @param void |
||
70 | * @return void |
||
71 | */ |
||
72 | protected function registerBladeDirectives() |
||
73 | { |
||
74 | $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
||
75 | $bladeCompiler->directive('impersonating', function ($guard = null) { |
||
76 | return "<?php if (is_impersonating({$guard})) : ?>"; |
||
77 | }); |
||
78 | |||
79 | $bladeCompiler->directive('endImpersonating', function () { |
||
80 | return '<?php endif; ?>'; |
||
81 | }); |
||
82 | |||
83 | $bladeCompiler->directive('canImpersonate', function ($guard = null) { |
||
84 | return "<?php if (can_impersonate({$guard})) : ?>"; |
||
85 | }); |
||
86 | |||
87 | $bladeCompiler->directive('endCanImpersonate', function () { |
||
88 | return '<?php endif; ?>'; |
||
89 | }); |
||
90 | |||
91 | $bladeCompiler->directive('canBeImpersonated', function ($expression) { |
||
92 | $args = preg_split("/,(\s+)?/", $expression); |
||
93 | $guard = $args[1] ?? null; |
||
94 | |||
95 | return "<?php if (can_be_impersonated({$args[0]}, {$guard})) : ?>"; |
||
96 | }); |
||
97 | |||
98 | $bladeCompiler->directive('endCanBeImpersonated', function () { |
||
99 | return '<?php endif; ?>'; |
||
100 | }); |
||
101 | }); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Register routes macro. |
||
106 | * |
||
107 | * @param void |
||
108 | * @return void |
||
109 | */ |
||
110 | protected function registerRoutesMacro() |
||
121 | |||
122 | /** |
||
123 | * @param void |
||
124 | * @return void |
||
125 | */ |
||
126 | protected function registerAuthDriver() |
||
151 | |||
152 | /** |
||
153 | * Register plugin middleware. |
||
154 | * |
||
155 | * @param void |
||
156 | * @return void |
||
157 | */ |
||
158 | public function registerMiddleware() |
||
162 | |||
163 | /** |
||
164 | * Merge config file. |
||
165 | * |
||
166 | * @param void |
||
167 | * @return void |
||
168 | */ |
||
169 | protected function mergeConfig() |
||
175 | |||
176 | /** |
||
177 | * Publish config file. |
||
178 | * |
||
179 | * @param void |
||
180 | * @return void |
||
181 | */ |
||
182 | protected function publishConfig() |
||
188 | } |
||
189 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.