Skip to content
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

Cache encoder detection #11

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/Encoder/EncoderDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,55 @@

use Soap\Engine\Metadata\Model\XsdType;
use stdClass;
use WeakMap;

final class EncoderDetector
{
/**
* @var WeakMap<XsdType, XmlEncoder<mixed, string>>
*/
private WeakMap $cache;

public static function default(): self
{
/** @var self $self */
static $self = new self();

return $self;
}

private function __construct()
{
/** @var WeakMap<XsdType, XmlEncoder<mixed, string>> cache */
$this->cache = new WeakMap();
}

/**
* @return XmlEncoder<mixed, string>
*
* @psalm-suppress InvalidReturnType, PossiblyInvalidArgument, InvalidReturnStatement - The simple type detector could return string|null, but should not be an issue here.
* @psalm-suppress InvalidArgument, InvalidReturnType, PossiblyInvalidArgument, InvalidReturnStatement - The simple type detector could return string|null, but should not be an issue here.
*/
public function __invoke(Context $context): XmlEncoder
{
$type = $context->type;
if ($cached = $this->cache[$type] ?? null) {
return $cached;
}

$meta = $type->getMeta();

$encoder = match(true) {
$meta->isSimple()->unwrapOr(false) => (new SimpleType\EncoderDetector())($context),
$meta->isSimple()->unwrapOr(false) => SimpleType\EncoderDetector::default()($context),
default => $this->detectComplexTypeEncoder($type, $context),
};

if (!$encoder instanceof Feature\ListAware && $meta->isRepeatingElement()->unwrapOr(false)) {
$encoder = new RepeatingElementEncoder($encoder);
}

return new ErrorHandlingEncoder($encoder);
$encoder = new ErrorHandlingEncoder($encoder);

return $this->cache[$type] = $encoder;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Encoder/ObjectEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private function decorateLensForType(Lens $lens, TypeMeta $meta): Lens
*/
private function detectProperties(Context $context): array
{
$type = (new ComplexTypeBuilder())($context);
$type = ComplexTypeBuilder::default()($context);

return reindex(
sort_by(
Expand Down
8 changes: 8 additions & 0 deletions src/Encoder/SimpleType/EncoderDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

final class EncoderDetector
{
public static function default(): self
{
/** @var self $self */
static $self = new self();

return $self;
}

/**
* @return XmlEncoder<mixed, string|null>
*/
Expand Down
2 changes: 1 addition & 1 deletion src/EncoderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,6 @@ public function hasRegisteredComplexTypeForXsdType(XsdType $type): bool
*/
public function detectEncoderForContext(Context $context): XmlEncoder
{
return (new EncoderDetector())($context);
return EncoderDetector::default()($context);
}
}
8 changes: 8 additions & 0 deletions src/TypeInference/ComplexTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

final class ComplexTypeBuilder
{
public static function default(): self
{
/** @var self $self */
static $self = new self();

return $self;
}

public function __invoke(Context $context): Type
{
$type = $context->metadata->getTypes()->fetchByNameAndXmlNamespace(
Expand Down