-
Notifications
You must be signed in to change notification settings - Fork 22
/
CoreGatewayFactory.php
240 lines (197 loc) · 9.46 KB
/
CoreGatewayFactory.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace Payum\Core;
use GuzzleHttp\Psr7\Request;
use Http\Adapter\Buzz\Client as HttpBuzzClient;
use Http\Adapter\Guzzle5\Client as HttpGuzzle5Client;
use Http\Adapter\Guzzle6\Client as HttpGuzzle6Client;
use Http\Adapter\Guzzle7\Client as HttpGuzzle7Client;
use Http\Client\Curl\Client as HttpCurlClient;
use Http\Client\Socket\Client as HttpSocketClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
use Http\Message\MessageFactory\DiactorosMessageFactory;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use Http\Message\StreamFactory\DiactorosStreamFactory;
use Http\Message\StreamFactory\GuzzleStreamFactory;
use LogicException;
use Nyholm\Psr7\Factory\HttplugFactory;
use Payum\Core\Action\AuthorizePaymentAction;
use Payum\Core\Action\CapturePaymentAction;
use Payum\Core\Action\ExecuteSameRequestWithModelDetailsAction;
use Payum\Core\Action\GetCurrencyAction;
use Payum\Core\Action\GetTokenAction;
use Payum\Core\Action\PayoutPayoutAction;
use Payum\Core\Bridge\Httplug\HttplugClient;
use Payum\Core\Bridge\PlainPhp\Action\GetHttpRequestAction;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Bridge\Twig\Action\RenderTemplateAction;
use Payum\Core\Bridge\Twig\TwigUtil;
use Payum\Core\Extension\EndlessCycleDetectorExtension;
use Symfony\Component\HttpClient\HttplugClient as SymfonyHttplugClient;
use Twig\Environment;
use Twig\Loader\ChainLoader;
class CoreGatewayFactory implements GatewayFactoryInterface
{
/**
* @var array
*/
protected $defaultConfig;
public function __construct(array $defaultConfig = [])
{
$this->defaultConfig = $defaultConfig;
}
public function create(array $config = [])
{
$config = ArrayObject::ensureArrayObject($config);
$config->defaults($this->createConfig());
$gateway = new Gateway();
$this->buildClosures($config);
$this->buildActions($gateway, $config);
$this->buildApis($gateway, $config);
$this->buildExtensions($gateway, $config);
return $gateway;
}
public function createConfig(array $config = [])
{
$config = ArrayObject::ensureArrayObject($config);
$config->defaults($this->defaultConfig);
$config->defaults([
'httplug.message_factory' => function (ArrayObject $config) {
if (class_exists(MessageFactoryDiscovery::class)) {
return MessageFactoryDiscovery::find();
}
if (class_exists(Request::class)) {
return new GuzzleMessageFactory();
}
if (class_exists(\Laminas\Diactoros\Request::class) || class_exists(\Zend\Diactoros\Request::class)) {
return new DiactorosMessageFactory();
}
if (class_exists(\Nyholm\Psr7\Request::class)) {
return new HttplugFactory();
}
throw new LogicException('The httplug.message_factory could not be guessed. Install one of the following packages: php-http/guzzle7-adapter, zendframework/zend-diactoros. You can also overwrite the config option with your implementation.');
},
'httplug.stream_factory' => function (ArrayObject $config) {
if (class_exists(StreamFactoryDiscovery::class)) {
return StreamFactoryDiscovery::find();
}
if (class_exists(Request::class)) {
return new GuzzleStreamFactory();
}
if (class_exists(\Zend\Diactoros\Request::class)) {
return new DiactorosStreamFactory();
}
if (class_exists(\Nyholm\Psr7\Request::class)) {
return new HttplugFactory();
}
throw new LogicException('The httplug.stream_factory could not be guessed. Install one of the following packages: php-http/guzzle7-adapter, zendframework/zend-diactoros. You can also overwrite the config option with your implementation.');
},
'httplug.client' => function (ArrayObject $config) {
if (class_exists(HttpClientDiscovery::class)) {
return HttpClientDiscovery::find();
}
if (class_exists(HttpGuzzle7Client::class)) {
return new HttpGuzzle7Client();
}
if (class_exists(HttpGuzzle6Client::class)) {
return new HttpGuzzle6Client();
}
if (class_exists(HttpGuzzle5Client::class)) {
return new HttpGuzzle5Client();
}
if (class_exists(SymfonyHttplugClient::class)) {
return new SymfonyHttplugClient();
}
if (class_exists(HttpSocketClient::class)) {
return new HttpSocketClient();
}
if (class_exists(HttpCurlClient::class)) {
return new HttpCurlClient($config['httplug.message_factory'], $config['httplug.stream_factory']);
}
if (class_exists(HttpBuzzClient::class)) {
return new HttpBuzzClient();
}
throw new LogicException('The httplug.client could not be guessed. Install one of the following packages: php-http/guzzle7-adapter, php-http/guzzle7-adapter. You can also overwrite the config option with your implementation.');
},
'payum.http_client' => fn (ArrayObject $config) => new HttplugClient($config['httplug.client']),
'payum.template.layout' => '@PayumCore/layout.html.twig',
'twig.env' => fn () => new Environment(new ChainLoader()),
'twig.register_paths' => function (ArrayObject $config) {
$twig = $config['twig.env'];
if (! $twig instanceof Environment) {
throw new LogicException(sprintf(
'The `twig.env config option must contains instance of Twig\Environment but got %s`',
get_debug_type($twig)
));
}
TwigUtil::registerPaths($twig, $config['payum.paths']);
return null;
},
'payum.action.get_http_request' => new GetHttpRequestAction(),
'payum.action.capture_payment' => new CapturePaymentAction(),
'payum.action.authorize_payment' => new AuthorizePaymentAction(),
'payum.action.payout_payout' => new PayoutPayoutAction(),
'payum.action.execute_same_request_with_model_details' => new ExecuteSameRequestWithModelDetailsAction(),
'payum.action.render_template' => fn (ArrayObject $config) => new RenderTemplateAction($config['twig.env'], $config['payum.template.layout']),
'payum.extension.endless_cycle_detector' => new EndlessCycleDetectorExtension(),
'payum.action.get_currency' => fn (ArrayObject $config) => new GetCurrencyAction(),
'payum.prepend_actions' => [],
'payum.prepend_extensions' => [],
'payum.prepend_apis' => [],
'payum.default_options' => [],
'payum.required_options' => [],
'payum.api.http_client' => fn (ArrayObject $config) => $config['payum.http_client'],
'payum.security.token_storage' => null,
]);
if ($config['payum.security.token_storage']) {
$config['payum.action.get_token'] = fn (ArrayObject $config) => new GetTokenAction($config['payum.security.token_storage']);
}
$config['payum.paths'] = array_replace([
'PayumCore' => __DIR__ . '/Resources/views',
], $config['payum.paths'] ?: []);
return (array) $config;
}
protected function buildClosures(ArrayObject $config): void
{
// with higher priority
foreach (['httplug.message_factory', 'httplug.stream_factory', 'httplug.client', 'payum.http_client', 'payum.paths', 'twig.env', 'twig.register_paths'] as $name) {
$value = $config[$name];
if (is_callable($value)) {
$config[$name] = call_user_func($value, $config);
}
}
foreach ($config as $name => $value) {
if (is_callable($value) && ! (is_string($value) && function_exists('\\' . $value))) {
$config[$name] = call_user_func($value, $config);
}
}
}
protected function buildActions(Gateway $gateway, ArrayObject $config): void
{
foreach ($config as $name => $value) {
if (str_starts_with($name, 'payum.action')) {
$prepend = in_array($name, $config['payum.prepend_actions']);
$gateway->addAction($value, $prepend);
}
}
}
protected function buildApis(Gateway $gateway, ArrayObject $config): void
{
foreach ($config as $name => $value) {
if (str_starts_with($name, 'payum.api')) {
$prepend = in_array($name, $config['payum.prepend_apis']);
$gateway->addApi($value, $prepend);
}
}
}
protected function buildExtensions(Gateway $gateway, ArrayObject $config): void
{
foreach ($config as $name => $value) {
if (str_starts_with($name, 'payum.extension')) {
$prepend = in_array($name, $config['payum.prepend_extensions']);
$gateway->addExtension($value, $prepend);
}
}
}
}