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