bearsunday /
BEAR.QueryRepository
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace BEAR\QueryRepository; |
||
| 6 | |||
| 7 | use BEAR\RepositoryModule\Annotation\MarshallerOptions; |
||
| 8 | use Override; |
||
| 9 | use Ray\Di\ProviderInterface; |
||
| 10 | use Symfony\Component\Cache\Marshaller\DefaultMarshaller; |
||
| 11 | use Symfony\Component\Cache\Marshaller\DeflateMarshaller; |
||
| 12 | use Symfony\Component\Cache\Marshaller\MarshallerInterface; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Provider for creating marshaller instances based on configuration options |
||
| 16 | * |
||
| 17 | * Supports the following marshaller types: |
||
| 18 | * - 'default': Basic marshaller with optional igbinary support |
||
| 19 | * - 'deflate': Compression-enabled marshaller (requires zlib extension) |
||
| 20 | * |
||
| 21 | * @implements ProviderInterface<MarshallerInterface|null> |
||
| 22 | */ |
||
| 23 | final readonly class MarshallerProvider implements ProviderInterface |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 24 | { |
||
| 25 | /** @param array{enabled?: bool, type?: string, use_igbinary?: bool} $options Marshalling options */ |
||
| 26 | public function __construct( |
||
| 27 | #[MarshallerOptions] |
||
| 28 | private array $options = [], |
||
| 29 | ) { |
||
| 30 | } |
||
| 31 | |||
| 32 | #[Override] |
||
| 33 | public function get(): MarshallerInterface|null |
||
| 34 | { |
||
| 35 | return $this->createMarshaller($this->options); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Create marshaller instance based on options |
||
| 40 | * |
||
| 41 | * @param array{enabled?: bool, type?: string, use_igbinary?: bool} $options |
||
| 42 | */ |
||
| 43 | private function createMarshaller(array $options): MarshallerInterface|null |
||
| 44 | { |
||
| 45 | if (empty($options) || ($options['enabled'] ?? false) !== true) { |
||
| 46 | return null; |
||
| 47 | } |
||
| 48 | |||
| 49 | $typeString = $options['type'] ?? 'default'; |
||
| 50 | $type = MarshallerType::tryFrom($typeString) ?? MarshallerType::DEFAULT; |
||
| 51 | $useIgbinary = $options['use_igbinary'] ?? false; |
||
| 52 | |||
| 53 | return match ($type) { |
||
| 54 | MarshallerType::DEFAULT => $this->createDefaultMarshaller($useIgbinary), |
||
| 55 | MarshallerType::DEFLATE => $this->createDeflateMarshaller($useIgbinary), |
||
| 56 | }; |
||
| 57 | } |
||
| 58 | |||
| 59 | private function createDefaultMarshaller(bool $useIgbinary): DefaultMarshaller |
||
| 60 | { |
||
| 61 | return new DefaultMarshaller($useIgbinary); |
||
| 62 | } |
||
| 63 | |||
| 64 | private function createDeflateMarshaller(bool $useIgbinary): DeflateMarshaller |
||
| 65 | { |
||
| 66 | $defaultMarshaller = $this->createDefaultMarshaller($useIgbinary); |
||
| 67 | |||
| 68 | return new DeflateMarshaller($defaultMarshaller); |
||
| 69 | } |
||
| 70 | } |
||
| 71 |