From 765789290c4b43dedf3f7e4f96ef32cbe82a9087 Mon Sep 17 00:00:00 2001 From: Jan Pedryc Date: Mon, 9 Dec 2024 12:58:45 +0100 Subject: [PATCH] perf(memoization): add memoization for build referencable schemas (#30) --- src/Concerns/Referencable.php | 4 +++- tests/Fixtures/ExampleSchema.php | 21 +++++++++++++++++++++ tests/MemoizationTest.php | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/ExampleSchema.php create mode 100644 tests/MemoizationTest.php diff --git a/src/Concerns/Referencable.php b/src/Concerns/Referencable.php index fd36525..4dfb8fe 100644 --- a/src/Concerns/Referencable.php +++ b/src/Concerns/Referencable.php @@ -14,6 +14,8 @@ trait Referencable { + static $buildReferencableSchemas = []; + public static function ref(?string $objectId = null): Schema { $instance = app(static::class); @@ -38,6 +40,6 @@ public static function ref(?string $objectId = null): Schema $baseRef = '#/components/securitySchemes/'; } - return Schema::ref($baseRef.$instance->build()->objectId, $objectId); + return self::$buildReferencableSchemas[static::class . "_{$objectId}"] ??= Schema::ref($baseRef.$instance->build()->objectId, $objectId); } } diff --git a/tests/Fixtures/ExampleSchema.php b/tests/Fixtures/ExampleSchema.php new file mode 100644 index 0000000..4737e32 --- /dev/null +++ b/tests/Fixtures/ExampleSchema.php @@ -0,0 +1,21 @@ +properties( + Schema::string('example_string'), + Schema::integer('example_integer'), + Schema::boolean('example_boolean'), + ); + } +} diff --git a/tests/MemoizationTest.php b/tests/MemoizationTest.php new file mode 100644 index 0000000..eef9be0 --- /dev/null +++ b/tests/MemoizationTest.php @@ -0,0 +1,14 @@ +