From 204de29123698a91435401b7d984e1df03bbd673 Mon Sep 17 00:00:00 2001 From: "n.shchukin" Date: Sun, 9 Apr 2023 22:59:43 +0300 Subject: [PATCH] Passing null transformer --- src/Resource/ResourceAbstract.php | 2 +- src/Scope.php | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Resource/ResourceAbstract.php b/src/Resource/ResourceAbstract.php index a6864fde..3284c377 100644 --- a/src/Resource/ResourceAbstract.php +++ b/src/Resource/ResourceAbstract.php @@ -110,7 +110,7 @@ public function getTransformer() /** * Set the transformer. * - * @param callable|TransformerAbstract $transformer + * @param callable|TransformerAbstract|null $transformer */ public function setTransformer($transformer): self { diff --git a/src/Scope.php b/src/Scope.php index 45037b66..84ab2e74 100644 --- a/src/Scope.php +++ b/src/Scope.php @@ -269,7 +269,7 @@ public function transformPrimitiveResource() } elseif (is_callable($transformer)) { $transformedData = call_user_func($transformer, $data); } else { - $transformer->setCurrentScope($this); + $transformer->setCurrentScope($this->getCurrentScopeWithoutTransformer()); $transformedData = $transformer->transform($data); } @@ -353,7 +353,7 @@ protected function fireTransformer($transformer, $data): array if (is_callable($transformer)) { $transformedData = call_user_func($transformer, $data); } else { - $transformer->setCurrentScope($this); + $transformer->setCurrentScope($this->getCurrentScopeWithoutTransformer()); $transformedData = $transformer->transform($data); } @@ -462,4 +462,17 @@ protected function getResourceType(): string { return $this->resource->getResourceKey(); } + + /** + * Return current scope with null transformer (due to memory leak) + * + * @return Scope + */ + private function getCurrentScopeWithoutTransformer(): Scope + { + $scopeWithoutTransformer = clone $this; + $scopeWithoutTransformer->getResource()->setTransformer(null); + + return $scopeWithoutTransformer; + } }