|
1
|
|
|
<?php namespace Arcanedev\NoCaptcha; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Support\PackageServiceProvider as ServiceProvider; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class NoCaptchaServiceProvider |
|
7
|
|
|
* |
|
8
|
|
|
* @package Arcanedev\NoCaptcha |
|
9
|
|
|
* @author ARCANEDEV <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class NoCaptchaServiceProvider extends ServiceProvider |
|
12
|
|
|
{ |
|
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
14
|
|
|
| Properties |
|
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
16
|
|
|
*/ |
|
17
|
|
|
/** |
|
18
|
|
|
* Package name. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $package = 'no-captcha'; |
|
23
|
|
|
|
|
24
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
25
|
|
|
| Main Functions |
|
26
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
27
|
|
|
*/ |
|
28
|
|
|
/** |
|
29
|
|
|
* Register the service provider. |
|
30
|
|
|
*/ |
|
31
|
21 |
|
public function register() |
|
32
|
|
|
{ |
|
33
|
21 |
|
parent::register(); |
|
34
|
|
|
|
|
35
|
21 |
|
$this->registerConfig(); |
|
36
|
21 |
|
$this->registerNoCaptcha(); |
|
37
|
21 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Bootstrap the application events. |
|
41
|
|
|
*/ |
|
42
|
21 |
|
public function boot() |
|
43
|
|
|
{ |
|
44
|
21 |
|
parent::boot(); |
|
45
|
|
|
|
|
46
|
21 |
|
$this->publishConfig(); |
|
47
|
21 |
|
$this->registerValidatorRules($this->app); |
|
48
|
21 |
|
$this->registerFormMacros($this->app); |
|
49
|
21 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the services provided by the provider. |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function provides() |
|
57
|
|
|
{ |
|
58
|
|
|
return [ |
|
59
|
3 |
|
Contracts\NoCaptcha::class, |
|
60
|
1 |
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
64
|
|
|
| Other Functions |
|
65
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
66
|
|
|
*/ |
|
67
|
|
|
/** |
|
68
|
|
|
* Register NoCaptcha service. |
|
69
|
|
|
*/ |
|
70
|
21 |
|
private function registerNoCaptcha() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->singleton(Contracts\NoCaptcha::class, function($app) { |
|
73
|
|
|
/** @var \Illuminate\Contracts\Config\Repository $config */ |
|
74
|
12 |
|
$config = $app['config']; |
|
75
|
|
|
|
|
76
|
12 |
|
return new NoCaptcha( |
|
77
|
12 |
|
$config->get('no-captcha.secret'), |
|
78
|
12 |
|
$config->get('no-captcha.sitekey'), |
|
79
|
12 |
|
$config->get('no-captcha.lang'), |
|
80
|
12 |
|
$config->get('no-captcha.attributes', []) |
|
81
|
4 |
|
); |
|
82
|
21 |
|
}); |
|
83
|
21 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Register Validator rules. |
|
87
|
|
|
* |
|
88
|
|
|
* @param \Illuminate\Foundation\Application $app |
|
89
|
|
|
*/ |
|
90
|
21 |
|
private function registerValidatorRules($app) |
|
91
|
|
|
{ |
|
92
|
|
|
$app['validator']->extend('captcha', function($attribute, $value) use ($app) { |
|
93
|
9 |
|
unset($attribute); |
|
94
|
9 |
|
$ip = $app['request']->getClientIp(); |
|
95
|
|
|
|
|
96
|
9 |
|
return $app[Contracts\NoCaptcha::class]->verify($value, $ip); |
|
97
|
21 |
|
}); |
|
98
|
21 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Register Form Macros. |
|
102
|
|
|
* |
|
103
|
|
|
* @param \Illuminate\Foundation\Application $app |
|
104
|
|
|
*/ |
|
105
|
21 |
|
private function registerFormMacros($app) |
|
106
|
|
|
{ |
|
107
|
21 |
|
if ($app->bound('form')) { |
|
108
|
|
|
$app['form']->macro('captcha', function($name = null, array $attributes = []) use ($app) { |
|
109
|
|
|
return $app[Contracts\NoCaptcha::class]->display($name, $attributes); |
|
110
|
|
|
}); |
|
111
|
|
|
} |
|
112
|
21 |
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|