-
Notifications
You must be signed in to change notification settings - Fork 45
/
SwapServiceProvider.php
224 lines (192 loc) · 5.69 KB
/
SwapServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/*
* This file is part of Laravel Swap.
*
* (c) Florian Voutzinos <florian@voutzinos.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swap\Laravel;
use Exchanger\Exchanger;
use Exchanger\Service\Chain;
use Exchanger\Service\PhpArray;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Swap\Swap;
/**
* Provides the Swap service.
*
* @author Florian Voutzinos <florian@voutzinos.com>
*/
final class SwapServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
$source = realpath(__DIR__.'/../config/swap.php');
$this->publishes([$source => $this->getConfigPath('swap.php')]);
$this->mergeConfigFrom($source, 'swap');
}
/**
* Register the application services.
*/
public function register()
{
$this->registerHttp($this->app);
$this->registerCacheItemPool($this->app);
$this->registerChain($this->app);
$this->registerExchangeRateProvider($this->app);
$this->registerSwap($this->app);
}
/**
* Register the http related stuff.
*
* @param Container $app
*/
private function registerHttp(Container $app)
{
$app->singleton('swap.http_client', function ($app) {
if ($httpClient = $app->config->get('swap.http_client')) {
return $app[$httpClient];
}
return HttpClientDiscovery::find();
});
$app->singleton('swap.request_factory', function ($app) {
if ($requestFactory = $app->config->get('swap.request_factory')) {
return $app[$requestFactory];
}
return MessageFactoryDiscovery::find();
});
}
/**
* Register the core services.
*
* @param Container $app
*/
private function registerServices(Container $app)
{
foreach ($app->config->get('swap.services', []) as $name => $config) {
if (false === $config) {
continue;
}
$class = $this->getServiceClass($name);
$serviceName = sprintf('swap.service.%s', $name);
// The PhpArray service is a particular case
if ('array' === $name) {
$app->singleton($serviceName, function () use ($config) {
return new PhpArray($config);
});
$app->tag($serviceName, 'swap.service');
continue;
}
// Process the regular services
if (!class_exists($class)) {
throw new \RuntimeException(sprintf('The service "%s" does not exist.', $name));
}
if (!is_array($config)) {
$config = [];
}
$app->singleton($serviceName, function ($app) use ($class, $config) {
return new $class($app['swap.http_client'], $app['swap.request_factory'], $config);
});
$app->tag($serviceName, 'swap.service');
}
}
/**
* Register the chain service.
*
* @param Container $app
*/
private function registerChain(Container $app)
{
$app->singleton('swap.chain', function ($app) {
$this->registerServices($app);
return new Chain($app->tagged('swap.service'));
});
}
/**
* Registers the cache.
*
* @param Container $app
*/
private function registerCacheItemPool(Container $app)
{
$app->singleton('swap.cache_item_pool', function ($app) {
if ($cacheItemPool = $app->config->get('swap.cache_item_pool')) {
return $app[$cacheItemPool];
}
if (null === $cache = $app->config->get('swap.cache')) {
return;
}
$repository = $app['cache']->store($cache);
return new LaravelStoreCachePool($repository->getStore());
});
}
/**
* Register the exchange rate provider.
*
* @param Container $app
*/
private function registerExchangeRateProvider(Container $app)
{
$app->singleton('swap.exchange_rate_provider', function ($app) {
return new Exchanger($app['swap.chain'], $app['swap.cache_item_pool'], $app->config->get('swap.options', []));
});
}
/**
* Registers Swap.
*
* @param Container $app
*/
private function registerSwap(Container $app)
{
$app->singleton('swap', function ($app) {
return new Swap($app['swap.exchange_rate_provider']);
});
$app->bind('Swap\Swap', 'swap');
}
/**
* Get the services provided by the provider.
*
* @return string[]
*/
public function provides()
{
return [
'swap',
];
}
/**
* Gets the service class from its name.
*
* @param string $name
*
* @return string
*/
private function getServiceClass($name)
{
// WebserviceX is a special case
if ('webservicex' === $name) {
$name = 'webservice_x';
}
$camelized = ucfirst(implode('', array_map('ucfirst', explode('_', $name))));
return 'Exchanger\\Service\\'.$camelized;
}
/**
* Gets the full path to the config.
*
* @param string $path
*
* @return string
*/
private function getConfigPath($path = '')
{
return app()->basePath().'/config'.($path ? '/'.$path : $path);
}
}