|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sebdesign\ArtisanCloudflare; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider; |
|
8
|
|
|
|
|
9
|
|
|
class ServiceProvider extends IlluminateServiceProvider |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Bootstrap the application services. |
|
13
|
|
|
*/ |
|
14
|
108 |
|
public function boot(): void |
|
15
|
|
|
{ |
|
16
|
108 |
|
if ($this->app->runningInConsole()) { |
|
17
|
108 |
|
$this->publishes([ |
|
18
|
108 |
|
__DIR__.'/../config/cloudflare.php' => config_path('cloudflare.php'), |
|
19
|
108 |
|
], 'config'); |
|
20
|
|
|
} |
|
21
|
108 |
|
} |
|
22
|
|
|
|
|
23
|
108 |
|
public function register(): void |
|
24
|
|
|
{ |
|
25
|
108 |
|
$this->mergeConfigFrom(__DIR__.'/../config/cloudflare.php', 'cloudflare'); |
|
26
|
|
|
|
|
27
|
108 |
|
$this->registerClient(); |
|
28
|
108 |
|
$this->registerCommands(); |
|
29
|
108 |
|
$this->registerMacros(); |
|
30
|
108 |
|
} |
|
31
|
|
|
|
|
32
|
108 |
|
protected function registerClient(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->app->bind(Client::class, function () { |
|
35
|
9 |
|
return new Client( |
|
36
|
9 |
|
$this->bootGuzzleClient(), |
|
37
|
9 |
|
$this->app['log'] |
|
38
|
|
|
); |
|
39
|
108 |
|
}); |
|
40
|
108 |
|
} |
|
41
|
|
|
|
|
42
|
9 |
|
protected function bootGuzzleClient(): GuzzleClient |
|
43
|
|
|
{ |
|
44
|
9 |
|
$config = $this->app['config']['cloudflare']; |
|
45
|
|
|
|
|
46
|
9 |
|
$authorization = isset($config['token']) ? [ |
|
47
|
3 |
|
'Authorization' => "Bearer {$config['token']}", |
|
48
|
|
|
] : [ |
|
49
|
6 |
|
'X-Auth-Key' => $config['key'], |
|
50
|
9 |
|
'X-Auth-Email' => $config['email'], |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
9 |
|
return new GuzzleClient([ |
|
54
|
9 |
|
'base_uri' => Client::BASE_URI, |
|
55
|
9 |
|
\GuzzleHttp\RequestOptions::HEADERS => $authorization, |
|
56
|
|
|
]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
108 |
|
protected function registerCommands(): void |
|
60
|
|
|
{ |
|
61
|
|
|
$this->app->bind(Commands\Cache\Purge::class, function () { |
|
62
|
63 |
|
return new Commands\Cache\Purge( |
|
63
|
63 |
|
$this->app['config']['cloudflare.zones'] ?? [] |
|
64
|
|
|
); |
|
65
|
108 |
|
}); |
|
66
|
|
|
|
|
67
|
|
|
$this->app->bind(Commands\Waf\BlockIP::class, function () { |
|
68
|
60 |
|
return new Commands\Waf\BlockIP( |
|
69
|
60 |
|
$this->app['config']['cloudflare.zones'] ?? [] |
|
70
|
|
|
); |
|
71
|
108 |
|
}); |
|
72
|
|
|
|
|
73
|
108 |
|
$this->commands([ |
|
74
|
108 |
|
Commands\Cache\Purge::class, |
|
75
|
|
|
Commands\Waf\BlockIP::class, |
|
76
|
|
|
]); |
|
77
|
108 |
|
} |
|
78
|
|
|
|
|
79
|
108 |
|
protected function registerMacros(): void |
|
80
|
|
|
{ |
|
81
|
|
|
/* |
|
82
|
|
|
* Transpose with keys. |
|
83
|
|
|
* |
|
84
|
|
|
* Implementation for PHP < 5.6 and Laravel ~5.1. |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Illuminate\Support\Collection |
|
87
|
|
|
*/ |
|
88
|
|
|
Collection::macro('_transpose', function () { |
|
89
|
54 |
|
$keys = $this->keys()->all(); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$params = array_merge([function () use ($keys) { |
|
92
|
54 |
|
return new static(array_combine($keys, func_get_args())); |
|
|
|
|
|
|
93
|
54 |
|
}], $this->values()->toArray()); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
54 |
|
return new static(call_user_func_array('array_map', $params)); |
|
96
|
108 |
|
}); |
|
97
|
|
|
|
|
98
|
|
|
/* |
|
99
|
|
|
* Add a value between each item. |
|
100
|
|
|
* |
|
101
|
|
|
* @param mixed $value |
|
102
|
|
|
* @return \Illuminate\Support\Collection |
|
103
|
|
|
*/ |
|
104
|
|
|
Collection::macro('insertBetween', function ($value) { |
|
105
|
|
|
return $this->values()->flatMap(function ($item, $index) use ($value) { |
|
106
|
54 |
|
return [$index ? $value : null, $item]; |
|
107
|
54 |
|
})->forget(0)->values(); |
|
108
|
108 |
|
}); |
|
109
|
|
|
|
|
110
|
|
|
/* |
|
111
|
|
|
* Reorder the collection according to an array of keys |
|
112
|
|
|
* |
|
113
|
|
|
* @param mixed $keys |
|
114
|
|
|
* @return \Illuminate\Support\Collection |
|
115
|
|
|
*/ |
|
116
|
|
|
Collection::macro('reorder', function ($keys) { |
|
117
|
33 |
|
$order = $this->getArrayableItems($keys); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
return $this->sortBy(function ($item, $key) use ($order) { |
|
|
|
|
|
|
120
|
33 |
|
return array_search($key, $order); |
|
121
|
33 |
|
}); |
|
122
|
108 |
|
}); |
|
123
|
108 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Determine if the provider is deferred. |
|
127
|
|
|
* |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
6 |
|
public function isDeferred() |
|
131
|
|
|
{ |
|
132
|
6 |
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get the services provided by the provider. |
|
137
|
|
|
* |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
9 |
|
public function provides() |
|
141
|
|
|
{ |
|
142
|
|
|
return [ |
|
143
|
9 |
|
Client::class, |
|
144
|
|
|
Commands\Cache\Purge::class, |
|
145
|
|
|
]; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.