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
|
|
|
* Vendor name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $vendor = 'arcanedev'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Package name. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $package = 'no-captcha'; |
30
|
|
|
|
31
|
|
|
/* ------------------------------------------------------------------------------------------------ |
32
|
|
|
| Getters & Setters |
33
|
|
|
| ------------------------------------------------------------------------------------------------ |
34
|
|
|
*/ |
35
|
|
|
/** |
36
|
|
|
* Get the base path of the package. |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
72 |
|
public function getBasePath() |
41
|
|
|
{ |
42
|
72 |
|
return dirname(__DIR__); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/* ------------------------------------------------------------------------------------------------ |
46
|
|
|
| Main Functions |
47
|
|
|
| ------------------------------------------------------------------------------------------------ |
48
|
|
|
*/ |
49
|
|
|
/** |
50
|
|
|
* Register the service provider. |
51
|
|
|
*/ |
52
|
72 |
|
public function register() |
53
|
|
|
{ |
54
|
72 |
|
$this->registerConfig(); |
55
|
72 |
|
$this->registerNoCaptcha(); |
56
|
72 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Bootstrap the application events. |
60
|
|
|
*/ |
61
|
72 |
|
public function boot() |
62
|
|
|
{ |
63
|
72 |
|
parent::boot(); |
64
|
|
|
|
65
|
72 |
|
$this->publishConfig(); |
|
|
|
|
66
|
72 |
|
$this->registerValidatorRules($this->app); |
67
|
72 |
|
$this->registerFormMacros($this->app); |
68
|
72 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the services provided by the provider. |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
12 |
|
public function provides() |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
12 |
|
'arcanedev.no-captcha', |
79
|
|
|
\Arcanedev\NoCaptcha\Contracts\NoCaptcha::class |
80
|
9 |
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/* ------------------------------------------------------------------------------------------------ |
84
|
|
|
| Other Functions |
85
|
|
|
| ------------------------------------------------------------------------------------------------ |
86
|
|
|
*/ |
87
|
|
|
/** |
88
|
|
|
* Register NoCaptcha service. |
89
|
|
|
*/ |
90
|
72 |
|
private function registerNoCaptcha() |
91
|
|
|
{ |
92
|
|
|
$this->app->bind('arcanedev.no-captcha', function($app) { |
93
|
|
|
/** @var \Illuminate\Config\Repository $config */ |
94
|
60 |
|
$config = $app['config']; |
95
|
|
|
|
96
|
60 |
|
return new NoCaptcha( |
97
|
60 |
|
$config->get('no-captcha.secret'), |
98
|
60 |
|
$config->get('no-captcha.sitekey'), |
99
|
60 |
|
$config->get('no-captcha.lang') |
100
|
45 |
|
); |
101
|
72 |
|
}); |
102
|
|
|
|
103
|
72 |
|
$this->app->bind( |
104
|
72 |
|
\Arcanedev\NoCaptcha\Contracts\NoCaptcha::class, |
105
|
18 |
|
'arcanedev.no-captcha' |
106
|
54 |
|
); |
107
|
72 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Register Validator rules. |
111
|
|
|
* |
112
|
72 |
|
* @param \Illuminate\Foundation\Application $app |
113
|
|
|
*/ |
114
|
72 |
|
private function registerValidatorRules($app) |
115
|
72 |
|
{ |
116
|
72 |
|
$app['validator']->extend('captcha', function($attribute, $value) use ($app) { |
117
|
72 |
|
unset($attribute); |
118
|
|
|
$ip = $app['request']->getClientIp(); |
119
|
|
|
|
120
|
|
|
return $app['arcanedev.no-captcha']->verify($value, $ip); |
121
|
|
|
}); |
122
|
72 |
|
} |
123
|
|
|
|
124
|
72 |
|
/** |
125
|
|
|
* Register Form Macros. |
126
|
|
|
* |
127
|
36 |
|
* @param \Illuminate\Foundation\Application $app |
128
|
36 |
|
*/ |
129
|
|
|
private function registerFormMacros($app) |
130
|
36 |
|
{ |
131
|
72 |
|
if ($app->bound('form')) { |
132
|
72 |
|
$app['form']->macro('captcha', function(array $attributes = []) use ($app) { |
133
|
|
|
return $app['arcanedev.no-captcha']->display($attributes); |
134
|
|
|
}); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: