-
Notifications
You must be signed in to change notification settings - Fork 442
feat: Make serializer configurable via YAML configuration #1390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat: Make serializer configurable via YAML configuration #1390
Conversation
- Add support for configuring serializers through YAML in RdKafkaContext - Allow serializer specification as a class name, array with options, or instance
pkg/rdkafka/RdKafkaContext.php
Outdated
if (is_string($config['serializer'])) { | ||
$this->setSerializer(new $config['serializer']()); | ||
} elseif (is_array($config['serializer']) && isset($config['serializer']['class'])) { | ||
$serializerClass = $config['serializer']['class']; | ||
$serializerOptions = $config['serializer']['options'] ?? []; | ||
if (!empty($serializerOptions)) { | ||
$this->setSerializer(new $serializerClass($serializerOptions)); | ||
} else { | ||
$this->setSerializer(new $serializerClass()); | ||
} | ||
} elseif ($config['serializer'] instanceof Serializer) { | ||
$this->setSerializer($config['serializer']); | ||
} else { | ||
throw new InvalidArgumentException('Invalid serializer configuration'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I'd prefer something like this, with more verbosity provided for invalid configuration values.
$serializerClass
"normalization" could be extracted to a separate private method. Similarly, exception message/instance creation.
Also, added check that the instance we are creating is actually of the right class. Usually object creation is a domain of an app, but in rare circumstance that somehow user could control what ends up in $config['serializer']
, a malicious user could for example create PHP subprocesses that would allow backdoor access to a server. Unlikely, but it doesn't hurt to check.
if (is_string($config['serializer'])) { | |
$this->setSerializer(new $config['serializer']()); | |
} elseif (is_array($config['serializer']) && isset($config['serializer']['class'])) { | |
$serializerClass = $config['serializer']['class']; | |
$serializerOptions = $config['serializer']['options'] ?? []; | |
if (!empty($serializerOptions)) { | |
$this->setSerializer(new $serializerClass($serializerOptions)); | |
} else { | |
$this->setSerializer(new $serializerClass()); | |
} | |
} elseif ($config['serializer'] instanceof Serializer) { | |
$this->setSerializer($config['serializer']); | |
} else { | |
throw new InvalidArgumentException('Invalid serializer configuration'); | |
} | |
if ($config['serializer'] instanceof Serializer) { | |
$this->setSerializer($config['serializer']); | |
return; | |
} | |
if (is_string($config['serializer']) { | |
$serializerClass = $config['serializer']; | |
} elseif (is_array($config['serializer'] && isset($config['serializer']['class']) { | |
$serializerClass = $config['serializer']['class']; | |
} else { | |
throw new InvalidArgumentException(sprintf( | |
'Invalid serializer configuration. Expected "%s" option to be string, array with "class" property, or %s instance. Received %s instead.', | |
'serializer', | |
Serializer::class, | |
get_debug_type($config['serializer']), | |
); | |
} | |
if (!class_exists($serializerClass) || !is_a($serializerClass, Serializer, true)) { | |
throw new InvalidArgumentException(sprintf( | |
'Invalid serializer configuration. Expected "%s" option to be string, array with "class" property, or %s instance. Received %s instead.', | |
'serializer', | |
Serializer::class, | |
$serializerClass, | |
); | |
} | |
$serializerOptions = $config['serializer']['options'] ?? []; | |
$this->setSerializer(new $serializerClass($serializerOptions)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Steveb-p I have refactored the code according to your suggestions and have added default fallback in order not to break backwards compatibility.
- Ensures robustness by providing a default serializer - Refactoring
This change enables users to specify custom serializers with configuration
options in their Enqueue bundle YAML configuration, improving flexibility
when working with different message formats.