1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\QueryRepository; |
6
|
|
|
|
7
|
|
|
use BEAR\RepositoryModule\Annotation\MarshallerOptions; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Override; |
10
|
|
|
use Ray\Di\ProviderInterface; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use Symfony\Component\Cache\Marshaller\DefaultMarshaller; |
13
|
|
|
use Symfony\Component\Cache\Marshaller\DeflateMarshaller; |
14
|
|
|
use Symfony\Component\Cache\Marshaller\MarshallerInterface; |
15
|
|
|
use Symfony\Component\Cache\Marshaller\SodiumMarshaller; |
16
|
|
|
|
17
|
|
|
use function extension_loaded; |
18
|
|
|
use function is_array; |
19
|
|
|
use function is_string; |
20
|
|
|
use function sprintf; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Provider for creating marshaller instances based on configuration options |
24
|
|
|
* |
25
|
|
|
* @implements ProviderInterface<MarshallerInterface|null> |
26
|
|
|
*/ |
27
|
|
|
final class MarshallerProvider implements ProviderInterface |
28
|
|
|
{ |
29
|
|
|
/** @param array<string, mixed> $options Marshalling options */ |
30
|
|
|
public function __construct( |
31
|
|
|
#[MarshallerOptions] |
32
|
|
|
private readonly array $options = [], |
33
|
|
|
) { |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
#[Override] |
37
|
|
|
public function get(): MarshallerInterface|null |
38
|
|
|
{ |
39
|
|
|
return $this->createMarshaller($this->options); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create marshaller instance based on options |
44
|
|
|
* |
45
|
|
|
* @param array<string, mixed> $options |
46
|
|
|
*/ |
47
|
|
|
private function createMarshaller(array $options): MarshallerInterface|null |
48
|
|
|
{ |
49
|
|
|
if (empty($options) || ($options['enabled'] ?? false) !== true) { |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @var string $type */ |
54
|
|
|
$type = is_string($options['type'] ?? null) ? $options['type'] : 'default'; |
55
|
|
|
$useIgbinary = (bool) ($options['use_igbinary'] ?? false); |
56
|
|
|
|
57
|
|
|
return match ($type) { |
58
|
|
|
'default' => $this->createDefaultMarshaller($useIgbinary), |
59
|
|
|
'deflate' => $this->createDeflateMarshaller($useIgbinary), |
60
|
|
|
'sodium' => $this->createSodiumMarshaller($options, $useIgbinary), |
61
|
|
|
default => throw new InvalidArgumentException(sprintf('Invalid marshaller type: %s', $type)), |
62
|
|
|
}; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function createDefaultMarshaller(bool $useIgbinary): DefaultMarshaller |
66
|
|
|
{ |
67
|
|
|
if ($useIgbinary && ! extension_loaded('igbinary')) { |
68
|
|
|
throw new RuntimeException('igbinary extension is required for igbinary marshaller'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return new DefaultMarshaller($useIgbinary); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function createDeflateMarshaller(bool $useIgbinary): DeflateMarshaller |
75
|
|
|
{ |
76
|
|
|
if (! extension_loaded('zlib')) { |
77
|
|
|
throw new RuntimeException('zlib extension is required for deflate marshaller'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$defaultMarshaller = $this->createDefaultMarshaller($useIgbinary); |
81
|
|
|
|
82
|
|
|
return new DeflateMarshaller($defaultMarshaller); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** @param array<string, mixed> $options */ |
86
|
|
|
private function createSodiumMarshaller(array $options, bool $useIgbinary): SodiumMarshaller |
87
|
|
|
{ |
88
|
|
|
if (! extension_loaded('sodium')) { |
89
|
|
|
throw new RuntimeException('sodium extension is required for sodium marshaller'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (! isset($options['keys']) || ! is_array($options['keys']) || empty($options['keys'])) { |
93
|
|
|
throw new InvalidArgumentException('Keys are required for sodium marshaller'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** @var array<string> $keys */ |
97
|
|
|
$keys = $options['keys']; |
98
|
|
|
$defaultMarshaller = $this->createDefaultMarshaller($useIgbinary); |
99
|
|
|
|
100
|
|
|
return new SodiumMarshaller($keys, $defaultMarshaller); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.