1 | <?php |
||
2 | /** |
||
3 | * Copyright (c) 2017 - present |
||
4 | * LaravelGoogleRecaptcha - ReCaptchaServiceProvider.php |
||
5 | * author: Roberto Belotti - [email protected] |
||
6 | * web : robertobelotti.com, github.com/biscolab |
||
7 | * Initial version created on: 12/9/2018 |
||
8 | * MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE |
||
9 | */ |
||
10 | |||
11 | namespace Biscolab\ReCaptcha; |
||
12 | |||
13 | use Illuminate\Support\Facades\Route; |
||
14 | use Illuminate\Support\Facades\Validator; |
||
15 | use Illuminate\Support\ServiceProvider; |
||
16 | |||
17 | /** |
||
18 | * Class ReCaptchaServiceProvider |
||
19 | * @package Biscolab\ReCaptcha |
||
20 | */ |
||
21 | class ReCaptchaServiceProvider extends ServiceProvider |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Indicates if loading of the provider is deferred. |
||
26 | * |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $defer = false; |
||
30 | |||
31 | /** |
||
32 | * |
||
33 | */ |
||
34 | 49 | public function boot() |
|
35 | { |
||
36 | |||
37 | 49 | $this->addValidationRule(); |
|
38 | 49 | $this->registerRoutes(); |
|
39 | 49 | $this->publishes([ |
|
40 | 49 | __DIR__ . '/../config/recaptcha.php' => config_path('recaptcha.php'), |
|
41 | 49 | ], 'config'); |
|
42 | |||
43 | 49 | } |
|
44 | |||
45 | /** |
||
46 | * Extends Validator to include a recaptcha type |
||
47 | */ |
||
48 | 49 | public function addValidationRule() |
|
49 | { |
||
50 | |||
51 | 49 | Validator::extendImplicit(recaptchaRuleName(), function ($attribute, $value) { |
|
52 | |||
53 | return app('recaptcha')->validate($value); |
||
54 | 49 | }, trans('validation.recaptcha')); |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
55 | 49 | } |
|
56 | |||
57 | /** |
||
58 | * Register the service provider. |
||
59 | * |
||
60 | * @return void |
||
61 | */ |
||
62 | 49 | public function register() |
|
63 | { |
||
64 | |||
65 | 49 | $this->mergeConfigFrom( |
|
66 | 49 | __DIR__ . '/../config/recaptcha.php', 'recaptcha' |
|
67 | ); |
||
68 | |||
69 | 49 | $this->registerReCaptchaBuilder(); |
|
70 | 49 | } |
|
71 | |||
72 | /** |
||
73 | * Get the services provided by the provider. |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | public function provides(): array |
||
78 | { |
||
79 | |||
80 | return ['recaptcha']; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return ReCaptchaServiceProvider |
||
85 | * |
||
86 | * @since v3.4.1 |
||
87 | */ |
||
88 | 49 | protected function registerRoutes(): ReCaptchaServiceProvider |
|
89 | { |
||
90 | |||
91 | 49 | Route::get( |
|
92 | 49 | config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate'), |
|
93 | 49 | ['uses' => 'Biscolab\ReCaptcha\Controllers\ReCaptchaController@validateV3'] |
|
94 | 49 | )->middleware('web'); |
|
95 | |||
96 | 49 | return $this; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Register the HTML builder instance. |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | 49 | protected function registerReCaptchaBuilder() |
|
105 | { |
||
106 | |||
107 | 49 | $this->app->singleton('recaptcha', function ($app) { |
|
108 | |||
109 | 29 | $recaptcha_class = ''; |
|
110 | |||
111 | 29 | switch (config('recaptcha.version')) { |
|
112 | 29 | case 'v3' : |
|
113 | 2 | $recaptcha_class = ReCaptchaBuilderV3::class; |
|
114 | 2 | break; |
|
115 | 27 | case 'v2' : |
|
116 | 21 | $recaptcha_class = ReCaptchaBuilderV2::class; |
|
117 | 21 | break; |
|
118 | 6 | case 'invisible': |
|
119 | 6 | $recaptcha_class = ReCaptchaBuilderInvisible::class; |
|
120 | 6 | break; |
|
121 | } |
||
122 | |||
123 | 29 | return new $recaptcha_class(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key')); |
|
124 | |||
125 | 49 | }); |
|
126 | 49 | } |
|
127 | |||
128 | } |
||
129 |