From cc05f92181f74627054ffe89b9fd7c9b78cc8320 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Wed, 21 Aug 2024 23:35:57 -0400 Subject: [PATCH 1/5] Attach behavior via "as foo" with Closure --- framework/base/Component.php | 2 ++ tests/framework/base/ComponentTest.php | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/framework/base/Component.php b/framework/base/Component.php index 7cee3b0720d..9cab8373ae3 100644 --- a/framework/base/Component.php +++ b/framework/base/Component.php @@ -190,6 +190,8 @@ public function __set($name, $value) $name = trim(substr($name, 3)); if ($value instanceof Behavior) { $this->attachBehavior($name, $value); + } elseif ($value instanceof \Closure) { + $this->attachBehavior($name, call_user_func($value)); } elseif ((isset($value['class']) && is_subclass_of($value['class'], Behavior::class)) || (isset($value['__class']) && is_subclass_of($value['__class'], Behavior::class))) { $this->attachBehavior($name, Yii::createObject($value)); } elseif (is_string($value) && is_subclass_of($value, Behavior::class, true)) { diff --git a/tests/framework/base/ComponentTest.php b/tests/framework/base/ComponentTest.php index dca3c37bd91..725a7ee7878 100644 --- a/tests/framework/base/ComponentTest.php +++ b/tests/framework/base/ComponentTest.php @@ -344,6 +344,11 @@ public function testAttachBehavior() $component->{'as c'} = ['__class' => NewBehavior::class]; $this->assertNotNull($component->getBehavior('c')); + + $component->{'as d'} = function () { + return new NewBehavior(); + }; + $this->assertNotNull($component->getBehavior('d')); } public function testAttachBehaviors() From be3d8efe0b7b228c3b98b8403248db15aa257152 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Thu, 22 Aug 2024 15:29:41 -0400 Subject: [PATCH 2/5] Fix attachBehavior test --- tests/framework/base/ComponentTest.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/framework/base/ComponentTest.php b/tests/framework/base/ComponentTest.php index 725a7ee7878..55634b0011a 100644 --- a/tests/framework/base/ComponentTest.php +++ b/tests/framework/base/ComponentTest.php @@ -328,20 +328,19 @@ public function testAttachBehavior() $this->assertTrue($component->hasProperty('p')); $component->test(); $this->assertTrue($component->behaviorCalled); + } - $this->assertSame($behavior, $component->detachBehavior('a')); - $this->assertFalse($component->hasProperty('p')); - $this->expectException('yii\base\UnknownMethodException'); - $component->test(); - - $p = 'as b'; + public function testAs() + { $component = new NewComponent(); - $component->$p = ['class' => 'NewBehavior']; - $this->assertSame($behavior, $component->getBehavior('a')); + $component->{'as a'} = new NewBehavior(); $this->assertTrue($component->hasProperty('p')); $component->test(); $this->assertTrue($component->behaviorCalled); + $component->{'as b'} = ['class' => NewBehavior::class]; + $this->assertNotNull($component->getBehavior('b')); + $component->{'as c'} = ['__class' => NewBehavior::class]; $this->assertNotNull($component->getBehavior('c')); @@ -381,6 +380,9 @@ public function testDetachBehavior() $detachedBehavior = $component->detachBehavior('z'); $this->assertNull($detachedBehavior); + + $this->expectException('yii\base\UnknownMethodException'); + $component->test(); } public function testDetachBehaviors() From ff60985e5128a499c5840cae7f20a953ed49119f Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Thu, 22 Aug 2024 16:12:42 -0400 Subject: [PATCH 3/5] CS fixes --- framework/db/BaseActiveRecord.php | 2 +- framework/db/mssql/Schema.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index 10021e362cc..761bc2cb994 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -1783,7 +1783,7 @@ private function isValueDifferent($newValue, $oldValue) { if (is_array($newValue) && is_array($oldValue)) { // Only sort associative arrays - $sorter = function(&$array) { + $sorter = function (&$array) { if (ArrayHelper::isAssociative($array)) { ksort($array); } diff --git a/framework/db/mssql/Schema.php b/framework/db/mssql/Schema.php index 35908437f48..1b1fb267e51 100644 --- a/framework/db/mssql/Schema.php +++ b/framework/db/mssql/Schema.php @@ -823,5 +823,4 @@ public function createColumnSchemaBuilder($type, $length = null) { return Yii::createObject(ColumnSchemaBuilder::className(), [$type, $length, $this->db]); } - } From 2d38497f0a96974ab621c4198ba60c2ecf4763c3 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Sun, 25 Aug 2024 11:57:46 -0400 Subject: [PATCH 4/5] Revert "CS fixes" This reverts commit ff60985e5128a499c5840cae7f20a953ed49119f. --- framework/db/BaseActiveRecord.php | 2 +- framework/db/mssql/Schema.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index 761bc2cb994..10021e362cc 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -1783,7 +1783,7 @@ private function isValueDifferent($newValue, $oldValue) { if (is_array($newValue) && is_array($oldValue)) { // Only sort associative arrays - $sorter = function (&$array) { + $sorter = function(&$array) { if (ArrayHelper::isAssociative($array)) { ksort($array); } diff --git a/framework/db/mssql/Schema.php b/framework/db/mssql/Schema.php index 1b1fb267e51..35908437f48 100644 --- a/framework/db/mssql/Schema.php +++ b/framework/db/mssql/Schema.php @@ -823,4 +823,5 @@ public function createColumnSchemaBuilder($type, $length = null) { return Yii::createObject(ColumnSchemaBuilder::className(), [$type, $length, $this->db]); } + } From 8724ba350505ec53fcc11406f09eeca9bce90d81 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Wed, 25 Sep 2024 15:46:52 -0400 Subject: [PATCH 5/5] Changelog --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 80a0547e494..294810f9fc7 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -8,6 +8,7 @@ Yii Framework 2 Change Log - Bug #20231: Fix regression introduced in #20167 in `yii\validators\FileValidator` (bizley) - Enh #20247: Support for variadic console controller action methods (brandonkelly) - Bug #20256: Add support for dropping views in MSSQL server when running migrate/fresh (ambrozt) +- Enh #20248: Add support for attaching behaviors in configurations with Closure (timkelty) 2.0.51 July 18, 2024 --------------------