-
-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathMiddleware.php
More file actions
58 lines (48 loc) · 1.61 KB
/
Middleware.php
File metadata and controls
58 lines (48 loc) · 1.61 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
<?php
declare(strict_types=1);
namespace Kreait\Firebase\Http;
use Beste\Json;
use Closure;
use GuzzleHttp\Psr7\Query;
use Psr\Http\Message\RequestInterface;
use function array_merge;
use function ltrim;
use function str_ends_with;
/**
* @internal
*/
final class Middleware
{
/**
* Ensures that the ".json" suffix is added to URIs and that the content type is set correctly.
*
* @return callable(callable): Closure
*/
public static function ensureJsonSuffix(): callable
{
return static fn(callable $handler): Closure => static function (RequestInterface $request, ?array $options = null) use ($handler) {
$uri = $request->getUri();
$path = '/'.ltrim($uri->getPath(), '/');
if (!str_ends_with($path, '.json')) {
$uri = $uri->withPath($path.'.json');
$request = $request->withUri($uri);
}
return $handler($request, $options);
};
}
/**
* @param array<string, mixed>|null $override
*
* @return callable(callable): Closure
*/
public static function addDatabaseAuthVariableOverride(?array $override): callable
{
return static fn(callable $handler): Closure => static function (RequestInterface $request, ?array $options = null) use ($handler, $override) {
$uri = $request->getUri();
$uri = $uri->withQuery(Query::build(
array_merge(Query::parse($uri->getQuery()), ['auth_variable_override' => Json::encode($override)]),
));
return $handler($request->withUri($uri), $options);
};
}
}