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
|
78 |
|
public function boot() |
15
|
|
|
{ |
16
|
78 |
|
if ($this->app->runningInConsole()) { |
17
|
78 |
|
$this->publishes([ |
18
|
78 |
|
__DIR__.'/../config/cloudflare.php' => config_path('cloudflare.php'), |
19
|
|
|
], 'config'); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
78 |
|
public function register() |
24
|
|
|
{ |
25
|
78 |
|
$this->mergeConfigFrom(__DIR__.'/../config/cloudflare.php', 'cloudflare'); |
26
|
|
|
|
27
|
78 |
|
$this->registerClient(); |
28
|
78 |
|
$this->registerCommands(); |
29
|
78 |
|
$this->registerMacros(); |
30
|
|
|
} |
31
|
|
|
|
32
|
78 |
|
protected function registerClient() |
33
|
|
|
{ |
34
|
78 |
|
$this->app->bind(Client::class, function () { |
35
|
9 |
|
return new Client( |
36
|
9 |
|
$this->bootGuzzleClient(), |
37
|
9 |
|
$this->app['log'] |
38
|
|
|
); |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
9 |
|
protected function bootGuzzleClient() |
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
|
6 |
|
'X-Auth-Email' => $config['email'], |
51
|
|
|
]; |
52
|
|
|
|
53
|
9 |
|
return new GuzzleClient([ |
54
|
|
|
'base_uri' => Client::BASE_URI, |
55
|
|
|
\GuzzleHttp\RequestOptions::HEADERS => $authorization, |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
78 |
|
protected function registerCommands() |
60
|
|
|
{ |
61
|
78 |
|
$this->app->bind(Commands\Cache\Purge::class, function () { |
62
|
36 |
|
return new Commands\Cache\Purge( |
63
|
36 |
|
$this->app['config']['cloudflare.zones'] ?? [] |
64
|
|
|
); |
65
|
|
|
}); |
66
|
|
|
|
67
|
78 |
|
$this->commands([ |
68
|
|
|
Commands\Cache\Purge::class, |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
78 |
|
protected function registerMacros() |
73
|
|
|
{ |
74
|
|
|
/* |
75
|
|
|
* Transpose with keys. |
76
|
|
|
* |
77
|
|
|
* Implementation for PHP < 5.6 and Laravel ~5.1. |
78
|
|
|
* |
79
|
|
|
* @return \Illuminate\Support\Collection |
80
|
|
|
*/ |
81
|
78 |
|
Collection::macro('_transpose', function () { |
82
|
33 |
|
$keys = $this->keys()->all(); |
|
|
|
|
83
|
|
|
|
84
|
33 |
|
$params = array_merge([function () use ($keys) { |
85
|
33 |
|
return new static(array_combine($keys, func_get_args())); |
|
|
|
|
86
|
33 |
|
}], $this->values()->toArray()); |
|
|
|
|
87
|
|
|
|
88
|
33 |
|
return new static(call_user_func_array('array_map', $params)); |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
/* |
92
|
|
|
* Add a value between each item. |
93
|
|
|
* |
94
|
|
|
* @param mixed $value |
95
|
|
|
* @return \Illuminate\Support\Collection |
96
|
|
|
*/ |
97
|
78 |
|
Collection::macro('insertBetween', function ($value) { |
98
|
33 |
|
return $this->values()->flatMap(function ($item, $index) use ($value) { |
99
|
33 |
|
return [$index ? $value : null, $item]; |
100
|
33 |
|
})->forget(0)->values(); |
101
|
|
|
}); |
102
|
|
|
|
103
|
|
|
/* |
104
|
|
|
* Reorder the collection according to an array of keys |
105
|
|
|
* |
106
|
|
|
* @param mixed $keys |
107
|
|
|
* @return \Illuminate\Support\Collection |
108
|
|
|
*/ |
109
|
78 |
|
Collection::macro('reorder', function ($keys) { |
110
|
33 |
|
$order = $this->getArrayableItems($keys); |
|
|
|
|
111
|
|
|
|
112
|
33 |
|
return $this->sortBy(function ($item, $key) use ($order) { |
|
|
|
|
113
|
33 |
|
return array_search($key, $order); |
114
|
|
|
}); |
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Determine if the provider is deferred. |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
6 |
|
public function isDeferred() |
124
|
|
|
{ |
125
|
6 |
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the services provided by the provider. |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function provides() |
134
|
|
|
{ |
135
|
|
|
return [ |
136
|
|
|
Client::class, |
137
|
|
|
Commands\Cache\Purge::class, |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
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.