-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathConfig.php
More file actions
141 lines (119 loc) · 3.29 KB
/
Config.php
File metadata and controls
141 lines (119 loc) · 3.29 KB
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
<?php
declare(strict_types=1);
namespace Saloon;
use DateTimeImmutable;
use Saloon\Enums\PipeOrder;
use Saloon\Contracts\Sender;
use Psr\Clock\ClockInterface;
use Saloon\Http\PendingRequest;
use Saloon\Http\Senders\GuzzleSender;
use Saloon\Helpers\MiddlewarePipeline;
use Saloon\Exceptions\StrayRequestException;
final class Config
{
/**
* Default Sender
*
* @var class-string<\Saloon\Contracts\Sender>
*/
public static string $defaultSender = GuzzleSender::class;
/**
* Default TLS Method (v1.2)
*/
public static int $defaultTlsMethod = STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
/**
* Default timeout (in seconds) for establishing a connection.
*/
public static int $defaultConnectionTimeout = 10;
/**
* Default timeout (in seconds) for making requests
*/
public static int $defaultRequestTimeout = 30;
/**
* Resolve the sender with a callback
*
* @var callable|null
*/
private static mixed $senderResolver = null;
/**
* Global Middleware Pipeline
*/
private static ?MiddlewarePipeline $globalMiddlewarePipeline = null;
/**
* Whether stray requests should be prevented
*/
private static bool $preventStrayRequests = false;
/**
* Global clock used by built-in time-aware features.
*/
private static ?ClockInterface $clock = null;
/**
* Write a custom sender resolver
*/
public static function setSenderResolver(?callable $senderResolver): void
{
self::$senderResolver = $senderResolver;
}
/**
* Create a new default sender
*/
public static function getDefaultSender(): Sender
{
$senderResolver = self::$senderResolver;
return is_callable($senderResolver) ? $senderResolver() : new self::$defaultSender;
}
/**
* Set the global package clock.
*/
public static function setClock(?ClockInterface $clock): void
{
self::$clock = $clock;
}
/**
* Get the global package clock.
*/
public static function getClock(): ?ClockInterface
{
return self::$clock;
}
/**
* Resolve the current time.
*/
public static function now(): DateTimeImmutable
{
return self::$clock?->now() ?? new DateTimeImmutable;
}
/**
* Update global middleware
*/
public static function globalMiddleware(): MiddlewarePipeline
{
return self::$globalMiddlewarePipeline ??= new MiddlewarePipeline;
}
/**
* Reset global middleware
*/
public static function clearGlobalMiddleware(): void
{
self::$globalMiddlewarePipeline = null;
}
/**
* Throw an exception if a request without a MockClient is made.
*/
public static function preventStrayRequests(): void
{
self::$preventStrayRequests = true;
self::globalMiddleware()->onRequest(static function (PendingRequest $pendingRequest) {
if (self::$preventStrayRequests && ! $pendingRequest->hasMockClient()) {
throw new StrayRequestException;
}
}, order: PipeOrder::LAST);
}
/**
* Allow stray requests without a MockClient.
*/
public static function allowStrayRequests(): void
{
self::$preventStrayRequests = false;
}
}