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