Skip to content

Commit

Permalink
cache type descriptors and media types (#145)
Browse files Browse the repository at this point in the history
Co-authored-by: jlabedo <[email protected]>
Co-authored-by: Dariusz Gafka <[email protected]>
  • Loading branch information
3 people authored Jun 9, 2023
1 parent 39ceb03 commit 42e9a08
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/Ecotone/src/Messaging/Conversion/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ final class MediaType

private const TYPE_PARAMETER = 'type';

private static array $parsedMediaTypes = [];

private string $type;
private string $subtype;
/**
Expand Down Expand Up @@ -184,6 +186,9 @@ public static function createWithParameters(string $type, string $subtype, array
*/
public static function parseMediaType(string $mediaType): self
{
if (isset(self::$parsedMediaTypes[$mediaType])) {
return self::$parsedMediaTypes[$mediaType];
}
$parsedMediaType = explode('/', $mediaType);

Assert::keyExists($parsedMediaType, 0, "Passed media type {$mediaType} has no type");
Expand All @@ -196,7 +201,7 @@ public static function parseMediaType(string $mediaType): self
$parameters[$parameter[0]] = $parameter[1];
}

return self::createWithParameters($parsedMediaType[0], $subtype, $parameters);
return self::$parsedMediaTypes[$mediaType] = self::createWithParameters($parsedMediaType[0], $subtype, $parameters);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public function getMethodCall(Message $message): MethodCall
)) {
$convertedData = $this->doConversion($this->interfaceToCall, $interfaceParameter, $data, $sourceTypeDescriptor, $currentParameterMediaType, $parameterType, $parameterMediaType);
} elseif ($message->getHeaders()->containsKey(MessageHeaders::TYPE_ID)) {
$resolvedTargetParameterType = $message->getHeaders()->containsKey(MessageHeaders::TYPE_ID) ? TypeDescriptor::create($message->getHeaders()->get(MessageHeaders::TYPE_ID)) : $parameterType;
$resolvedTargetParameterType = TypeDescriptor::create($message->getHeaders()->get(MessageHeaders::TYPE_ID));
if ($this->canConvertParameter(
$sourceTypeDescriptor,
$currentParameterMediaType,
Expand Down
14 changes: 12 additions & 2 deletions packages/Ecotone/src/Messaging/Handler/TypeDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ final class TypeDescriptor implements Type
public const MIXED = 'mixed';
public const NULL = 'null';

private static array $cache = [];

private string $type;

private static function resolveCollectionTypes(string $foundCollectionTypes): array
Expand Down Expand Up @@ -163,6 +165,10 @@ public function isCompatibleWith(Type $toCompare): bool
return false;
}

if (is_a($this->type, $toCompare->getTypeHint(), true)) {
return true;
}

if ($this->isAnything() || $toCompare->isAnything()) {
return true;
}
Expand All @@ -173,7 +179,7 @@ public function isCompatibleWith(Type $toCompare): bool

if (! $this->isScalar() && $toCompare->isScalar()) {
if ($this->isClassOrInterface()) {
if ($this->equals(TypeDescriptor::create(TypeDescriptor::OBJECT))) {
if ($this->isCompoundObjectType()) {
return false;
}

Expand Down Expand Up @@ -256,6 +262,10 @@ public static function isItTypeOfExistingClassOrInterface(string $typeHint): boo

private static function initialize(?string $typeHint, ?string $docBlockTypeDescription): Type
{
$cacheKey = $typeHint . ':' . $docBlockTypeDescription;
if (isset(self::$cache[$cacheKey])) {
return self::$cache[$cacheKey];
}
$resolvedType = [];
foreach (self::resolveType($typeHint)->getUnionTypes() as $declarationType) {
if ($declarationType->isIterable() && ! $declarationType->isCollection() && $docBlockTypeDescription) {
Expand All @@ -280,7 +290,7 @@ private static function initialize(?string $typeHint, ?string $docBlockTypeDescr
}
}

return UnionTypeDescriptor::createWith($resolvedType);
return self::$cache[$cacheKey] = UnionTypeDescriptor::createWith($resolvedType);
}

/**
Expand Down

0 comments on commit 42e9a08

Please sign in to comment.