From ecc5556b2a532ecead05104774e1f5a036b67f40 Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 13 Mar 2022 23:21:17 +0300 Subject: [PATCH 1/6] Fix yii\caching\Dependency::generateReusableHash() --- framework/caching/Dependency.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/framework/caching/Dependency.php b/framework/caching/Dependency.php index 9093e8ef196..d401a94e2fb 100644 --- a/framework/caching/Dependency.php +++ b/framework/caching/Dependency.php @@ -99,16 +99,31 @@ public static function resetReusableData() /** * Generates a unique hash that can be used for retrieving reusable dependency data. + * * @return string a unique hash value for this cache dependency. - * @see reusable + * @see $reusable */ protected function generateReusableHash() { $data = $this->data; - $this->data = null; // https://github.com/yiisoft/yii2/issues/3052 - $key = sha1(serialize($this)); + $this->data = null; // https://github.com/yiisoft/yii2/issues/3052 + + try { + $serialized = serialize($this); + } catch (\Exception $e) { + $clone = clone $this; + // unserialeable properties are nulled + foreach ($clone as $name => $value) { + if (is_object($value) && $value instanceof \Closure) { + $clone->{$name} = null; + } + } + $serialized = serialize($clone); + } + $this->data = $data; - return $key; + + return sha1($serialized); } /** From d6372b644006be4fc762319b627d8781860f8ebe Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 14 Mar 2022 00:30:18 +0300 Subject: [PATCH 2/6] Update DbQueryDependencyTest.php --- .../caching/DbQueryDependencyTest.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/framework/caching/DbQueryDependencyTest.php b/tests/framework/caching/DbQueryDependencyTest.php index 0d718544f77..f46c952f1f6 100644 --- a/tests/framework/caching/DbQueryDependencyTest.php +++ b/tests/framework/caching/DbQueryDependencyTest.php @@ -108,4 +108,30 @@ public function testCustomMethodCallback() $this->assertTrue($dependency->isChanged($cache)); } + + /** + * @depends testCustomMethod + */ + public function testReusableAndCustomMethodCallback() + { + $db = $this->getConnection(false); + $cache = new ArrayCache(); + + $dependency = new DbQueryDependency(); + $dependency->db = $db; + $dependency->query = (new Query()) + ->from('dependency_item') + ->andWhere(['value' => 'not exist']); + $dependency->reusable = true; + $dependency->method = function (Query $query, $db) { + return $query->orWhere(['value' => 'initial'])->exists($db); + }; + + $dependency->evaluateDependency($cache); + $this->assertFalse($dependency->isChanged($cache)); + + $db->createCommand()->delete('dependency_item')->execute(); + + $this->assertTrue($dependency->isChanged($cache)); + } } From 3447cfe70b3e0435f8df3560c103ff881379e0d3 Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 14 Mar 2022 00:34:03 +0300 Subject: [PATCH 3/6] Update CHANGELOG.md --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 5117c60c6a2..04e0026ddae 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -7,6 +7,7 @@ Yii Framework 2 Change Log - Bug #19243: Handle `finfo_open` for tar.xz as `application/octet-stream` on PHP 8.1 (longthanhtran) - Bug #19235: Fix return type compatibility of `yii\web\SessionIterator` class methods for PHP 8.1 (virtual-designer) - Bug #19291: Reset errors and validators in `yii\base\Model::__clone()` (WinterSilence) +- Bug #19303: Fix serialization in `yii\caching\Dependency::generateReusableHash()` (WinterSilence) 2.0.45 February 11, 2022 From 38cce787f2f2957aa5112eeece21a5e5fa767315 Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 14 Mar 2022 06:04:00 +0300 Subject: [PATCH 4/6] Update Dependency.php --- framework/caching/Dependency.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/framework/caching/Dependency.php b/framework/caching/Dependency.php index d401a94e2fb..91056abc9fe 100644 --- a/framework/caching/Dependency.php +++ b/framework/caching/Dependency.php @@ -105,13 +105,12 @@ public static function resetReusableData() */ protected function generateReusableHash() { - $data = $this->data; - $this->data = null; // https://github.com/yiisoft/yii2/issues/3052 + $clone = clone $this; + $clone->data = null; // https://github.com/yiisoft/yii2/issues/3052 try { - $serialized = serialize($this); + $serialized = serialize($clone); } catch (\Exception $e) { - $clone = clone $this; // unserialeable properties are nulled foreach ($clone as $name => $value) { if (is_object($value) && $value instanceof \Closure) { @@ -121,8 +120,6 @@ protected function generateReusableHash() $serialized = serialize($clone); } - $this->data = $data; - return sha1($serialized); } From 3cc84e4a18ffc9e835e01b5126f4e40a094b3cdf Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 14 Mar 2022 10:08:24 +0300 Subject: [PATCH 5/6] Update framework/caching/Dependency.php Co-authored-by: Bizley --- framework/caching/Dependency.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/caching/Dependency.php b/framework/caching/Dependency.php index 91056abc9fe..38437465ca4 100644 --- a/framework/caching/Dependency.php +++ b/framework/caching/Dependency.php @@ -111,7 +111,7 @@ protected function generateReusableHash() try { $serialized = serialize($clone); } catch (\Exception $e) { - // unserialeable properties are nulled + // unserializable properties are nulled foreach ($clone as $name => $value) { if (is_object($value) && $value instanceof \Closure) { $clone->{$name} = null; From 13268ab47396f660b5960aae4913cb5287494b5f Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 28 Mar 2022 21:28:59 +0300 Subject: [PATCH 6/6] Update comment Dependency::generateReusableHash() --- framework/caching/Dependency.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/caching/Dependency.php b/framework/caching/Dependency.php index 38437465ca4..a0a02f16a86 100644 --- a/framework/caching/Dependency.php +++ b/framework/caching/Dependency.php @@ -101,7 +101,7 @@ public static function resetReusableData() * Generates a unique hash that can be used for retrieving reusable dependency data. * * @return string a unique hash value for this cache dependency. - * @see $reusable + * @see reusable */ protected function generateReusableHash() {