Skip to content

Commit 7d94550

Browse files
authored
Merge pull request #11 from veewee/cached-encoder-detection
Cache encoder detection
2 parents 14754ce + ee5aad6 commit 7d94550

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

src/Encoder/EncoderDetector.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,55 @@
55

66
use Soap\Engine\Metadata\Model\XsdType;
77
use stdClass;
8+
use WeakMap;
89

910
final class EncoderDetector
1011
{
12+
/**
13+
* @var WeakMap<XsdType, XmlEncoder<mixed, string>>
14+
*/
15+
private WeakMap $cache;
16+
17+
public static function default(): self
18+
{
19+
/** @var self $self */
20+
static $self = new self();
21+
22+
return $self;
23+
}
24+
25+
private function __construct()
26+
{
27+
/** @var WeakMap<XsdType, XmlEncoder<mixed, string>> cache */
28+
$this->cache = new WeakMap();
29+
}
30+
1131
/**
1232
* @return XmlEncoder<mixed, string>
1333
*
14-
* @psalm-suppress InvalidReturnType, PossiblyInvalidArgument, InvalidReturnStatement - The simple type detector could return string|null, but should not be an issue here.
34+
* @psalm-suppress InvalidArgument, InvalidReturnType, PossiblyInvalidArgument, InvalidReturnStatement - The simple type detector could return string|null, but should not be an issue here.
1535
*/
1636
public function __invoke(Context $context): XmlEncoder
1737
{
1838
$type = $context->type;
39+
if ($cached = $this->cache[$type] ?? null) {
40+
return $cached;
41+
}
42+
1943
$meta = $type->getMeta();
2044

2145
$encoder = match(true) {
22-
$meta->isSimple()->unwrapOr(false) => (new SimpleType\EncoderDetector())($context),
46+
$meta->isSimple()->unwrapOr(false) => SimpleType\EncoderDetector::default()($context),
2347
default => $this->detectComplexTypeEncoder($type, $context),
2448
};
2549

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

30-
return new ErrorHandlingEncoder($encoder);
54+
$encoder = new ErrorHandlingEncoder($encoder);
55+
56+
return $this->cache[$type] = $encoder;
3157
}
3258

3359
/**

src/Encoder/ObjectEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private function decorateLensForType(Lens $lens, TypeMeta $meta): Lens
222222
*/
223223
private function detectProperties(Context $context): array
224224
{
225-
$type = (new ComplexTypeBuilder())($context);
225+
$type = ComplexTypeBuilder::default()($context);
226226

227227
return reindex(
228228
sort_by(

src/Encoder/SimpleType/EncoderDetector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
final class EncoderDetector
1515
{
16+
public static function default(): self
17+
{
18+
/** @var self $self */
19+
static $self = new self();
20+
21+
return $self;
22+
}
23+
1624
/**
1725
* @return XmlEncoder<mixed, string|null>
1826
*/

src/EncoderRegistry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,6 @@ public function hasRegisteredComplexTypeForXsdType(XsdType $type): bool
317317
*/
318318
public function detectEncoderForContext(Context $context): XmlEncoder
319319
{
320-
return (new EncoderDetector())($context);
320+
return EncoderDetector::default()($context);
321321
}
322322
}

src/TypeInference/ComplexTypeBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
final class ComplexTypeBuilder
1212
{
13+
public static function default(): self
14+
{
15+
/** @var self $self */
16+
static $self = new self();
17+
18+
return $self;
19+
}
20+
1321
public function __invoke(Context $context): Type
1422
{
1523
$type = $context->metadata->getTypes()->fetchByNameAndXmlNamespace(

0 commit comments

Comments
 (0)